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('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 ActivityStreams 'favorite' actions
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/
50 class AtompubfavoritefeedAction extends ApiAuthAction
52 private $_profile = null;
53 private $_faves = null;
56 * For initializing members of the class.
58 * @param array $argarray misc. arguments
60 * @return boolean true
63 function prepare($argarray)
65 parent::prepare($argarray);
67 $this->_profile = Profile::staticGet('id', $this->trimmed('profile'));
69 if (empty($this->_profile)) {
70 throw new ClientException(_('No such profile'), 404);
73 $offset = ($this->page-1) * $this->count;
74 $limit = $this->count + 1;
76 $this->_faves = Fave::byProfile($this->_profile->id,
86 * @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->addFavorite();
104 throw new ClientException(_('HTTP method not supported.'), 405);
112 * Show a feed of favorite activity streams objects
119 header('Content-Type: application/atom+xml; charset=utf-8');
121 $url = common_local_url('AtomPubFavoriteFeed',
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 $feed->setTitle(sprintf(_("%s favorites"),
143 $this->_profile->getBestName()));
145 $feed->setSubtitle(sprintf(_("Notices %s has favorited to on %s"),
146 $this->_profile->getBestName(),
147 common_config('site', 'name')));
149 $feed->addLink(common_local_url('showfavorites',
151 $this->_profile->nickname)));
154 array('rel' => 'self',
155 'type' => 'application/atom+xml'));
157 // If there's more...
159 if ($this->page > 1) {
161 array('rel' => 'first',
162 'type' => 'application/atom+xml'));
164 $feed->addLink(common_local_url('AtomPubFavoriteFeed',
166 $this->_profile->id),
169 array('rel' => 'prev',
170 'type' => 'application/atom+xml'));
173 if ($this->_faves->N > $this->count) {
175 $feed->addLink(common_local_url('AtomPubFavoriteFeed',
177 $this->_profile->id),
180 array('rel' => 'next',
181 'type' => 'application/atom+xml'));
186 while ($this->_faves->fetch()) {
188 // We get one more than needed; skip that one
192 if ($i > $this->count) {
196 $act = $this->_faves->asActivity();
197 $feed->addEntryRaw($act->asString(false, false, false));
200 $this->raw($feed->getString());
209 function addFavorite()
211 // XXX: Refactor this; all the same for atompub
213 if (empty($this->auth_user) ||
214 $this->auth_user->id != $this->_profile->id) {
215 throw new ClientException(_("Can't add someone else's".
216 " subscription"), 403);
219 $xml = file_get_contents('php://input');
221 $dom = DOMDocument::loadXML($xml);
223 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
224 $dom->documentElement->localName != 'entry') {
225 // TRANS: Client error displayed when not using an Atom entry.
226 throw new ClientException(_('Atom post must be an Atom entry.'));
230 $activity = new Activity($dom->documentElement);
234 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
236 if ($activity->verb != ActivityVerb::FAVORITE) {
237 // TRANS: Client error displayed when not using the POST verb.
238 // TRANS: Do not translate POST.
239 throw new ClientException(_('Can only handle Favorite activities.'));
243 $note = $activity->objects[0];
245 if (!in_array($note->type, array(ActivityObject::NOTE,
246 ActivityObject::BLOGENTRY,
247 ActivityObject::STATUS))) {
248 throw new ClientException(_('Can only fave notices.'));
252 $notice = Notice::staticGet('uri', $note->id);
254 if (empty($notice)) {
255 // XXX: import from listed URL or something
256 throw new ClientException(_('Unknown note.'));
259 $old = Fave::pkeyGet(array('user_id' => $this->auth_user->id,
260 'notice_id' => $notice->id));
263 throw new ClientException(_('Already a favorite.'));
266 $profile = $this->auth_user->getProfile();
268 $fave = Fave::addNew($profile, $notice);
271 $this->_profile->blowFavesCache();
272 $this->notify($fave, $notice, $this->auth_user);
275 Event::handle('EndAtomPubNewActivity', array($activity, $fave));
279 $act = $fave->asActivity();
281 header('Content-Type: application/atom+xml; charset=utf-8');
282 header('Content-Location: ' . $act->selfLink);
285 $this->raw($act->asString(true, true, true));
291 * Return true if read only.
295 * @param array $args other arguments
297 * @return boolean is read only action?
300 function isReadOnly($args)
302 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
303 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
311 * Return last modified, if applicable.
315 * @return string last modified http header
317 function lastModified()
319 // For comparison with If-Last-Modified
320 // If not applicable, return null
325 * Return etag, if applicable.
329 * @return string etag http header
338 * Does this require authentication?
340 * @return boolean true if delete, else false
343 function requiresAuth()
345 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
346 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
354 * Notify the author of the favorite that the user likes their notice
356 * @param Favorite $fave the favorite in question
357 * @param Notice $notice the notice that's been faved
358 * @param User $user the user doing the favoriting
363 function notify($fave, $notice, $user)
365 $other = User::staticGet('id', $notice->profile_id);
366 if ($other && $other->id != $user->id) {
367 if ($other->email && $other->emailnotifyfav) {
368 mail_notify_fave($other, $user, $notice);
371 // XXX: notify by SMS