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('GNUSOCIAL')) { exit(1); }
23 * @package OStatusPlugin
24 * @author James Walker <james@status.net>
26 class GroupsalmonAction extends SalmonAction
30 protected function prepare(array $args=array())
32 parent::prepare($args);
34 $id = $this->trimmed('id');
37 // TRANS: Client error.
38 $this->clientError(_m('No ID.'));
41 $this->group = User_group::getKV('id', $id);
43 if (!$this->group instanceof User_group) {
44 // TRANS: Client error.
45 $this->clientError(_m('No such group.'));
49 $this->target = $this->group;
51 $oprofile = Ostatus_profile::getKV('group_id', $id);
52 if ($oprofile instanceof Ostatus_profile) {
53 // TRANS: Client error.
54 $this->clientError(_m('Cannot accept remote posts for a remote group.'));
61 * We've gotten a post event on the Salmon backchannel, probably a reply.
65 // @fixme process all objects?
66 switch ($this->activity->objects[0]->type) {
67 case ActivityObject::ARTICLE:
68 case ActivityObject::BLOGENTRY:
69 case ActivityObject::NOTE:
70 case ActivityObject::STATUS:
71 case ActivityObject::COMMENT:
74 // TRANS: Client exception.
75 throw new ClientException('Cannot handle that kind of post.');
78 // Notice must be to the attention of this group
79 if (empty($this->activity->context->attention)) {
80 // TRANS: Client exception.
81 throw new ClientException("Not to the attention of anyone.");
83 $uri = common_local_url('groupbyid', array('id' => $this->group->id));
85 if (!array_key_exists($uri, $this->activity->context->attention)) {
86 // TRANS: Client exception.
87 throw new ClientException("Not to the attention of this group.");
91 $profile = $this->ensureProfile();
96 * We've gotten a follow/subscribe notification from a remote user.
97 * Save a subscription relationship for them.
101 * Postel's law: consider a "follow" notification as a "join".
103 function handleFollow()
109 * Postel's law: consider an "unfollow" notification as a "leave".
111 function handleUnfollow()
113 $this->handleLeave();
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.
122 function handleJoin()
124 $oprofile = $this->ensureProfile();
125 if (!$oprofile instanceof Ostatus_profile) {
126 // TRANS: Client error.
127 $this->clientError(_m('Cannot read profile to set up group membership.'));
129 if ($oprofile->isGroup()) {
130 // TRANS: Client error.
131 $this->clientError(_m('Groups cannot join groups.'));
134 common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
135 $profile = $oprofile->localProfile();
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.
143 if (Group_block::isBlocked($this->group, $profile)) {
144 // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
145 $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
149 $profile->joinGroup($this->group);
150 } catch (Exception $e) {
151 // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
152 $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
153 $oprofile->uri, $this->group->nickname));
158 * A remote user left our group.
160 function handleLeave()
162 $oprofile = $this->ensureProfile();
163 if (!$oprofile instanceof Ostatus_profile) {
164 // TRANS: Client error displayed when group membership cannot be cancelled
165 // TRANS: because the remote profile could not be read.
166 $this->clientError(_m('Cannot read profile to cancel group membership.'));
168 if ($oprofile->isGroup()) {
169 // TRANS: Client error displayed when trying to have a group join another group.
170 $this->clientError(_m('Groups cannot join groups.'));
173 common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
174 $profile = $oprofile->localProfile();
177 $profile->leaveGroup($this->group);
178 } catch (Exception $e) {
179 // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
180 $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
181 $oprofile->uri, $this->group->nickname));