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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * Feed of group memberships for a user, in ActivityStreams format
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/
43 class AtompubmembershipfeedAction extends AtompubAction
45 private $_profile = null;
46 private $_memberships = null;
48 protected function atompubPrepare()
50 $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
52 if (!$this->_profile instanceof Profile) {
53 // TRANS: Client exception.
54 throw new ClientException(_('No such profile.'), 404);
57 $this->_memberships = Group_member::byMember($this->_profile->id,
63 protected function handleGet()
65 return $this->showFeed();
68 protected function handlePost()
70 return $this->addMembership();
74 * Show a feed of favorite activity streams objects
80 header('Content-Type: application/atom+xml; charset=utf-8');
82 $url = common_local_url('AtomPubMembershipFeed',
83 array('profile' => $this->_profile->id));
85 $feed = new Atom10Feed(true);
87 $feed->addNamespace('activity',
88 'http://activitystrea.ms/spec/1.0/');
90 $feed->addNamespace('poco',
91 'http://portablecontacts.net/spec/1.0');
93 $feed->addNamespace('media',
94 'http://purl.org/syndication/atommedia');
98 $feed->setUpdated('now');
100 $feed->addAuthor($this->_profile->getBestName(),
101 $this->_profile->getURI());
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()));
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')));
114 $feed->addLink(common_local_url('usergroups',
116 $this->_profile->nickname)));
119 array('rel' => 'self',
120 'type' => 'application/atom+xml'));
122 // If there's more...
124 if ($this->page > 1) {
126 array('rel' => 'first',
127 'type' => 'application/atom+xml'));
129 $feed->addLink(common_local_url('AtomPubMembershipFeed',
131 $this->_profile->id),
134 array('rel' => 'prev',
135 'type' => 'application/atom+xml'));
138 if ($this->_memberships->N > $this->count) {
140 $feed->addLink(common_local_url('AtomPubMembershipFeed',
142 $this->_profile->id),
145 array('rel' => 'next',
146 'type' => 'application/atom+xml'));
151 while ($this->_memberships->fetch()) {
153 // We get one more than needed; skip that one
157 if ($i > $this->count) {
161 $act = $this->_memberships->asActivity();
162 $feed->addEntryRaw($act->asString(false, false, false));
165 $this->raw($feed->getString());
173 function addMembership()
175 // XXX: Refactor this; all the same for atompub
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);
184 $xml = file_get_contents('php://input');
186 $dom = DOMDocument::loadXML($xml);
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.'));
195 $activity = new Activity($dom->documentElement);
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.'));
205 $groupObj = $activity->objects[0];
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.'));
212 $group = User_group::getKV('uri', $groupObj->id);
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.'));
220 $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
221 'group_id' => $group->id));
224 // TRANS: Client exception thrown when trying to subscribe to an already subscribed group.
225 throw new ClientException(_('Already a member.'));
228 $profile = $this->auth_user->getProfile();
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.'));
236 $this->auth_user->joinGroup($group);
238 Event::handle('EndAtomPubNewActivity', array($activity, $membership));
241 if (!empty($membership)) {
242 $act = $membership->asActivity();
244 header('Content-Type: application/atom+xml; charset=utf-8');
245 header('Content-Location: ' . $act->selfLink);
248 $this->raw($act->asString(true, true, true));
254 * Return last modified, if applicable.
258 * @return string last modified http header
260 function lastModified()
262 // For comparison with If-Last-Modified
263 // If not applicable, return null
268 * Return etag, if applicable.
272 * @return string etag http header