3 * StatusNet, the distributed open-source microblogging tool
5 * Show the home timeline
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 Jeffery To <jeffery.to@gmail.com>
27 * @author mac65 <mac65@mac65.com>
28 * @author Mike Cochrane <mikec@mikenz.geek.nz>
29 * @author Robin Millette <robin@millette.info>
30 * @author Zach Copley <zach@status.net>
31 * @copyright 2009 StatusNet, Inc.
32 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34 * @link http://status.net/
37 if (!defined('STATUSNET')) {
41 require_once INSTALLDIR . '/lib/apibareauth.php';
44 * Returns the most recent notices (default 20) posted by the target user.
45 * This is the equivalent of 'You and friends' page accessed via Web.
49 * @author Craig Andrews <candrews@integralblue.com>
50 * @author Evan Prodromou <evan@status.net>
51 * @author Jeffery To <jeffery.to@gmail.com>
52 * @author mac65 <mac65@mac65.com>
53 * @author Mike Cochrane <mikec@mikenz.geek.nz>
54 * @author Robin Millette <robin@millette.info>
55 * @author Zach Copley <zach@status.net>
56 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57 * @link http://status.net/
59 class ApiTimelineHomeAction extends ApiBareAuthAction
64 * Take arguments for running
66 * @param array $args $_REQUEST args
68 * @return boolean success flag
70 function prepare($args)
72 parent::prepare($args);
74 $this->user = $this->getTargetUser($this->arg('id'));
76 if (empty($this->user)) {
77 // TRANS: Client error displayed when requesting most recent dents by user and friends for a non-existing user.
78 $this->clientError(_('No such user.'), 404, $this->format);
82 $this->notices = $this->getNotices();
90 * Just show the notices
92 * @param array $args $_REQUEST data (unused)
96 function handle($args)
98 parent::handle($args);
99 $this->showTimeline();
103 * Show the timeline of notices
107 function showTimeline()
109 $profile = $this->user->getProfile();
110 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
111 $sitename = common_config('site', 'name');
112 // TRANS: Timeline title for user and friends. %s is a user nickname.
113 $title = sprintf(_("%s and friends"), $this->user->nickname);
114 $taguribase = TagURI::base();
115 $id = "tag:$taguribase:HomeTimeline:" . $this->user->id;
118 // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
119 _('Updates from %1$s and friends on %2$s!'),
120 $this->user->nickname, $sitename
123 $link = common_local_url(
125 array('nickname' => $this->user->nickname)
128 $self = $this->getSelfUri();
130 $logo = (!empty($avatar))
131 ? $avatar->displayUrl()
132 : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
134 switch($this->format) {
136 $this->showXmlTimeline($this->notices);
139 $this->showRssTimeline(
151 header('Content-Type: application/atom+xml; charset=utf-8');
153 $atom = new AtomNoticeFeed($this->auth_user);
156 $atom->setTitle($title);
157 $atom->setSubtitle($subtitle);
158 $atom->setLogo($logo);
159 $atom->setUpdated('now');
161 $atom->addLink($link);
162 $atom->setSelfLink($self);
164 $atom->addEntryFromNotices($this->notices);
165 $this->raw($atom->getString());
169 $this->showJsonTimeline($this->notices);
172 header('Content-Type: application/json; charset=utf-8');
173 $doc = new ActivityStreamJSONDocument($this->auth_user);
174 $doc->setTitle($title);
175 $doc->addLink($link, 'alternate', 'text/html');
176 $doc->addItemsFromNotices($this->notices);
177 $this->raw($doc->asString());
180 // TRANS: Client error displayed when coming across a non-supported API method.
181 $this->clientError(_('API method not found.'), $code = 404);
189 * @return array notices
191 function getNotices()
195 if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
196 $notice = $this->user->noticeInbox(
197 ($this->page-1) * $this->count,
198 $this->count, $this->since_id,
202 $notice = $this->user->noticesWithFriends(
203 ($this->page-1) * $this->count,
204 $this->count, $this->since_id,
209 while ($notice->fetch()) {
210 $notices[] = clone($notice);
217 * Is this action read only?
219 * @param array $args other arguments
221 * @return boolean true
223 function isReadOnly($args)
229 * When was this feed last modified?
231 * @return string datestamp of the latest notice in the stream
233 function lastModified()
235 if (!empty($this->notices) && (count($this->notices) > 0)) {
236 return strtotime($this->notices[0]->created);
243 * An entity tag for this stream
245 * Returns an Etag based on the action name, language, user ID, and
246 * timestamps of the first and last notice in the timeline
248 * @return string etag
252 if (!empty($this->notices) && (count($this->notices) > 0)) {
254 $last = count($this->notices) - 1;
256 return '"' . implode(
258 array($this->arg('action'),
259 common_user_cache_hash($this->auth_user),
262 strtotime($this->notices[0]->created),
263 strtotime($this->notices[$last]->created))