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.'));
53 * We've gotten a post event on the Salmon backchannel, probably a reply.
58 switch ($this->act->object->type) {
59 case ActivityObject::ARTICLE:
60 case ActivityObject::BLOGENTRY:
61 case ActivityObject::NOTE:
62 case ActivityObject::STATUS:
63 case ActivityObject::COMMENT:
66 throw new ClientException("Can't handle that kind of post.");
69 // Notice must be to the attention of this group
71 $context = $this->act->context;
73 if (empty($context->attention)) {
74 throw new ClientException("Not to the attention of anyone.");
76 $uri = common_local_url('groupbyid', array('id' => $this->group->id));
77 if (!in_array($context->attention, $uri)) {
78 throw new ClientException("Not to the attention of this group.");
82 $profile = $this->ensureProfile();
83 // @fixme save the post
87 * We've gotten a follow/subscribe notification from a remote user.
88 * Save a subscription relationship for them.
92 * Postel's law: consider a "follow" notification as a "join".
94 function handleFollow()
100 * Postel's law: consider an "unfollow" notification as a "leave".
102 function handleUnfollow()
104 $this->handleLeave();
108 * A remote user joined our group.
109 * @fixme move permission checks and event call into common code,
110 * currently we're doing the main logic in joingroup action
111 * and so have to repeat it here.
114 function handleJoin()
116 $oprofile = $this->ensureProfile();
118 $this->clientError(_m("Can't read profile to set up group membership."));
120 if ($oprofile->isGroup()) {
121 $this->clientError(_m("Groups can't join groups."));
124 common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
125 $profile = $oprofile->localProfile();
127 if ($profile->isMember($this->group)) {
128 // Already a member; we'll take it silently to aid in resolving
129 // inconsistencies on the other side.
133 if (Group_block::isBlocked($this->group, $profile)) {
134 $this->clientError(_('You have been blocked from that group by the admin.'), 403);
139 // @fixme that event currently passes a user from main UI
140 // Event should probably move into Group_member::join
141 // and take a Profile object.
143 //if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
144 Group_member::join($this->group->id, $profile->id);
145 //Event::handle('EndJoinGroup', array($this->group, $profile));
147 } catch (Exception $e) {
148 $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
149 $oprofile->uri, $this->group->nickname));
154 * A remote user left our group.
157 function handleLeave()
159 $oprofile = $this->ensureProfile();
161 $this->clientError(_m("Can't read profile to cancel group membership."));
163 if ($oprofile->isGroup()) {
164 $this->clientError(_m("Groups can't join groups."));
167 common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
168 $profile = $oprofile->localProfile();
171 // @fixme event needs to be refactored as above
172 //if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
173 Group_member::leave($this->group->id, $profile->id);
174 //Event::handle('EndLeaveGroup', array($this->group, $profile));
176 } catch (Exception $e) {
177 $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
178 $oprofile->uri, $this->group->nickname));