3 * StatusNet, the distributed open-source microblogging tool
5 * Show a user's 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('GNUSOCIAL')) {
42 * Returns the most recent notices (default 20) posted by the authenticating
43 * user. Another user's timeline can be requested via the id parameter. This
44 * is the API equivalent of the user profile web page.
48 * @author Craig Andrews <candrews@integralblue.com>
49 * @author Evan Prodromou <evan@status.net>
50 * @author Jeffery To <jeffery.to@gmail.com>
51 * @author mac65 <mac65@mac65.com>
52 * @author Mike Cochrane <mikec@mikenz.geek.nz>
53 * @author Robin Millette <robin@millette.info>
54 * @author Zach Copley <zach@status.net>
55 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
56 * @link http://status.net/
58 class ApiTimelineUserAction extends ApiBareAuthAction
60 public $notices = null;
62 public $next_id = null;
65 * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
67 * @param array $args other arguments
69 * @return boolean true
72 public function isReadOnly($args)
74 return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
78 * When was this feed last modified?
80 * @return string datestamp of the latest notice in the stream
82 public function lastModified()
84 if (!empty($this->notices) && (count($this->notices) > 0)) {
85 return strtotime($this->notices[0]->created);
92 * An entity tag for this stream
94 * Returns an Etag based on the action name, language, user ID, and
95 * timestamps of the first and last notice in the timeline
99 public function etag()
101 if (!empty($this->notices) && (count($this->notices) > 0)) {
102 $last = count($this->notices) - 1;
104 return '"' . implode(
106 array($this->arg('action'),
107 common_user_cache_hash($this->scoped),
109 $this->target->getID(),
110 strtotime($this->notices[0]->created),
111 strtotime($this->notices[$last]->created))
120 * Take arguments for running
122 * @param array $args $_REQUEST args
124 * @return boolean success flag
125 * @throws AuthorizationException
126 * @throws ClientException
128 protected function prepare(array $args = [])
130 parent::prepare($args);
132 $this->target = $this->getTargetProfile($this->arg('id'));
134 if (!($this->target instanceof Profile)) {
135 // TRANS: Client error displayed requesting most recent notices for a non-existing user.
136 $this->clientError(_('No such user.'), 404);
139 if (!$this->target->isLocal()) {
140 $this->serverError(_('Remote user timelines are not available here yet.'), 501);
143 $this->notices = $this->getNotices();
151 * @return array notices
153 public function getNotices()
157 $notice = $this->target->getNotices(
158 ($this->page - 1) * $this->count,
165 while ($notice->fetch()) {
166 if (count($notices) < $this->count) {
167 $notices[] = clone($notice);
169 $this->next_id = $notice->id;
180 * Just show the notices
183 * @throws ClientException
184 * @throws ServerException
186 protected function handle()
190 if ($this->isPost()) {
193 $this->showTimeline();
197 public function handlePost()
199 if (!$this->scoped instanceof Profile ||
200 !$this->target->sameAs($this->scoped)) {
201 // TRANS: Client error displayed trying to add a notice to another user's timeline.
202 $this->clientError(_('Only the user can add to their own timeline.'), 403);
205 // Only handle posts for Atom
206 if ($this->format != 'atom') {
207 // TRANS: Client error displayed when using another format than AtomPub.
208 $this->clientError(_('Only accept AtomPub for Atom feeds.'));
211 $xml = trim(file_get_contents('php://input'));
213 // TRANS: Client error displayed attempting to post an empty API notice.
214 $this->clientError(_('Atom post must not be empty.'));
217 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
218 $dom = new DOMDocument();
219 $ok = $dom->loadXML($xml);
220 error_reporting($old);
222 // TRANS: Client error displayed attempting to post an API that is not well-formed XML.
223 $this->clientError(_('Atom post must be well-formed XML.'));
226 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
227 $dom->documentElement->localName != 'entry') {
228 // TRANS: Client error displayed when not using an Atom entry.
229 $this->clientError(_('Atom post must be an Atom entry.'));
232 $activity = new Activity($dom->documentElement);
234 common_debug('AtomPub: Ignoring right now, but this POST was made to collection: ' . $activity->id);
236 // Reset activity data so we can handle it in the same functions as with OStatus
237 // because we don't let clients set their own UUIDs... Not sure what AtomPub thinks
238 // about that though.
239 $activity->id = null;
240 $activity->actor = null; // not used anyway, we use $this->target
241 $activity->objects[0]->id = null;
244 if (Event::handle('StartAtomPubNewActivity', array($activity, $this->target, &$stored))) {
245 // TRANS: Client error displayed when not using the POST verb. Do not translate POST.
246 throw new ClientException(_('Could not handle this Atom Activity.'));
248 if (!$stored instanceof Notice) {
249 throw new ServerException('Server did not create a Notice object from handled AtomPub activity.');
251 Event::handle('EndAtomPubNewActivity', array($activity, $this->target, $stored));
253 header('HTTP/1.1 201 Created');
254 header("Location: " . common_local_url('ApiStatusesShow', array('id' => $stored->getID(),
255 'format' => 'atom')));
256 $this->showSingleAtomStatus($stored);
260 * Show the timeline of notices
263 * @throws ClientException
264 * @throws ServerException
265 * @throws UserNoProfileException
267 public function showTimeline()
269 // We'll use the shared params from the Atom stub
270 // for other feed types.
271 $atom = new AtomUserNoticeFeed($this->target->getUser(), $this->scoped);
273 $link = common_local_url(
275 array('nickname' => $this->target->getNickname())
278 $self = $this->getSelfUri();
280 // FriendFeed's SUP protocol
281 // Also added RSS and Atom feeds
283 $suplink = common_local_url('sup', null, null, $this->target->getID());
284 header('X-SUP-ID: ' . $suplink);
288 $nextUrl = !empty($this->next_id)
291 array('format' => $this->format,
292 'id' => $this->target->getID()),
293 array('max_id' => $this->next_id)
298 if (!empty($this->notices)) {
299 assert($this->notices[0] instanceof Notice);
300 $prevExtra['since_id'] = $this->notices[0]->id;
303 $prevUrl = common_local_url(
305 array('format' => $this->format,
306 'id' => $this->target->getID()),
309 $firstUrl = common_local_url(
311 array('format' => $this->format,
312 'id' => $this->target->getID())
315 switch ($this->format) {
317 $this->showXmlTimeline($this->notices);
320 $this->showRssTimeline(
331 header('Content-Type: application/atom+xml; charset=utf-8');
334 $atom->setSelfLink($self);
336 // Add navigation links: next, prev, first
337 // Note: we use IDs rather than pages for navigation; page boundaries
338 // change too quickly!
340 if (!empty($this->next_id)) {
343 array('rel' => 'next',
344 'type' => 'application/atom+xml')
348 if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
351 array('rel' => 'prev',
352 'type' => 'application/atom+xml')
356 if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
359 array('rel' => 'first',
360 'type' => 'application/atom+xml')
364 $atom->addEntryFromNotices($this->notices);
365 $this->raw($atom->getString());
369 $this->showJsonTimeline($this->notices);
372 header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
373 $doc = new ActivityStreamJSONDocument($this->scoped);
374 $doc->setTitle($atom->title);
375 $doc->addLink($link, 'alternate', 'text/html');
376 $doc->addItemsFromNotices($this->notices);
378 if (!empty($this->next_id)) {
381 array('rel' => 'next',
382 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
386 if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
389 array('rel' => 'prev',
390 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
394 if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
397 array('rel' => 'first',
398 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
402 $this->raw($doc->asString());
405 // TRANS: Client error displayed when coming across a non-supported API method.
406 $this->clientError(_('API method not found.'), 404);