]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/peopletagsalmon.php
35911a68077526cb506198befb569a86af3bbf2f
[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::getKV('id', $id);
44
45         if (!$this->peopletag instanceof Profile_list) {
46             // TRANS: Client error displayed when referring to a non-existing list.
47             $this->clientError(_m('No such list.'));
48         }
49
50         $this->target = $this->peopletag;
51
52         $remote_list = Ostatus_profile::getKV('peopletag_id', $id);
53
54         if ($remote_list instanceof Ostatus_profile) {
55             // TRANS: Client error displayed when trying to send a message to a remote list.
56             $this->clientError(_m('Cannot accept remote posts for a remote list.'));
57         }
58
59         return true;
60     }
61
62     /**
63      * We've gotten a follow/subscribe notification from a remote user.
64      * Save a subscription relationship for them.
65      */
66
67     /**
68      * Postel's law: consider a "follow" notification as a "join".
69      */
70     function handleFollow()
71     {
72         $this->handleSubscribe();
73     }
74
75     /**
76      * Postel's law: consider an "unfollow" notification as a "unsubscribe".
77      */
78     function handleUnfollow()
79     {
80         $this->handleUnsubscribe();
81     }
82
83     /**
84      * A remote user subscribed.
85      * @fixme move permission checks and event call into common code,
86      *        currently we're doing the main logic in joingroup action
87      *        and so have to repeat it here.
88      */
89     function handleSubscribe()
90     {
91         if ($this->oprofile->isGroup()) {
92             // TRANS: Client error displayed when trying to subscribe a group to a list.
93             $this->clientError(_m('Groups cannot subscribe to lists.'));
94         }
95
96         common_log(LOG_INFO, sprintf('Remote profile %s subscribing to local peopletag %s', $this->oprofile->getUri(), $this->peopletag->getBestName()));
97         if ($this->peopletag->hasSubscriber($this->actor)) {
98             // Already a member; we'll take it silently to aid in resolving
99             // inconsistencies on the other side.
100             return true;
101         }
102
103         // should we block those whom the tagger has blocked from listening to
104         // his own updates?
105
106         try {
107             Profile_tag_subscription::add($this->peopletag, $this->actor);
108         } catch (Exception $e) {
109             // TRANS: Server error displayed when subscribing a remote user to a list fails.
110             // TRANS: %1$s is a profile URI, %2$s is a list name.
111             $this->serverError(sprintf(_m('Could not subscribe remote user %1$s to list %2$s.'),
112                                        $this->oprofile->getUri(), $this->peopletag->getBestName()));
113         }
114     }
115
116     /**
117      * A remote user unsubscribed from our list.
118      *
119      * @return void
120      * @throws Exception through clientError and serverError
121      */
122     function handleUnsubscribe()
123     {
124         if ($this->oprofile->isGroup()) {
125             // TRANS: Client error displayed when trying to unsubscribe a group from a list.
126             $this->clientError(_m('Groups cannot subscribe to lists.'));
127         }
128
129         common_log(LOG_INFO, sprintf('Remote profile %s unsubscribing from local peopletag %s', $this->oprofile->getUri(), $this->peopletag->getBestName()));
130         try {
131             Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $this->actor->id);
132         } catch (Exception $e) {
133             // TRANS: Client error displayed when trying to unsubscribe a remote user from a list fails.
134             // TRANS: %1$s is a profile URL, %2$s is a list name.
135             $this->serverError(sprintf(_m('Could not unsubscribe remote user %1$s from list %2$s.'),
136                                        $this->oprofile->getUri(), $this->peopletag->getBestName()));
137         }
138     }
139 }