]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/groupsalmon.php
Merge branch '0.9.x'
[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::staticGet('id', $id);
44
45         if (empty($this->group)) {
46             // TRANS: Client error.
47             $this->clientError(_m('No such group.'));
48         }
49
50         $oprofile = Ostatus_profile::staticGet('group_id', $id);
51         if ($oprofile) {
52             // TRANS: Client error.
53             $this->clientError(_m("Can't accept remote posts for a remote group."));
54         }
55
56         return true;
57     }
58
59     /**
60      * We've gotten a post event on the Salmon backchannel, probably a reply.
61      */
62     function handlePost()
63     {
64         // @fixme process all objects?
65         switch ($this->activity->objects[0]->type) {
66         case ActivityObject::ARTICLE:
67         case ActivityObject::BLOGENTRY:
68         case ActivityObject::NOTE:
69         case ActivityObject::STATUS:
70         case ActivityObject::COMMENT:
71             break;
72         default:
73             // TRANS: Client exception.
74             throw new ClientException("Can't handle that kind of post.");
75         }
76
77         // Notice must be to the attention of this group
78         $context = $this->activity->context;
79
80         if (empty($context->attention)) {
81             // TRANS: Client exception.
82             throw new ClientException("Not to the attention of anyone.");
83         } else {
84             $uri = common_local_url('groupbyid', array('id' => $this->group->id));
85             if (!in_array($uri, $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         $oprofile = $this->ensureProfile();
125         if (!$oprofile) {
126             // TRANS: Client error.
127             $this->clientError(_m("Can't read profile to set up group membership."));
128         }
129         if ($oprofile->isGroup()) {
130             // TRANS: Client error.
131             $this->clientError(_m("Groups can't join groups."));
132         }
133
134         common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
135         $profile = $oprofile->localProfile();
136
137         if ($profile->isMember($this->group)) {
138             // Already a member; we'll take it silently to aid in resolving
139             // inconsistencies on the other side.
140             return true;
141         }
142
143         if (Group_block::isBlocked($this->group, $profile)) {
144             $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
145             return false;
146         }
147
148         try {
149             // @fixme that event currently passes a user from main UI
150             // Event should probably move into Group_member::join
151             // and take a Profile object.
152             //
153             //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
154                 Group_member::join($this->group->id, $profile->id);
155                 //Event::handle('EndJoinGroup', array($this->group, $profile));
156             //}
157         } catch (Exception $e) {
158             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
159             $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
160                                        $oprofile->uri, $this->group->nickname));
161         }
162     }
163
164     /**
165      * A remote user left our group.
166      */
167     function handleLeave()
168     {
169         $oprofile = $this->ensureProfile();
170         if (!$oprofile) {
171             $this->clientError(_m("Can't read profile to cancel group membership."));
172         }
173         if ($oprofile->isGroup()) {
174             $this->clientError(_m("Groups can't join groups."));
175         }
176
177         common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
178         $profile = $oprofile->localProfile();
179
180         try {
181             // @fixme event needs to be refactored as above
182             //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
183                 Group_member::leave($this->group->id, $profile->id);
184                 //Event::handle('EndLeaveGroup', array($this->group, $profile));
185             //}
186         } catch (Exception $e) {
187             // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
188             $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
189                                        $oprofile->uri, $this->group->nickname));
190             return;
191         }
192     }
193 }