]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/peopletagsalmon.php
19e56cb216a947b8a10eb4d7e6710200059e91c9
[quix0rs-gnu-social.git] / plugins / OStatus / actions / peopletagsalmon.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  */
23
24 if (!defined('STATUSNET')) {
25     exit(1);
26 }
27
28 class PeopletagsalmonAction extends SalmonAction
29 {
30     var $peopletag = null;
31
32     function prepare($args)
33     {
34         parent::prepare($args);
35
36         $id = $this->trimmed('id');
37
38         if (!$id) {
39             // TRANS: Client error displayed trying to perform an action without providing an ID.
40             $this->clientError(_m('No ID.'));
41         }
42
43         $this->peopletag = Profile_list::staticGet('id', $id);
44
45         if (empty($this->peopletag)) {
46             // TRANS: Client error displayed when referring to a non-existing list.
47             $this->clientError(_m('No such list.'));
48         }
49
50         $oprofile = Ostatus_profile::staticGet('peopletag_id', $id);
51
52         if (!empty($oprofile)) {
53             // TRANS: Client error displayed when trying to send a message to a remote list.
54             $this->clientError(_m('Cannot accept remote posts for a remote list.'));
55         }
56
57         return true;
58     }
59
60     /**
61      * We've gotten a follow/subscribe notification from a remote user.
62      * Save a subscription relationship for them.
63      */
64
65     /**
66      * Postel's law: consider a "follow" notification as a "join".
67      */
68     function handleFollow()
69     {
70         $this->handleSubscribe();
71     }
72
73     /**
74      * Postel's law: consider an "unfollow" notification as a "unsubscribe".
75      */
76     function handleUnfollow()
77     {
78         $this->handleUnsubscribe();
79     }
80
81     /**
82      * A remote user subscribed.
83      * @fixme move permission checks and event call into common code,
84      *        currently we're doing the main logic in joingroup action
85      *        and so have to repeat it here.
86      */
87     function handleSubscribe()
88     {
89         $oprofile = $this->ensureProfile();
90         if (!$oprofile) {
91             // TRANS: Client error displayed when referring to a non-existing remote list.
92             $this->clientError(_m('Cannot read profile to set up list subscription.'));
93         }
94         if ($oprofile->isGroup()) {
95             // TRANS: Client error displayed when trying to subscribe a group to a list.
96             $this->clientError(_m('Groups cannot subscribe to lists.'));
97         }
98
99         common_log(LOG_INFO, "Remote profile {$oprofile->uri} subscribing to local peopletag ".$this->peopletag->getBestName());
100         $profile = $oprofile->localProfile();
101
102         if ($this->peopletag->hasSubscriber($profile)) {
103             // Already a member; we'll take it silently to aid in resolving
104             // inconsistencies on the other side.
105             return true;
106         }
107
108         // should we block those whom the tagger has blocked from listening to
109         // his own updates?
110
111         try {
112             Profile_tag_subscription::add($this->peopletag, $profile);
113         } catch (Exception $e) {
114             // TRANS: Server error displayed when subscribing a remote user to a list fails.
115             // TRANS: %1$s is a profile URI, %2$s is a list name.
116             $this->serverError(sprintf(_m('Could not subscribe remote user %1$s to list %2$s.'),
117                                        $oprofile->uri, $this->peopletag->getBestName()));
118         }
119     }
120
121     /**
122      * A remote user unsubscribed from our list.
123      */
124     function handleUnsubscribe()
125     {
126         $oprofile = $this->ensureProfile();
127         if (!$oprofile) {
128             // TRANS: Client error displayed when trying to unsubscribe from non-existing list.
129             $this->clientError(_m('Cannot read profile to cancel list subscription.'));
130         }
131         if ($oprofile->isGroup()) {
132             // TRANS: Client error displayed when trying to unsubscribe a group from a list.
133             $this->clientError(_m('Groups cannot subscribe to lists.'));
134         }
135
136         common_log(LOG_INFO, "Remote profile {$oprofile->uri} unsubscribing from local peopletag ".$this->peopletag->getBestName());
137         $profile = $oprofile->localProfile();
138
139         try {
140                 Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $profile->id);
141
142         } catch (Exception $e) {
143             // TRANS: Client error displayed when trying to unsubscribe a remote user from a list fails.
144             // TRANS: %1$s is a profile URL, %2$s is a list name.
145             $this->serverError(sprintf(_m('Could not unsubscribe remote user %1$s from list %2$s.'),
146                                        $oprofile->uri, $this->peopletag->getBestName()));
147             return;
148         }
149     }
150 }