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