3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * AtomPub subscription feed
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.
38 * Subscription feed class for AtomPub
40 * Generates a list of the user's subscriptions
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 AtompubsubscriptionfeedAction extends ApiAuthAction
51 private $_profile = null;
52 private $_subscriptions = 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 $subscriber = $this->trimmed('subscriber');
67 $this->_profile = Profile::getKV('id', $subscriber);
69 if (empty($this->_profile)) {
70 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
71 // TRANS: %d is the non-existing profile ID number.
72 throw new ClientException(sprintf(_('No such profile id: %d.'),
76 // page and count from ApiAction
78 $offset = ($this->page-1) * $this->count;
80 $this->_subscriptions = Subscription::bySubscriber($subscriber,
90 * @param array $argarray is ignored since it's now passed in in prepare()
94 function handle($argarray=null)
96 parent::handle($argarray);
97 switch ($_SERVER['REQUEST_METHOD']) {
103 $this->addSubscription();
106 // TRANS: Client exception thrown when using an unsupported HTTP method.
107 $this->clientError(_('HTTP method not supported.'), 405);
114 * Show the feed of subscriptions
120 header('Content-Type: application/atom+xml; charset=utf-8');
122 $url = common_local_url('AtomPubSubscriptionFeed',
123 array('subscriber' => $this->_profile->id));
125 $feed = new Atom10Feed(true);
127 $feed->addNamespace('activity',
128 'http://activitystrea.ms/spec/1.0/');
130 $feed->addNamespace('poco',
131 'http://portablecontacts.net/spec/1.0');
133 $feed->addNamespace('media',
134 'http://purl.org/syndication/atommedia');
138 $feed->setUpdated('now');
140 $feed->addAuthor($this->_profile->getBestName(),
141 $this->_profile->getURI());
143 // TRANS: Title for Atom subscription feed.
144 // TRANS: %s is a user nickname.
145 $feed->setTitle(sprintf(_("%s subscriptions"),
146 $this->_profile->getBestName()));
148 // TRANS: Subtitle for Atom subscription feed.
149 // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
150 $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
151 $this->_profile->getBestName(),
152 common_config('site', 'name')));
154 $feed->addLink(common_local_url('subscriptions',
156 $this->_profile->nickname)));
159 array('rel' => 'self',
160 'type' => 'application/atom+xml'));
162 // If there's more...
164 if ($this->page > 1) {
166 array('rel' => 'first',
167 'type' => 'application/atom+xml'));
169 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
170 array('subscriber' =>
171 $this->_profile->id),
174 array('rel' => 'prev',
175 'type' => 'application/atom+xml'));
178 if ($this->_subscriptions->N > $this->count) {
180 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
181 array('subscriber' =>
182 $this->_profile->id),
185 array('rel' => 'next',
186 'type' => 'application/atom+xml'));
191 // XXX: This is kind of inefficient
193 while ($this->_subscriptions->fetch()) {
195 // We get one more than needed; skip that one
199 if ($i > $this->count) {
203 $act = $this->_subscriptions->asActivity();
204 $feed->addEntryRaw($act->asString(false, false, false));
207 $this->raw($feed->getString());
211 * Add a new subscription
213 * Handling the POST method for AtomPub
217 function addSubscription()
219 if (empty($this->auth_user) ||
220 $this->auth_user->id != $this->_profile->id) {
221 // TRANS: Client exception thrown when trying to subscribe another user.
222 throw new ClientException(_("Cannot add someone else's".
223 " subscription."), 403);
226 $xml = file_get_contents('php://input');
228 $dom = DOMDocument::loadXML($xml);
230 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
231 $dom->documentElement->localName != 'entry') {
232 // TRANS: Client error displayed when not using an Atom entry.
233 $this->clientError(_('Atom post must be an Atom entry.'));
236 $activity = new Activity($dom->documentElement);
240 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
242 if ($activity->verb != ActivityVerb::FOLLOW) {
243 // TRANS: Client error displayed when not using the follow verb.
244 $this->clientError(_('Can only handle Follow activities.'));
247 $person = $activity->objects[0];
249 if ($person->type != ActivityObject::PERSON) {
250 // TRANS: Client exception thrown when subscribing to an object that is not a person.
251 $this->clientError(_('Can only follow people.'));
254 // XXX: OStatus discovery (maybe)
256 $profile = Profile::fromUri($person->id);
257 } catch (UnknownUriException $e) {
258 // TRANS: Client exception thrown when subscribing to a non-existing profile.
259 // TRANS: %s is the unknown profile ID.
260 $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
263 if (Subscription::exists($this->_profile, $profile)) {
265 // TRANS: Client error displayed trying to subscribe to an already subscribed profile.
266 // TRANS: %s is the profile the user already has a subscription on.
267 $this->clientError(sprintf(_('Already subscribed to %s.'),
272 if (Subscription::start($this->_profile, $profile)) {
273 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
274 'subscribed' => $profile->id));
277 Event::handle('EndAtomPubNewActivity', array($activity, $sub));
281 $act = $sub->asActivity();
283 header('Content-Type: application/atom+xml; charset=utf-8');
284 header('Content-Location: ' . $act->selfLink);
287 $this->raw($act->asString(true, true, true));
293 * Return true if read only.
295 * @param array $args other arguments
297 * @return boolean is read only action?
299 function isReadOnly($args)
301 return $_SERVER['REQUEST_METHOD'] != 'POST';
305 * Return last modified, if applicable.
307 * @return string last modified http header
309 function lastModified()
315 * Return etag, if applicable.
317 * @return string etag http header
325 * Does this require authentication?
327 * @return boolean true if delete, else false
329 function requiresAuth()
331 if ($_SERVER['REQUEST_METHOD'] == 'POST') {