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/
49 class AtompubmembershipfeedAction extends ApiAuthAction
51 private $_profile = null;
52 private $_memberships = null;
55 * For initializing members of the class.
57 * @param array $argarray misc. arguments
59 * @return boolean true
61 function prepare($argarray)
63 parent::prepare($argarray);
65 $profileId = $this->trimmed('profile');
67 $this->_profile = Profile::staticGet('id', $profileId);
69 if (empty($this->_profile)) {
70 // TRANS: Client exception.
71 throw new ClientException(_('No such profile.'), 404);
74 $offset = ($this->page-1) * $this->count;
75 $limit = $this->count + 1;
77 $this->_memberships = Group_member::byMember($this->_profile->id,
87 * @param array $argarray is ignored since it's now passed in in prepare()
91 function handle($argarray=null)
93 parent::handle($argarray);
95 switch ($_SERVER['REQUEST_METHOD']) {
101 $this->addMembership();
104 // TRANS: Client exception thrown when using an unsupported HTTP method.
105 throw new ClientException(_('HTTP method not supported.'), 405);
113 * Show a feed of favorite activity streams objects
119 header('Content-Type: application/atom+xml; charset=utf-8');
121 $url = common_local_url('AtomPubMembershipFeed',
122 array('profile' => $this->_profile->id));
124 $feed = new Atom10Feed(true);
126 $feed->addNamespace('activity',
127 'http://activitystrea.ms/spec/1.0/');
129 $feed->addNamespace('poco',
130 'http://portablecontacts.net/spec/1.0');
132 $feed->addNamespace('media',
133 'http://purl.org/syndication/atommedia');
137 $feed->setUpdated('now');
139 $feed->addAuthor($this->_profile->getBestName(),
140 $this->_profile->getURI());
142 // TRANS: Title for group membership feed.
143 // TRANS: %s is a username.
144 $feed->setTitle(sprintf(_("%s group memberships"),
145 $this->_profile->getBestName()));
147 // TRANS: Subtitle for group membership feed.
148 // TRANS: %1$s is a username, %2$s is the StatusNet sitename.
149 $feed->setSubtitle(sprintf(_('Groups %1$s is a member of on %2$s'),
150 $this->_profile->getBestName(),
151 common_config('site', 'name')));
153 $feed->addLink(common_local_url('usergroups',
155 $this->_profile->nickname)));
158 array('rel' => 'self',
159 'type' => 'application/atom+xml'));
161 // If there's more...
163 if ($this->page > 1) {
165 array('rel' => 'first',
166 'type' => 'application/atom+xml'));
168 $feed->addLink(common_local_url('AtomPubMembershipFeed',
170 $this->_profile->id),
173 array('rel' => 'prev',
174 'type' => 'application/atom+xml'));
177 if ($this->_memberships->N > $this->count) {
179 $feed->addLink(common_local_url('AtomPubMembershipFeed',
181 $this->_profile->id),
184 array('rel' => 'next',
185 'type' => 'application/atom+xml'));
190 while ($this->_memberships->fetch()) {
192 // We get one more than needed; skip that one
196 if ($i > $this->count) {
200 $act = $this->_memberships->asActivity();
201 $feed->addEntryRaw($act->asString(false, false, false));
204 $this->raw($feed->getString());
212 function addMembership()
214 // XXX: Refactor this; all the same for atompub
216 if (empty($this->auth_user) ||
217 $this->auth_user->id != $this->_profile->id) {
218 // TRANS: Client exception thrown when trying subscribe someone else to a group.
219 throw new ClientException(_("Cannot add someone else's".
220 " membership."), 403);
223 $xml = file_get_contents('php://input');
225 $dom = DOMDocument::loadXML($xml);
227 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
228 $dom->documentElement->localName != 'entry') {
229 // TRANS: Client error displayed when not using an Atom entry.
230 throw new ClientException(_('Atom post must be an Atom entry.'));
234 $activity = new Activity($dom->documentElement);
238 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
239 if ($activity->verb != ActivityVerb::JOIN) {
240 // TRANS: Client error displayed when not using the POST verb.
241 // TRANS: Do not translate POST.
242 throw new ClientException(_('Can only handle join activities.'));
246 $groupObj = $activity->objects[0];
248 if ($groupObj->type != ActivityObject::GROUP) {
249 // TRANS: Client exception thrown when trying favorite an object that is not a notice.
250 throw new ClientException(_('Can only fave notices.'));
254 $group = User_group::staticGet('uri', $groupObj->id);
257 // XXX: import from listed URL or something
258 // TRANS: Client exception thrown when trying to subscribe to a non-existing group.
259 throw new ClientException(_('Unknown group.'));
262 $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
263 'group_id' => $group->id));
266 // TRANS: Client exception thrown when trying to subscribe to an already subscribed group.
267 throw new ClientException(_('Already a member.'));
270 $profile = $this->auth_user->getProfile();
272 if (Group_block::isBlocked($group, $profile)) {
273 // XXX: import from listed URL or something
274 // TRANS: Client exception thrown when trying to subscribe to group while blocked from that group.
275 throw new ClientException(_('Blocked by admin.'));
278 if (Event::handle('StartJoinGroup', array($group, $this->auth_user))) {
279 $membership = Group_member::join($group->id, $this->auth_user->id);
280 Event::handle('EndJoinGroup', array($group, $this->auth_user));
283 Event::handle('EndAtomPubNewActivity', array($activity, $membership));
286 if (!empty($membership)) {
287 $act = $membership->asActivity();
289 header('Content-Type: application/atom+xml; charset=utf-8');
290 header('Content-Location: ' . $act->selfLink);
293 $this->raw($act->asString(true, true, true));
299 * Return true if read only.
303 * @param array $args other arguments
305 * @return boolean is read only action?
307 function isReadOnly($args)
309 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
310 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
318 * Return last modified, if applicable.
322 * @return string last modified http header
324 function lastModified()
326 // For comparison with If-Last-Modified
327 // If not applicable, return null
332 * Return etag, if applicable.
336 * @return string etag http header
344 * Does this require authentication?
346 * @return boolean true if delete, else false
348 function requiresAuth()
350 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
351 $_SERVER['REQUEST_METHOD'] == 'HEAD') {