3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET')) {
25 * @package OStatusPlugin
26 * @author James Walker <james@status.net>
28 class GroupsalmonAction extends SalmonAction
32 function prepare($args)
34 parent::prepare($args);
36 $id = $this->trimmed('id');
39 // TRANS: Client error.
40 $this->clientError(_m('No ID.'));
43 $this->group = User_group::staticGet('id', $id);
45 if (empty($this->group)) {
46 // TRANS: Client error.
47 $this->clientError(_m('No such group.'));
51 $this->target = $this->group;
53 $oprofile = Ostatus_profile::staticGet('group_id', $id);
55 // TRANS: Client error.
56 $this->clientError(_m("Can't accept remote posts for a remote group."));
63 * We've gotten a post event on the Salmon backchannel, probably a reply.
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:
76 // TRANS: Client exception.
77 throw new ClientException("Can't handle that kind of post.");
80 // Notice must be to the attention of this group
81 $context = $this->activity->context;
83 if (empty($context->attention)) {
84 // TRANS: Client exception.
85 throw new ClientException("Not to the attention of anyone.");
87 $uri = common_local_url('groupbyid', array('id' => $this->group->id));
88 if (!in_array($uri, $context->attention)) {
89 // TRANS: Client exception.
90 throw new ClientException("Not to the attention of this group.");
94 $profile = $this->ensureProfile();
99 * We've gotten a follow/subscribe notification from a remote user.
100 * Save a subscription relationship for them.
104 * Postel's law: consider a "follow" notification as a "join".
106 function handleFollow()
112 * Postel's law: consider an "unfollow" notification as a "leave".
114 function handleUnfollow()
116 $this->handleLeave();
120 * A remote user joined our group.
121 * @fixme move permission checks and event call into common code,
122 * currently we're doing the main logic in joingroup action
123 * and so have to repeat it here.
125 function handleJoin()
127 $oprofile = $this->ensureProfile();
129 // TRANS: Client error.
130 $this->clientError(_m("Can't read profile to set up group membership."));
132 if ($oprofile->isGroup()) {
133 // TRANS: Client error.
134 $this->clientError(_m("Groups can't join groups."));
137 common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
138 $profile = $oprofile->localProfile();
140 if ($profile->isMember($this->group)) {
141 // Already a member; we'll take it silently to aid in resolving
142 // inconsistencies on the other side.
146 if (Group_block::isBlocked($this->group, $profile)) {
147 $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
152 // @fixme that event currently passes a user from main UI
153 // Event should probably move into Group_member::join
154 // and take a Profile object.
156 //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
157 Group_member::join($this->group->id, $profile->id);
158 //Event::handle('EndJoinGroup', array($this->group, $profile));
160 } catch (Exception $e) {
161 // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
162 $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
163 $oprofile->uri, $this->group->nickname));
168 * A remote user left our group.
170 function handleLeave()
172 $oprofile = $this->ensureProfile();
174 $this->clientError(_m("Can't read profile to cancel group membership."));
176 if ($oprofile->isGroup()) {
177 $this->clientError(_m("Groups can't join groups."));
180 common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
181 $profile = $oprofile->localProfile();
184 // @fixme event needs to be refactored as above
185 //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
186 Group_member::leave($this->group->id, $profile->id);
187 //Event::handle('EndLeaveGroup', array($this->group, $profile));
189 } catch (Exception $e) {
190 // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
191 $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
192 $oprofile->uri, $this->group->nickname));