]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/groupsalmon.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[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         // @fixme process all objects?
64         switch ($this->activity->objects[0]->type) {
65         case ActivityObject::ARTICLE:
66         case ActivityObject::BLOGENTRY:
67         case ActivityObject::NOTE:
68         case ActivityObject::STATUS:
69         case ActivityObject::COMMENT:
70             break;
71         default:
72             throw new ClientException("Can't handle that kind of post.");
73         }
74
75         // Notice must be to the attention of this group
76
77         $context = $this->activity->context;
78
79         if (empty($context->attention)) {
80             throw new ClientException("Not to the attention of anyone.");
81         } else {
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.");
85             }
86         }
87
88         $profile = $this->ensureProfile();
89         $this->saveNotice();
90     }
91
92     /**
93      * We've gotten a follow/subscribe notification from a remote user.
94      * Save a subscription relationship for them.
95      */
96
97     /**
98      * Postel's law: consider a "follow" notification as a "join".
99      */
100     function handleFollow()
101     {
102         $this->handleJoin();
103     }
104
105     /**
106      * Postel's law: consider an "unfollow" notification as a "leave".
107      */
108     function handleUnfollow()
109     {
110         $this->handleLeave();
111     }
112
113     /**
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.
118      */
119
120     function handleJoin()
121     {
122         $oprofile = $this->ensureProfile();
123         if (!$oprofile) {
124             $this->clientError(_m("Can't read profile to set up group membership."));
125         }
126         if ($oprofile->isGroup()) {
127             $this->clientError(_m("Groups can't join groups."));
128         }
129
130         common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
131         $profile = $oprofile->localProfile();
132
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.
136             return true;
137         }
138
139         if (Group_block::isBlocked($this->group, $profile)) {
140             $this->clientError(_('You have been blocked from that group by the admin.'), 403);
141             return false;
142         }
143
144         try {
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.
148             //
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));
152             //}
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));
156         }
157     }
158
159     /**
160      * A remote user left our group.
161      */
162
163     function handleLeave()
164     {
165         $oprofile = $this->ensureProfile();
166         if (!$oprofile) {
167             $this->clientError(_m("Can't read profile to cancel group membership."));
168         }
169         if ($oprofile->isGroup()) {
170             $this->clientError(_m("Groups can't join groups."));
171         }
172
173         common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
174         $profile = $oprofile->localProfile();
175
176         try {
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));
181             //}
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));
185             return;
186         }
187     }
188
189 }