]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/groupsalmon.php
OStatus: initial hookup of remote group membership (notice delivery not yet working...
[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         return true;
50     }
51
52     /**
53      * We've gotten a post event on the Salmon backchannel, probably a reply.
54      */
55
56     function handlePost()
57     {
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:
64             break;
65         default:
66             throw new ClientException("Can't handle that kind of post.");
67         }
68
69         // Notice must be to the attention of this group
70
71         $context = $this->act->context;
72
73         if (empty($context->attention)) {
74             throw new ClientException("Not to the attention of anyone.");
75         } else {
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.");
79             }
80         }
81
82         $profile = $this->ensureProfile();
83         // @fixme save the post
84     }
85
86     /**
87      * We've gotten a follow/subscribe notification from a remote user.
88      * Save a subscription relationship for them.
89      */
90
91     /**
92      * Postel's law: consider a "follow" notification as a "join".
93      */
94     function handleFollow()
95     {
96         $this->handleJoin();
97     }
98
99     /**
100      * Postel's law: consider an "unfollow" notification as a "leave".
101      */
102     function handleUnfollow()
103     {
104         $this->handleLeave();
105     }
106
107     /**
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.
112      */
113
114     function handleJoin()
115     {
116         $oprofile = $this->ensureProfile();
117         if (!$oprofile) {
118             $this->clientError(_m("Can't read profile to set up group membership."));
119         }
120         if ($oprofile->isGroup()) {
121             $this->clientError(_m("Groups can't join groups."));
122         }
123
124         common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
125         $profile = $oprofile->localProfile();
126
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.
130             return true;
131         }
132
133         if (Group_block::isBlocked($this->group, $profile)) {
134             $this->clientError(_('You have been blocked from that group by the admin.'), 403);
135             return false;
136         }
137
138         try {
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.
142             //
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));
146             //}
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));
150         }
151     }
152
153     /**
154      * A remote user left our group.
155      */
156
157     function handleLeave()
158     {
159         $oprofile = $this->ensureProfile();
160         if (!$oprofile) {
161             $this->clientError(_m("Can't read profile to cancel group membership."));
162         }
163         if ($oprofile->isGroup()) {
164             $this->clientError(_m("Groups can't join groups."));
165         }
166
167         common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
168         $profile = $oprofile->localProfile();
169
170         try {
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));
175             //}
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));
179             return;
180         }
181     }
182
183 }