]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubmembershipfeed.php
updateWithKeys reworked to more reliable execution
[quix0rs-gnu-social.git] / actions / atompubmembershipfeed.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Feed of group memberships for a user, in ActivityStreams format
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  AtomPub
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32
33 /**
34  * Feed of group memberships for a user, in ActivityStreams format
35  *
36  * @category  Action
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2010 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class AtompubmembershipfeedAction extends AtompubAction
44 {
45     private $_profile     = null;
46     private $_memberships = null;
47
48     protected function atompubPrepare()
49     {
50         $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
51
52         if (!$this->_profile instanceof Profile) {
53             // TRANS: Client exception.
54             throw new ClientException(_('No such profile.'), 404);
55         }
56
57         $this->_memberships = Group_member::byMember($this->_profile->id,
58                                                      $this->offset,
59                                                      $this->limit);
60         return true;
61     }
62
63     protected function handleGet()
64     {
65         return $this->showFeed();
66     }
67
68     protected function handlePost()
69     {
70         return $this->addMembership();
71     }
72
73     /**
74      * Show a feed of favorite activity streams objects
75      *
76      * @return void
77      */
78     function showFeed()
79     {
80         header('Content-Type: application/atom+xml; charset=utf-8');
81
82         $url = common_local_url('AtomPubMembershipFeed',
83                                 array('profile' => $this->_profile->id));
84
85         $feed = new Atom10Feed(true);
86
87         $feed->addNamespace('activity',
88                             'http://activitystrea.ms/spec/1.0/');
89
90         $feed->addNamespace('poco',
91                             'http://portablecontacts.net/spec/1.0');
92
93         $feed->addNamespace('media',
94                             'http://purl.org/syndication/atommedia');
95
96         $feed->id = $url;
97
98         $feed->setUpdated('now');
99
100         $feed->addAuthor($this->_profile->getBestName(),
101                          $this->_profile->getURI());
102
103         // TRANS: Title for group membership feed.
104         // TRANS: %s is a username.
105         $feed->setTitle(sprintf(_('Group memberships of %s'),
106                                 $this->_profile->getBestName()));
107
108         // TRANS: Subtitle for group membership feed.
109         // TRANS: %1$s is a username, %2$s is the StatusNet sitename.
110         $feed->setSubtitle(sprintf(_('Groups %1$s is a member of on %2$s'),
111                                    $this->_profile->getBestName(),
112                                    common_config('site', 'name')));
113
114         $feed->addLink(common_local_url('usergroups',
115                                         array('nickname' =>
116                                               $this->_profile->nickname)));
117
118         $feed->addLink($url,
119                        array('rel' => 'self',
120                              'type' => 'application/atom+xml'));
121
122         // If there's more...
123
124         if ($this->page > 1) {
125             $feed->addLink($url,
126                            array('rel' => 'first',
127                                  'type' => 'application/atom+xml'));
128
129             $feed->addLink(common_local_url('AtomPubMembershipFeed',
130                                             array('profile' =>
131                                                   $this->_profile->id),
132                                             array('page' =>
133                                                   $this->page - 1)),
134                            array('rel' => 'prev',
135                                  'type' => 'application/atom+xml'));
136         }
137
138         if ($this->_memberships->N > $this->count) {
139
140             $feed->addLink(common_local_url('AtomPubMembershipFeed',
141                                             array('profile' =>
142                                                   $this->_profile->id),
143                                             array('page' =>
144                                                   $this->page + 1)),
145                            array('rel' => 'next',
146                                  'type' => 'application/atom+xml'));
147         }
148
149         $i = 0;
150
151         while ($this->_memberships->fetch()) {
152
153             // We get one more than needed; skip that one
154
155             $i++;
156
157             if ($i > $this->count) {
158                 break;
159             }
160
161             $act = $this->_memberships->asActivity();
162             $feed->addEntryRaw($act->asString(false, false, false));
163         }
164
165         $this->raw($feed->getString());
166     }
167
168     /**
169      * add a new favorite
170      *
171      * @return void
172      */
173     function addMembership()
174     {
175         // XXX: Refactor this; all the same for atompub
176
177         if (empty($this->auth_user) ||
178             $this->auth_user->id != $this->_profile->id) {
179             // TRANS: Client exception thrown when trying subscribe someone else to a group.
180             throw new ClientException(_("Cannot add someone else's".
181                                         " membership."), 403);
182         }
183
184         $xml = file_get_contents('php://input');
185
186         $dom = DOMDocument::loadXML($xml);
187
188         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
189             $dom->documentElement->localName != 'entry') {
190             // TRANS: Client error displayed when not using an Atom entry.
191             throw new ClientException(_('Atom post must be an Atom entry.'));
192             return;
193         }
194
195         $activity = new Activity($dom->documentElement);
196
197         $membership = null;
198
199         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
200             if ($activity->verb != ActivityVerb::JOIN) {
201                 // TRANS: Client error displayed when not using the join verb.
202                 throw new ClientException(_('Can only handle join activities.'));
203             }
204
205             $groupObj = $activity->objects[0];
206
207             if ($groupObj->type != ActivityObject::GROUP) {
208                 // TRANS: Client exception thrown when trying to join something which is not a group
209                 throw new ClientException(_('Can only join groups.'));
210             }
211
212             $group = User_group::getKV('uri', $groupObj->id);
213
214             if (empty($group)) {
215                 // XXX: import from listed URL or something
216                 // TRANS: Client exception thrown when trying to subscribe to a non-existing group.
217                 throw new ClientException(_('Unknown group.'));
218             }
219
220             $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
221                                                'group_id' => $group->id));
222
223             if (!empty($old)) {
224                 // TRANS: Client exception thrown when trying to subscribe to an already subscribed group.
225                 throw new ClientException(_('Already a member.'));
226             }
227
228             $profile = $this->auth_user->getProfile();
229
230             if (Group_block::isBlocked($group, $profile)) {
231                 // XXX: import from listed URL or something
232                 // TRANS: Client exception thrown when trying to subscribe to group while blocked from that group.
233                 throw new ClientException(_('Blocked by admin.'));
234             }
235
236             $this->auth_user->joinGroup($group);
237
238             Event::handle('EndAtomPubNewActivity', array($activity, $membership));
239         }
240
241         if (!empty($membership)) {
242             $act = $membership->asActivity();
243
244             header('Content-Type: application/atom+xml; charset=utf-8');
245             header('Content-Location: ' . $act->selfLink);
246
247             $this->startXML();
248             $this->raw($act->asString(true, true, true));
249             $this->endXML();
250         }
251     }
252
253     /**
254      * Return last modified, if applicable.
255      *
256      * MAY override
257      *
258      * @return string last modified http header
259      */
260     function lastModified()
261     {
262         // For comparison with If-Last-Modified
263         // If not applicable, return null
264         return null;
265     }
266
267     /**
268      * Return etag, if applicable.
269      *
270      * MAY override
271      *
272      * @return string etag http header
273      */
274     function etag()
275     {
276         return null;
277     }
278 }