3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Feed of ActivityStreams 'favorite' actions
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 ActivityStreams 'favorite' actions
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 AtompubfavoritefeedAction extends ApiAuthAction
45 private $_profile = null;
46 private $_faves = null;
48 protected function prepare(array $args=array())
50 parent::prepare($args);
52 $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
54 if (!$this->_profile instanceof Profile) {
55 // TRANS: Client exception thrown when requesting a favorite feed for a non-existing profile.
56 throw new ClientException(_('No such profile.'), 404);
59 $offset = ($this->page-1) * $this->count;
60 $limit = $this->count + 1;
62 $this->_faves = Fave::byProfile($this->_profile->id,
69 protected function handle()
73 switch ($_SERVER['REQUEST_METHOD']) {
82 // TRANS: Client exception thrown when using an unsupported HTTP method.
83 throw new ClientException(_('HTTP method not supported.'), 405);
90 * Show a feed of favorite activity streams objects
96 header('Content-Type: application/atom+xml; charset=utf-8');
98 $url = common_local_url('AtomPubFavoriteFeed',
99 array('profile' => $this->_profile->id));
101 $feed = new Atom10Feed(true);
103 $feed->addNamespace('activity',
104 'http://activitystrea.ms/spec/1.0/');
106 $feed->addNamespace('poco',
107 'http://portablecontacts.net/spec/1.0');
109 $feed->addNamespace('media',
110 'http://purl.org/syndication/atommedia');
114 $feed->setUpdated('now');
116 $feed->addAuthor($this->_profile->getBestName(),
117 $this->_profile->getUri());
119 // TRANS: Title for Atom favorites feed.
120 // TRANS: %s is a user nickname.
121 $feed->setTitle(sprintf(_("%s favorites"),
122 $this->_profile->getBestName()));
124 // TRANS: Subtitle for Atom favorites feed.
125 // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename.
126 $feed->setSubtitle(sprintf(_('Notices %1$s has favorited on %2$s'),
127 $this->_profile->getBestName(),
128 common_config('site', 'name')));
130 $feed->addLink(common_local_url('showfavorites',
132 $this->_profile->nickname)));
135 array('rel' => 'self',
136 'type' => 'application/atom+xml'));
138 // If there's more...
140 if ($this->page > 1) {
142 array('rel' => 'first',
143 'type' => 'application/atom+xml'));
145 $feed->addLink(common_local_url('AtomPubFavoriteFeed',
147 $this->_profile->id),
150 array('rel' => 'prev',
151 'type' => 'application/atom+xml'));
154 if ($this->_faves->N > $this->count) {
156 $feed->addLink(common_local_url('AtomPubFavoriteFeed',
158 $this->_profile->id),
161 array('rel' => 'next',
162 'type' => 'application/atom+xml'));
167 while ($this->_faves->fetch()) {
169 // We get one more than needed; skip that one
173 if ($i > $this->count) {
177 $act = $this->_faves->asActivity();
178 $feed->addEntryRaw($act->asString(false, false, false));
181 $this->raw($feed->getString());
189 function addFavorite()
191 // XXX: Refactor this; all the same for atompub
193 if (!$this->scoped instanceof Profile ||
194 $this->scoped->id != $this->_profile->id) {
195 // TRANS: Client exception thrown when trying to set a favorite for another user.
196 throw new ClientException(_("Cannot add someone else's subscription."), 403);
199 $xml = file_get_contents('php://input');
201 $dom = DOMDocument::loadXML($xml);
203 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
204 $dom->documentElement->localName != 'entry') {
205 // TRANS: Client error displayed when not using an Atom entry.
206 throw new ClientException(_('Atom post must be an Atom entry.'));
209 $activity = new Activity($dom->documentElement);
212 // Favorite plugin handles these as ActivityHandlerPlugin through Notice->saveActivity
213 // which in turn uses "StoreActivityObject" event.
214 Event::handle('StartAtomPubNewActivity', array(&$activity, $this->scoped, &$notice));
215 assert($notice instanceof Notice);
217 $act = $notice->asActivity();
219 header('Content-Type: application/atom+xml; charset=utf-8');
220 header('Content-Location: ' . $act->selfLink);
223 $this->raw($act->asString(true, true, true));
228 * Return true if read only.
232 * @param array $args other arguments
234 * @return boolean is read only action?
236 function isReadOnly($args)
238 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
239 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
247 * Return last modified, if applicable.
251 * @return string last modified http header
253 function lastModified()
255 // For comparison with If-Last-Modified
256 // If not applicable, return null
261 * Return etag, if applicable.
265 * @return string etag http header
273 * Does this require authentication?
275 * @return boolean true if delete, else false
277 function requiresAuth()
279 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
280 $_SERVER['REQUEST_METHOD'] == 'HEAD') {