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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * Subscription feed class for AtomPub
36 * Generates a list of the user's subscriptions
40 * @author Evan Prodromou <evan@status.net>
41 * @copyright 2010 StatusNet, Inc.
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43 * @link http://status.net/
45 class AtompubsubscriptionfeedAction extends AtompubAction
47 private $_profile = null;
48 private $_subscriptions = null;
50 protected function atompubPrepare()
52 $subscriber = $this->trimmed('subscriber');
54 $this->_profile = Profile::getKV('id', $subscriber);
56 if (!$this->_profile instanceof Profile) {
57 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
58 // TRANS: %d is the non-existing profile ID number.
59 throw new ClientException(sprintf(_('No such profile id: %d.'),
63 $this->_subscriptions = Subscription::bySubscriber($this->_profile->id,
70 protected function handleGet()
75 protected function handlePost()
77 $this->addSubscription();
81 * Show the feed of subscriptions
87 header('Content-Type: application/atom+xml; charset=utf-8');
89 $url = common_local_url('AtomPubSubscriptionFeed',
90 array('subscriber' => $this->_profile->id));
92 $feed = new Atom10Feed(true);
94 $feed->addNamespace('activity',
95 'http://activitystrea.ms/spec/1.0/');
97 $feed->addNamespace('poco',
98 'http://portablecontacts.net/spec/1.0');
100 $feed->addNamespace('media',
101 'http://purl.org/syndication/atommedia');
103 $feed->addNamespace('georss',
104 'http://www.georss.org/georss');
108 $feed->setUpdated('now');
110 $feed->addAuthor($this->_profile->getBestName(),
111 $this->_profile->getURI());
113 // TRANS: Title for Atom subscription feed.
114 // TRANS: %s is a user nickname.
115 $feed->setTitle(sprintf(_("%s subscriptions"),
116 $this->_profile->getBestName()));
118 // TRANS: Subtitle for Atom subscription feed.
119 // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
120 $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
121 $this->_profile->getBestName(),
122 common_config('site', 'name')));
124 $feed->addLink(common_local_url('subscriptions',
126 $this->_profile->nickname)));
129 array('rel' => 'self',
130 'type' => 'application/atom+xml'));
132 // If there's more...
134 if ($this->page > 1) {
136 array('rel' => 'first',
137 'type' => 'application/atom+xml'));
139 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
140 array('subscriber' =>
141 $this->_profile->id),
144 array('rel' => 'prev',
145 'type' => 'application/atom+xml'));
148 if ($this->_subscriptions->N > $this->count) {
150 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
151 array('subscriber' =>
152 $this->_profile->id),
155 array('rel' => 'next',
156 'type' => 'application/atom+xml'));
161 // XXX: This is kind of inefficient
163 while ($this->_subscriptions->fetch()) {
165 // We get one more than needed; skip that one
169 if ($i > $this->count) {
173 $act = $this->_subscriptions->asActivity();
174 $feed->addEntryRaw($act->asString(false, false, false));
177 $this->raw($feed->getString());
181 * Add a new subscription
183 * Handling the POST method for AtomPub
187 function addSubscription()
189 if (empty($this->auth_user) ||
190 $this->auth_user->id != $this->_profile->id) {
191 // TRANS: Client exception thrown when trying to subscribe another user.
192 throw new ClientException(_("Cannot add someone else's".
193 " subscription."), 403);
196 $xml = file_get_contents('php://input');
198 $dom = DOMDocument::loadXML($xml);
200 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
201 $dom->documentElement->localName != 'entry') {
202 // TRANS: Client error displayed when not using an Atom entry.
203 $this->clientError(_('Atom post must be an Atom entry.'));
206 $activity = new Activity($dom->documentElement);
210 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
212 if ($activity->verb != ActivityVerb::FOLLOW) {
213 // TRANS: Client error displayed when not using the follow verb.
214 $this->clientError(_('Can only handle Follow activities.'));
217 $person = $activity->objects[0];
219 if ($person->type != ActivityObject::PERSON) {
220 // TRANS: Client exception thrown when subscribing to an object that is not a person.
221 $this->clientError(_('Can only follow people.'));
224 // XXX: OStatus discovery (maybe)
226 $profile = Profile::fromUri($person->id);
227 } catch (UnknownUriException $e) {
228 // TRANS: Client exception thrown when subscribing to a non-existing profile.
229 // TRANS: %s is the unknown profile ID.
230 $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
234 $sub = Subscription::start($this->_profile, $profile);
235 } catch (AlreadyFulfilledException $e) {
237 $this->clientError($e->getMessage(), 409);
240 Event::handle('EndAtomPubNewActivity', array($activity, $sub));
244 $act = $sub->asActivity();
246 header('Content-Type: application/atom+xml; charset=utf-8');
247 header('Content-Location: ' . $act->selfLink);
250 $this->raw($act->asString(true, true, true));