3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Feed of group memberships for a user, in ActivityStreams format
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Feed of group memberships for a user, in ActivityStreams format
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2010 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47 * @link http://status.net/
50 class AtompubmembershipfeedAction extends ApiAuthAction
52 private $_profile = null;
53 private $_memberships = null;
56 * For initializing members of the class.
58 * @param array $argarray misc. arguments
60 * @return boolean true
63 function prepare($argarray)
65 parent::prepare($argarray);
67 $profileId = $this->trimmed('profile');
69 $this->_profile = Profile::staticGet('id', $profileId);
71 if (empty($this->_profile)) {
72 throw new ClientException(_('No such profile.'), 404);
75 $offset = ($this->page-1) * $this->count;
76 $limit = $this->count + 1;
78 $this->_memberships = Group_member::byMember($this->_profile->id,
88 * @param array $argarray is ignored since it's now passed in in prepare()
93 function handle($argarray=null)
95 parent::handle($argarray);
97 switch ($_SERVER['REQUEST_METHOD']) {
103 $this->addMembership();
106 throw new ClientException(_('HTTP method not supported.'), 405);
114 * Show a feed of favorite activity streams objects
121 header('Content-Type: application/atom+xml; charset=utf-8');
123 $url = common_local_url('AtomPubMembershipFeed',
124 array('profile' => $this->_profile->id));
126 $feed = new Atom10Feed(true);
128 $feed->addNamespace('activity',
129 'http://activitystrea.ms/spec/1.0/');
131 $feed->addNamespace('poco',
132 'http://portablecontacts.net/spec/1.0');
134 $feed->addNamespace('media',
135 'http://purl.org/syndication/atommedia');
139 $feed->setUpdated('now');
141 $feed->addAuthor($this->_profile->getBestName(),
142 $this->_profile->getURI());
144 $feed->setTitle(sprintf(_("%s group memberships"),
145 $this->_profile->getBestName()));
147 $feed->setSubtitle(sprintf(_("Groups %s is a member of on %s"),
148 $this->_profile->getBestName(),
149 common_config('site', 'name')));
151 $feed->addLink(common_local_url('usergroups',
153 $this->_profile->nickname)));
156 array('rel' => 'self',
157 'type' => 'application/atom+xml'));
159 // If there's more...
161 if ($this->page > 1) {
163 array('rel' => 'first',
164 'type' => 'application/atom+xml'));
166 $feed->addLink(common_local_url('AtomPubMembershipFeed',
168 $this->_profile->id),
171 array('rel' => 'prev',
172 'type' => 'application/atom+xml'));
175 if ($this->_memberships->N > $this->count) {
177 $feed->addLink(common_local_url('AtomPubMembershipFeed',
179 $this->_profile->id),
182 array('rel' => 'next',
183 'type' => 'application/atom+xml'));
188 while ($this->_memberships->fetch()) {
190 // We get one more than needed; skip that one
194 if ($i > $this->count) {
198 $act = $this->_memberships->asActivity();
199 $feed->addEntryRaw($act->asString(false, false, false));
202 $this->raw($feed->getString());
211 function addMembership()
213 // XXX: Refactor this; all the same for atompub
215 if (empty($this->auth_user) ||
216 $this->auth_user->id != $this->_profile->id) {
217 throw new ClientException(_("Can't add someone else's".
218 " membership"), 403);
221 $xml = file_get_contents('php://input');
223 $dom = DOMDocument::loadXML($xml);
225 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
226 $dom->documentElement->localName != 'entry') {
227 // TRANS: Client error displayed when not using an Atom entry.
228 throw new ClientException(_('Atom post must be an Atom entry.'));
232 $activity = new Activity($dom->documentElement);
236 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
238 if ($activity->verb != ActivityVerb::JOIN) {
239 // TRANS: Client error displayed when not using the POST verb.
240 // TRANS: Do not translate POST.
241 throw new ClientException(_('Can only handle Join activities.'));
245 $groupObj = $activity->objects[0];
247 if ($groupObj->type != ActivityObject::GROUP) {
248 throw new ClientException(_('Can only fave notices.'));
252 $group = User_group::staticGet('uri', $groupObj->id);
255 // XXX: import from listed URL or something
256 throw new ClientException(_('Unknown group.'));
259 $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
260 'group_id' => $group->id));
263 throw new ClientException(_('Already a member.'));
266 $profile = $this->auth_user->getProfile();
268 if (Group_block::isBlocked($group, $profile)) {
269 // XXX: import from listed URL or something
270 throw new ClientException(_('Blocked by admin.'));
273 if (Event::handle('StartJoinGroup', array($group, $this->auth_user))) {
274 $membership = Group_member::join($group->id, $this->auth_user->id);
275 Event::handle('EndJoinGroup', array($group, $this->auth_user));
278 Event::handle('EndAtomPubNewActivity', array($activity, $membership));
281 if (!empty($membership)) {
282 $act = $membership->asActivity();
284 header('Content-Type: application/atom+xml; charset=utf-8');
285 header('Content-Location: ' . $act->selfLink);
288 $this->raw($act->asString(true, true, true));
294 * Return true if read only.
298 * @param array $args other arguments
300 * @return boolean is read only action?
303 function isReadOnly($args)
305 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
306 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
314 * Return last modified, if applicable.
318 * @return string last modified http header
320 function lastModified()
322 // For comparison with If-Last-Modified
323 // If not applicable, return null
328 * Return etag, if applicable.
332 * @return string etag http header
341 * Does this require authentication?
343 * @return boolean true if delete, else false
346 function requiresAuth()
348 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
349 $_SERVER['REQUEST_METHOD'] == 'HEAD') {