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('STATUSNET')) {
41 require_once INSTALLDIR . '/lib/apibareauth.php';
44 * Returns the most recent notices (default 20) posted by the authenticating
45 * user. Another user's timeline can be requested via the id parameter. This
46 * is the API equivalent of the user profile web page.
50 * @author Craig Andrews <candrews@integralblue.com>
51 * @author Evan Prodromou <evan@status.net>
52 * @author Jeffery To <jeffery.to@gmail.com>
53 * @author mac65 <mac65@mac65.com>
54 * @author Mike Cochrane <mikec@mikenz.geek.nz>
55 * @author Robin Millette <robin@millette.info>
56 * @author Zach Copley <zach@status.net>
57 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
58 * @link http://status.net/
61 class ApiTimelineUserAction extends ApiBareAuthAction
67 * Take arguments for running
69 * @param array $args $_REQUEST args
71 * @return boolean success flag
75 function prepare($args)
77 parent::prepare($args);
79 $this->user = $this->getTargetUser($this->arg('id'));
81 if (empty($this->user)) {
82 $this->clientError(_('No such user.'), 404, $this->format);
86 $this->notices = $this->getNotices();
94 * Just show the notices
96 * @param array $args $_REQUEST data (unused)
101 function handle($args)
103 parent::handle($args);
105 if ($this->isPost()) {
108 $this->showTimeline();
113 * Show the timeline of notices
118 function showTimeline()
120 $profile = $this->user->getProfile();
122 // We'll use the shared params from the Atom stub
123 // for other feed types.
124 $atom = new AtomUserNoticeFeed($this->user, $this->auth_user);
126 $link = common_local_url(
128 array('nickname' => $this->user->nickname)
131 $self = $this->getSelfUri();
133 // FriendFeed's SUP protocol
134 // Also added RSS and Atom feeds
136 $suplink = common_local_url('sup', null, null, $this->user->id);
137 header('X-SUP-ID: ' . $suplink);
139 switch($this->format) {
141 $this->showXmlTimeline($this->notices);
144 $this->showRssTimeline(
156 header('Content-Type: application/atom+xml; charset=utf-8');
159 $atom->setSelfLink($self);
161 // Add navigation links: next, prev, first
162 // Note: we use IDs rather than pages for navigation; page boundaries
163 // change too quickly!
165 if (!empty($this->next_id)) {
166 $nextUrl = common_local_url('ApiTimelineUser',
167 array('format' => 'atom',
168 'id' => $this->user->id),
169 array('max_id' => $this->next_id));
171 $atom->addLink($nextUrl,
172 array('rel' => 'next',
173 'type' => 'application/atom+xml'));
176 if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
178 $lastNotice = $this->notices[0];
179 $lastId = $lastNotice->id;
181 $prevUrl = common_local_url('ApiTimelineUser',
182 array('format' => 'atom',
183 'id' => $this->user->id),
184 array('since_id' => $lastId));
186 $atom->addLink($prevUrl,
187 array('rel' => 'prev',
188 'type' => 'application/atom+xml'));
191 if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
193 $firstUrl = common_local_url('ApiTimelineUser',
194 array('format' => 'atom',
195 'id' => $this->user->id));
197 $atom->addLink($firstUrl,
198 array('rel' => 'first',
199 'type' => 'application/atom+xml'));
203 $atom->addEntryFromNotices($this->notices);
204 $this->raw($atom->getString());
208 $this->showJsonTimeline($this->notices);
211 $this->clientError(_('API method not found.'), $code = 404);
220 * @return array notices
223 function getNotices()
227 $notice = $this->user->getNotices(($this->page-1) * $this->count,
232 while ($notice->fetch()) {
233 if (count($notices) < $this->count) {
234 $notices[] = clone($notice);
236 $this->next_id = $notice->id;
245 * Is this action read only?
247 * @param array $args other arguments
249 * @return boolean true
252 function isReadOnly($args)
258 * When was this feed last modified?
260 * @return string datestamp of the latest notice in the stream
263 function lastModified()
265 if (!empty($this->notices) && (count($this->notices) > 0)) {
266 return strtotime($this->notices[0]->created);
273 * An entity tag for this stream
275 * Returns an Etag based on the action name, language, user ID, and
276 * timestamps of the first and last notice in the timeline
278 * @return string etag
283 if (!empty($this->notices) && (count($this->notices) > 0)) {
285 $last = count($this->notices) - 1;
287 return '"' . implode(
289 array($this->arg('action'),
290 common_user_cache_hash($this->auth_user),
293 strtotime($this->notices[0]->created),
294 strtotime($this->notices[$last]->created))
302 function handlePost()
304 if (empty($this->auth_user) ||
305 $this->auth_user->id != $this->user->id) {
306 $this->clientError(_("Only the user can add to their own timeline."));
310 if ($this->format != 'atom') {
311 // Only handle posts for Atom
312 $this->clientError(_("Only accept AtomPub for atom feeds."));
316 $xml = file_get_contents('php://input');
318 $dom = DOMDocument::loadXML($xml);
320 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
321 $dom->documentElement->localName != 'entry') {
322 $this->clientError(_('Atom post must be an Atom entry.'));
326 $activity = new Activity($dom->documentElement);
328 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
330 if ($activity->verb != ActivityVerb::POST) {
331 $this->clientError(_('Can only handle post activities.'));
335 $note = $activity->objects[0];
337 if (!in_array($note->type, array(ActivityObject::NOTE,
338 ActivityObject::BLOGENTRY,
339 ActivityObject::STATUS))) {
340 $this->clientError(sprintf(_('Cannot handle activity object type "%s"',
345 $saved = $this->postNote($activity);
347 Event::handle('EndAtomPubNewActivity', array($activity, $saved));
350 if (!empty($saved)) {
351 header("Location: " . common_local_url('ApiStatusesShow', array('notice_id' => $saved->id,
352 'format' => 'atom')));
353 $this->showSingleAtomStatus($saved);
357 function postNote($activity)
359 $note = $activity->objects[0];
361 // Use summary as fallback for content
363 if (!empty($note->content)) {
364 $sourceContent = $note->content;
365 } else if (!empty($note->summary)) {
366 $sourceContent = $note->summary;
367 } else if (!empty($note->title)) {
368 $sourceContent = $note->title;
370 // @fixme fetch from $sourceUrl?
371 // @todo i18n FIXME: use sprintf and add i18n.
372 $this->clientError("No content for notice {$note->id}.");
376 // Get (safe!) HTML and text versions of the content
378 $rendered = $this->purify($sourceContent);
379 $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
381 $shortened = common_shorten_links($content);
383 $options = array('is_local' => Notice::LOCAL_PUBLIC,
384 'rendered' => $rendered,
385 'replies' => array(),
390 // accept remote URI (not necessarily a good idea)
392 common_debug("Note ID is {$note->id}");
394 if (!empty($note->id)) {
395 $notice = Notice::staticGet('uri', trim($note->id));
397 if (!empty($notice)) {
398 $this->clientError(sprintf(_('Notice with URI "%s" already exists.'),
402 common_log(LOG_NOTICE, "Saving client-supplied notice URI '$note->id'");
403 $options['uri'] = $note->id;
406 // accept remote create time (also maybe not such a good idea)
408 if (!empty($activity->time)) {
409 common_log(LOG_NOTICE, "Saving client-supplied create time {$activity->time}");
410 $options['created'] = common_sql_date($activity->time);
413 // Check for optional attributes...
415 if (!empty($activity->context)) {
417 foreach ($activity->context->attention as $uri) {
419 $profile = Profile::fromURI($uri);
421 if (!empty($profile)) {
422 $options['replies'] = $uri;
424 $group = User_group::staticGet('uri', $uri);
425 if (!empty($group)) {
426 $options['groups'] = $uri;
428 // @fixme: hook for discovery here
429 common_log(LOG_WARNING, sprintf(_('AtomPub post with unknown attention URI %s'), $uri));
434 // Maintain direct reply associations
435 // @fixme what about conversation ID?
437 if (!empty($activity->context->replyToID)) {
438 $orig = Notice::staticGet('uri',
439 $activity->context->replyToID);
441 $options['reply_to'] = $orig->id;
445 $location = $activity->context->location;
448 $options['lat'] = $location->lat;
449 $options['lon'] = $location->lon;
450 if ($location->location_id) {
451 $options['location_ns'] = $location->location_ns;
452 $options['location_id'] = $location->location_id;
457 // Atom categories <-> hashtags
459 foreach ($activity->categories as $cat) {
461 $term = common_canonical_tag($cat->term);
463 $options['tags'][] = $term;
468 // Atom enclosures -> attachment URLs
469 foreach ($activity->enclosures as $href) {
470 // @fixme save these locally or....?
471 $options['urls'][] = $href;
474 $saved = Notice::saveNew($this->user->id,
476 'atompub', // TODO: deal with this
482 function purify($content)
484 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
486 $config = array('safe' => 1,
487 'deny_attribute' => 'id,style,on*');
488 return htmLawed($content, $config);