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.
38 * Feed of ActivityStreams 'favorite' actions
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2010 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class AtompubfavoritefeedAction extends ApiAuthAction
49 private $_profile = null;
50 private $_faves = null;
53 * For initializing members of the class.
55 * @param array $argarray misc. arguments
57 * @return boolean true
59 function prepare($argarray)
61 parent::prepare($argarray);
63 $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
65 if (empty($this->_profile)) {
66 // TRANS: Client exception thrown when requesting a favorite feed for a non-existing profile.
67 throw new ClientException(_('No such profile.'), 404);
70 $offset = ($this->page-1) * $this->count;
71 $limit = $this->count + 1;
73 $this->_faves = Fave::byProfile($this->_profile->id,
83 * @param array $argarray is ignored since it's now passed in in prepare()
87 function handle($argarray=null)
89 parent::handle($argarray);
91 switch ($_SERVER['REQUEST_METHOD']) {
100 // TRANS: Client exception thrown when using an unsupported HTTP method.
101 throw new ClientException(_('HTTP method not supported.'), 405);
109 * Show a feed of favorite activity streams objects
115 header('Content-Type: application/atom+xml; charset=utf-8');
117 $url = common_local_url('AtomPubFavoriteFeed',
118 array('profile' => $this->_profile->id));
120 $feed = new Atom10Feed(true);
122 $feed->addNamespace('activity',
123 'http://activitystrea.ms/spec/1.0/');
125 $feed->addNamespace('poco',
126 'http://portablecontacts.net/spec/1.0');
128 $feed->addNamespace('media',
129 'http://purl.org/syndication/atommedia');
133 $feed->setUpdated('now');
135 $feed->addAuthor($this->_profile->getBestName(),
136 $this->_profile->getURI());
138 // TRANS: Title for Atom favorites feed.
139 // TRANS: %s is a user nickname.
140 $feed->setTitle(sprintf(_("%s favorites"),
141 $this->_profile->getBestName()));
143 // TRANS: Subtitle for Atom favorites feed.
144 // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename.
145 $feed->setSubtitle(sprintf(_('Notices %1$s has favorited on %2$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());
208 function addFavorite()
210 // XXX: Refactor this; all the same for atompub
212 if (empty($this->auth_user) ||
213 $this->auth_user->id != $this->_profile->id) {
214 // TRANS: Client exception thrown when trying to set a favorite for another user.
215 throw new ClientException(_("Cannot 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 exception thrown when trying use an incorrect activity verb for the Atom pub method.
238 throw new ClientException(_('Can only handle favorite activities.'));
242 $note = $activity->objects[0];
244 if (!in_array($note->type, array(ActivityObject::NOTE,
245 ActivityObject::BLOGENTRY,
246 ActivityObject::STATUS))) {
247 // TRANS: Client exception thrown when trying favorite an object that is not a notice.
248 throw new ClientException(_('Can only fave notices.'));
252 $notice = Notice::getKV('uri', $note->id);
254 if (empty($notice)) {
255 // XXX: import from listed URL or something
256 // TRANS: Client exception thrown when trying favorite a notice without content.
257 throw new ClientException(_('Unknown notice.'));
260 $old = Fave::pkeyGet(array('user_id' => $this->auth_user->id,
261 'notice_id' => $notice->id));
264 // TRANS: Client exception thrown when trying favorite an already favorited notice.
265 throw new ClientException(_('Already a favorite.'));
268 $profile = $this->auth_user->getProfile();
270 $fave = Fave::addNew($profile, $notice);
273 $this->_profile->blowFavesCache();
274 $this->notify($fave, $notice, $this->auth_user);
277 Event::handle('EndAtomPubNewActivity', array($activity, $fave));
281 $act = $fave->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.
297 * @param array $args other arguments
299 * @return boolean is read only action?
301 function isReadOnly($args)
303 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
304 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
312 * Return last modified, if applicable.
316 * @return string last modified http header
318 function lastModified()
320 // For comparison with If-Last-Modified
321 // If not applicable, return null
326 * Return etag, if applicable.
330 * @return string etag http header
338 * Does this require authentication?
340 * @return boolean true if delete, else false
342 function requiresAuth()
344 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
345 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
353 * Notify the author of the favorite that the user likes their notice
355 * @param Favorite $fave the favorite in question
356 * @param Notice $notice the notice that's been faved
357 * @param User $user the user doing the favoriting
361 function notify($fave, $notice, $user)
363 $other = User::getKV('id', $notice->profile_id);
364 if ($other && $other->id != $user->id) {
365 if ($other->email && $other->emailnotifyfav) {
366 mail_notify_fave($other, $user, $notice);
369 // XXX: notify by SMS