]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/groupsalmon.php
Merge request from robmyers upping the PHP version dependency
[quix0rs-gnu-social.git] / plugins / OStatus / actions / groupsalmon.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 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * @package OStatusPlugin
24  * @author James Walker <james@status.net>
25  */
26 class GroupsalmonAction extends SalmonAction
27 {
28     var $group = null;
29
30     protected function prepare(array $args=array())
31     {
32         parent::prepare($args);
33
34         $id = $this->trimmed('id');
35
36         if (!$id) {
37             // TRANS: Client error.
38             $this->clientError(_m('No ID.'));
39         }
40
41         $this->group = User_group::getKV('id', $id);
42
43         if (!$this->group instanceof User_group) {
44             // TRANS: Client error.
45             $this->clientError(_m('No such group.'));
46         }
47
48
49         $this->target = $this->group;
50
51         $remote_group = Ostatus_profile::getKV('group_id', $id);
52         if ($remote_group instanceof Ostatus_profile) {
53             // TRANS: Client error.
54             $this->clientError(_m('Cannot accept remote posts for a remote group.'));
55         }
56
57         return true;
58     }
59
60     /**
61      * We've gotten a post event on the Salmon backchannel, probably a reply.
62      */
63     function handlePost()
64     {
65         // @fixme process all objects?
66         switch ($this->activity->objects[0]->type) {
67         case ActivityObject::ARTICLE:
68         case ActivityObject::BLOGENTRY:
69         case ActivityObject::NOTE:
70         case ActivityObject::STATUS:
71         case ActivityObject::COMMENT:
72             break;
73         default:
74             // TRANS: Client exception.
75             throw new ClientException('Cannot handle that kind of post.');
76         }
77
78         // Notice must be to the attention of this group
79         if (empty($this->activity->context->attention)) {
80             // TRANS: Client exception.
81             throw new ClientException("Not to the attention of anyone.");
82         } else {
83             $uri = common_local_url('groupbyid', array('id' => $this->group->id));
84
85             if (!array_key_exists($uri, $this->activity->context->attention)) {
86                 // TRANS: Client exception.
87                 throw new ClientException("Not to the attention of this group.");
88             }
89         }
90
91         $profile = $this->ensureProfile();
92         $this->saveNotice();
93     }
94
95     /**
96      * We've gotten a follow/subscribe notification from a remote user.
97      * Save a subscription relationship for them.
98      */
99
100     /**
101      * Postel's law: consider a "follow" notification as a "join".
102      */
103     function handleFollow()
104     {
105         $this->handleJoin();
106     }
107
108     /**
109      * Postel's law: consider an "unfollow" notification as a "leave".
110      */
111     function handleUnfollow()
112     {
113         $this->handleLeave();
114     }
115
116     /**
117      * A remote user joined our group.
118      * @fixme move permission checks and event call into common code,
119      *        currently we're doing the main logic in joingroup action
120      *        and so have to repeat it here.
121      */
122     function handleJoin()
123     {
124         if ($this->oprofile->isGroup()) {
125             // TRANS: Client error.
126             $this->clientError(_m('Groups cannot join groups.'));
127         }
128
129         common_log(LOG_INFO, sprintf('Remote profile %s joining local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
130
131         if ($this->actor->isMember($this->group)) {
132             // Already a member; we'll take it silently to aid in resolving
133             // inconsistencies on the other side.
134             return true;
135         }
136
137         if (Group_block::isBlocked($this->group, $this->actor)) {
138             // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
139             $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
140         }
141
142         try {
143             $this->actor->joinGroup($this->group);
144         } catch (Exception $e) {
145             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
146             $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
147                                        $this->oprofile->getUri(), $this->group->getNickname()));
148         }
149     }
150
151     /**
152      * A remote user left our group.
153      */
154     function handleLeave()
155     {
156         // ensureProfile throws exception on failure
157         if ($this->oprofile->isGroup()) {
158             // TRANS: Client error displayed when trying to have a group join another group.
159             throw new AlreadyFulfilledException(_m('Groups cannot be members of groups'));
160         }
161
162         common_log(LOG_INFO, sprintf('Remote profile %s leaving local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
163
164         try {
165             $this->actor->leaveGroup($this->group);
166         } catch (Exception $e) {
167             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
168             $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
169                                        $this->oprofile->getUri(), $this->group->getNickname()));
170         }
171     }
172 }