3 * StatusNet, the distributed open-source microblogging tool
5 * Show a user's favorite notices
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Evan Prodromou <evan@status.net>
26 * @author Zach Copley <zach@status.net>
27 * @copyright 2009-2010 StatusNet, Inc.
28 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
37 require_once INSTALLDIR.'/lib/apibareauth.php';
40 * Returns the 20 most recent favorite notices for the authenticating user or user
41 * specified by the ID parameter in the requested format.
45 * @author Craig Andrews <candrews@integralblue.com>
46 * @author Evan Prodromou <evan@status.net>
47 * @author Zach Copley <zach@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
51 class ApiTimelineFavoritesAction extends ApiBareAuthAction
56 * Take arguments for running
58 * @param array $args $_REQUEST args
60 * @return boolean success flag
62 function prepare($args)
64 parent::prepare($args);
66 $this->user = $this->getTargetUser($this->arg('id'));
68 if (empty($this->user)) {
69 // TRANS: Client error displayed when requesting most recent favourite notices by a user for a non-existing user.
70 $this->clientError(_('No such user.'), 404, $this->format);
74 $this->notices = $this->getNotices();
82 * Just show the notices
84 * @param array $args $_REQUEST data (unused)
88 function handle($args)
90 parent::handle($args);
91 $this->showTimeline();
95 * Show the timeline of notices
99 function showTimeline()
101 $profile = $this->user->getProfile();
102 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
104 $sitename = common_config('site', 'name');
106 // TRANS: Title for timeline of most recent favourite notices by a user.
107 // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
108 _('%1$s / Favorites from %2$s'),
110 $this->user->nickname
113 $taguribase = TagURI::base();
114 $id = "tag:$taguribase:Favorites:" . $this->user->id;
117 // TRANS: Subtitle for timeline of most recent favourite notices by a user.
118 // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
119 // TRANS: %3$s is a user nickname.
120 _('%1$s updates favorited by %2$s / %3$s.'),
122 $profile->getBestName(),
123 $this->user->nickname
125 $logo = !empty($avatar)
126 ? $avatar->displayUrl()
127 : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
129 $link = common_local_url(
131 array('nickname' => $this->user->nickname)
134 $self = $this->getSelfUri();
136 switch($this->format) {
138 $this->showXmlTimeline($this->notices);
141 $this->showRssTimeline(
152 header('Content-Type: application/atom+xml; charset=utf-8');
154 $atom = new AtomNoticeFeed($this->auth_user);
157 $atom->setTitle($title);
158 $atom->setSubtitle($subtitle);
159 $atom->setLogo($logo);
160 $atom->setUpdated('now');
162 $atom->addLink($link);
163 $atom->setSelfLink($self);
165 $atom->addEntryFromNotices($this->notices);
167 $this->raw($atom->getString());
170 $this->showJsonTimeline($this->notices);
173 header('Content-Type: application/json; charset=utf-8');
174 $doc = new ActivityStreamJSONDocument($this->auth_user);
175 $doc->setTitle($title);
176 $doc->addLink($link,'alternate', 'text/html');
177 $doc->addItemsFromNotices($this->notices);
178 $this->raw($doc->asString());
181 // TRANS: Client error displayed when trying to handle an unknown API method.
182 $this->clientError(_('API method not found.'), $code = 404);
190 * @return array notices
192 function getNotices()
196 common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
198 if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
199 $notice = $this->user->favoriteNotices(
201 ($this->page-1) * $this->count,
207 $notice = $this->user->favoriteNotices(
209 ($this->page-1) * $this->count,
216 while ($notice->fetch()) {
217 $notices[] = clone($notice);
224 * Is this action read only?
226 * @param array $args other arguments
228 * @return boolean true
230 function isReadOnly($args)
236 * When was this feed last modified?
238 * @return string datestamp of the latest notice in the stream
240 function lastModified()
242 if (!empty($this->notices) && (count($this->notices) > 0)) {
243 return strtotime($this->notices[0]->created);
250 * An entity tag for this stream
252 * Returns an Etag based on the action name, language, user ID, and
253 * timestamps of the first and last notice in the timeline
255 * @return string etag
259 if (!empty($this->notices) && (count($this->notices) > 0)) {
261 $last = count($this->notices) - 1;
263 return '"' . implode(
265 array($this->arg('action'),
266 common_user_cache_hash($this->auth_user),
269 strtotime($this->notices[0]->created),
270 strtotime($this->notices[$last]->created))