]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/groupsalmon.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / OStatus / actions / groupsalmon.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @author James Walker <james@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 class GroupsalmonAction extends SalmonAction
30 {
31     var $group = null;
32
33     function prepare($args)
34     {
35         parent::prepare($args);
36
37         $id = $this->trimmed('id');
38
39         if (!$id) {
40             $this->clientError(_('No ID.'));
41         }
42
43         $this->group = User_group::staticGet('id', $id);
44
45         if (empty($this->group)) {
46             $this->clientError(_('No such group.'));
47         }
48
49         $oprofile = Ostatus_profile::staticGet('group_id', $id);
50         if ($oprofile) {
51             $this->clientError(_m("Can't accept remote posts for a remote group."));
52         }
53
54         return true;
55     }
56
57     /**
58      * We've gotten a post event on the Salmon backchannel, probably a reply.
59      */
60
61     function handlePost()
62     {
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:
69             break;
70         default:
71             throw new ClientException("Can't handle that kind of post.");
72         }
73
74         // Notice must be to the attention of this group
75
76         $context = $this->act->context;
77
78         if (empty($context->attention)) {
79             throw new ClientException("Not to the attention of anyone.");
80         } else {
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.");
84             }
85         }
86
87         $profile = $this->ensureProfile();
88         $this->saveNotice();
89     }
90
91     /**
92      * We've gotten a follow/subscribe notification from a remote user.
93      * Save a subscription relationship for them.
94      */
95
96     /**
97      * Postel's law: consider a "follow" notification as a "join".
98      */
99     function handleFollow()
100     {
101         $this->handleJoin();
102     }
103
104     /**
105      * Postel's law: consider an "unfollow" notification as a "leave".
106      */
107     function handleUnfollow()
108     {
109         $this->handleLeave();
110     }
111
112     /**
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.
117      */
118
119     function handleJoin()
120     {
121         $oprofile = $this->ensureProfile();
122         if (!$oprofile) {
123             $this->clientError(_m("Can't read profile to set up group membership."));
124         }
125         if ($oprofile->isGroup()) {
126             $this->clientError(_m("Groups can't join groups."));
127         }
128
129         common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
130         $profile = $oprofile->localProfile();
131
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.
135             return true;
136         }
137
138         if (Group_block::isBlocked($this->group, $profile)) {
139             $this->clientError(_('You have been blocked from that group by the admin.'), 403);
140             return false;
141         }
142
143         try {
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.
147             //
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));
151             //}
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));
155         }
156     }
157
158     /**
159      * A remote user left our group.
160      */
161
162     function handleLeave()
163     {
164         $oprofile = $this->ensureProfile();
165         if (!$oprofile) {
166             $this->clientError(_m("Can't read profile to cancel group membership."));
167         }
168         if ($oprofile->isGroup()) {
169             $this->clientError(_m("Groups can't join groups."));
170         }
171
172         common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
173         $profile = $oprofile->localProfile();
174
175         try {
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));
180             //}
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));
184             return;
185         }
186     }
187
188 }