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/>.
21 * @package OStatusPlugin
22 * @author James Walker <james@status.net>
25 if (!defined('STATUSNET')) {
29 class GroupsalmonAction extends SalmonAction
33 function prepare($args)
35 parent::prepare($args);
37 $id = $this->trimmed('id');
40 $this->clientError(_('No ID.'));
43 $this->group = User_group::staticGet('id', $id);
45 if (empty($this->group)) {
46 $this->clientError(_('No such group.'));
49 $oprofile = Ostatus_profile::staticGet('group_id', $id);
51 $this->clientError(_m("Can't accept remote posts for a remote group."));
58 * We've gotten a post event on the Salmon backchannel, probably a reply.
63 // @fixme process all objects?
64 switch ($this->act->objects[0]->type) {
65 case ActivityObject::ARTICLE:
66 case ActivityObject::BLOGENTRY:
67 case ActivityObject::NOTE:
68 case ActivityObject::STATUS:
69 case ActivityObject::COMMENT:
72 throw new ClientException("Can't handle that kind of post.");
75 // Notice must be to the attention of this group
77 $context = $this->act->context;
79 if (empty($context->attention)) {
80 throw new ClientException("Not to the attention of anyone.");
82 $uri = common_local_url('groupbyid', array('id' => $this->group->id));
83 if (!in_array($uri, $context->attention)) {
84 throw new ClientException("Not to the attention of this group.");
88 $profile = $this->ensureProfile();
93 * We've gotten a follow/subscribe notification from a remote user.
94 * Save a subscription relationship for them.
98 * Postel's law: consider a "follow" notification as a "join".
100 function handleFollow()
106 * Postel's law: consider an "unfollow" notification as a "leave".
108 function handleUnfollow()
110 $this->handleLeave();
114 * A remote user joined our group.
115 * @fixme move permission checks and event call into common code,
116 * currently we're doing the main logic in joingroup action
117 * and so have to repeat it here.
120 function handleJoin()
122 $oprofile = $this->ensureProfile();
124 $this->clientError(_m("Can't read profile to set up group membership."));
126 if ($oprofile->isGroup()) {
127 $this->clientError(_m("Groups can't join groups."));
130 common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
131 $profile = $oprofile->localProfile();
133 if ($profile->isMember($this->group)) {
134 // Already a member; we'll take it silently to aid in resolving
135 // inconsistencies on the other side.
139 if (Group_block::isBlocked($this->group, $profile)) {
140 $this->clientError(_('You have been blocked from that group by the admin.'), 403);
145 // @fixme that event currently passes a user from main UI
146 // Event should probably move into Group_member::join
147 // and take a Profile object.
149 //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
150 Group_member::join($this->group->id, $profile->id);
151 //Event::handle('EndJoinGroup', array($this->group, $profile));
153 } catch (Exception $e) {
154 $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
155 $oprofile->uri, $this->group->nickname));
160 * A remote user left our group.
163 function handleLeave()
165 $oprofile = $this->ensureProfile();
167 $this->clientError(_m("Can't read profile to cancel group membership."));
169 if ($oprofile->isGroup()) {
170 $this->clientError(_m("Groups can't join groups."));
173 common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
174 $profile = $oprofile->localProfile();
177 // @fixme event needs to be refactored as above
178 //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
179 Group_member::leave($this->group->id, $profile->id);
180 //Event::handle('EndLeaveGroup', array($this->group, $profile));
182 } catch (Exception $e) {
183 $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
184 $oprofile->uri, $this->group->nickname));