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