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 switch ($this->act->object->type) {
64 case ActivityObject::ARTICLE:
65 case ActivityObject::BLOGENTRY:
66 case ActivityObject::NOTE:
67 case ActivityObject::STATUS:
68 case ActivityObject::COMMENT:
71 throw new ClientException("Can't handle that kind of post.");
74 // Notice must be to the attention of this group
76 $context = $this->act->context;
78 if (empty($context->attention)) {
79 throw new ClientException("Not to the attention of anyone.");
81 $uri = common_local_url('groupbyid', array('id' => $this->group->id));
82 if (!in_array($uri, $context->attention)) {
83 throw new ClientException("Not to the attention of this group.");
87 $profile = $this->ensureProfile();
92 * We've gotten a follow/subscribe notification from a remote user.
93 * Save a subscription relationship for them.
97 * Postel's law: consider a "follow" notification as a "join".
99 function handleFollow()
105 * Postel's law: consider an "unfollow" notification as a "leave".
107 function handleUnfollow()
109 $this->handleLeave();
113 * A remote user joined our group.
114 * @fixme move permission checks and event call into common code,
115 * currently we're doing the main logic in joingroup action
116 * and so have to repeat it here.
119 function handleJoin()
121 $oprofile = $this->ensureProfile();
123 $this->clientError(_m("Can't read profile to set up group membership."));
125 if ($oprofile->isGroup()) {
126 $this->clientError(_m("Groups can't join groups."));
129 common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
130 $profile = $oprofile->localProfile();
132 if ($profile->isMember($this->group)) {
133 // Already a member; we'll take it silently to aid in resolving
134 // inconsistencies on the other side.
138 if (Group_block::isBlocked($this->group, $profile)) {
139 $this->clientError(_('You have been blocked from that group by the admin.'), 403);
144 // @fixme that event currently passes a user from main UI
145 // Event should probably move into Group_member::join
146 // and take a Profile object.
148 //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
149 Group_member::join($this->group->id, $profile->id);
150 //Event::handle('EndJoinGroup', array($this->group, $profile));
152 } catch (Exception $e) {
153 $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
154 $oprofile->uri, $this->group->nickname));
159 * A remote user left our group.
162 function handleLeave()
164 $oprofile = $this->ensureProfile();
166 $this->clientError(_m("Can't read profile to cancel group membership."));
168 if ($oprofile->isGroup()) {
169 $this->clientError(_m("Groups can't join groups."));
172 common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
173 $profile = $oprofile->localProfile();
176 // @fixme event needs to be refactored as above
177 //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
178 Group_member::leave($this->group->id, $profile->id);
179 //Event::handle('EndLeaveGroup', array($this->group, $profile));
181 } catch (Exception $e) {
182 $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
183 $oprofile->uri, $this->group->nickname));