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/
60 class ApiTimelineUserAction extends ApiBareAuthAction
65 * Take arguments for running
67 * @param array $args $_REQUEST args
69 * @return boolean success flag
71 function prepare($args)
73 parent::prepare($args);
75 $this->user = $this->getTargetUser($this->arg('id'));
77 if (empty($this->user)) {
78 // TRANS: Client error displayed requesting most recent notices for a non-existing user.
79 $this->clientError(_('No such user.'), 404, $this->format);
83 $this->notices = $this->getNotices();
91 * Just show the notices
93 * @param array $args $_REQUEST data (unused)
97 function handle($args)
99 parent::handle($args);
101 if ($this->isPost()) {
104 $this->showTimeline();
109 * Show the timeline of notices
113 function showTimeline()
115 $profile = $this->user->getProfile();
117 // We'll use the shared params from the Atom stub
118 // for other feed types.
119 $atom = new AtomUserNoticeFeed($this->user, $this->auth_user);
121 $link = common_local_url(
123 array('nickname' => $this->user->nickname)
126 $self = $this->getSelfUri();
128 // FriendFeed's SUP protocol
129 // Also added RSS and Atom feeds
131 $suplink = common_local_url('sup', null, null, $this->user->id);
132 header('X-SUP-ID: ' . $suplink);
134 switch($this->format) {
136 $this->showXmlTimeline($this->notices);
139 $this->showRssTimeline(
150 header('Content-Type: application/atom+xml; charset=utf-8');
153 $atom->setSelfLink($self);
155 // Add navigation links: next, prev, first
156 // Note: we use IDs rather than pages for navigation; page boundaries
157 // change too quickly!
159 if (!empty($this->next_id)) {
160 $nextUrl = common_local_url('ApiTimelineUser',
161 array('format' => 'atom',
162 'id' => $this->user->id),
163 array('max_id' => $this->next_id));
165 $atom->addLink($nextUrl,
166 array('rel' => 'next',
167 'type' => 'application/atom+xml'));
170 if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
172 $lastNotice = $this->notices[0];
173 $lastId = $lastNotice->id;
175 $prevUrl = common_local_url('ApiTimelineUser',
176 array('format' => 'atom',
177 'id' => $this->user->id),
178 array('since_id' => $lastId));
180 $atom->addLink($prevUrl,
181 array('rel' => 'prev',
182 'type' => 'application/atom+xml'));
185 if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
187 $firstUrl = common_local_url('ApiTimelineUser',
188 array('format' => 'atom',
189 'id' => $this->user->id));
191 $atom->addLink($firstUrl,
192 array('rel' => 'first',
193 'type' => 'application/atom+xml'));
197 $atom->addEntryFromNotices($this->notices);
198 $this->raw($atom->getString());
202 $this->showJsonTimeline($this->notices);
205 // TRANS: Client error displayed when trying to handle an unknown API method.
206 $this->clientError(_('API method not found.'), $code = 404);
214 * @return array notices
216 function getNotices()
220 $notice = $this->user->getNotices(($this->page-1) * $this->count,
225 while ($notice->fetch()) {
226 if (count($notices) < $this->count) {
227 $notices[] = clone($notice);
229 $this->next_id = $notice->id;
238 * Is this action read only?
240 * @param array $args other arguments
242 * @return boolean true
245 function isReadOnly($args)
247 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
255 * When was this feed last modified?
257 * @return string datestamp of the latest notice in the stream
259 function lastModified()
261 if (!empty($this->notices) && (count($this->notices) > 0)) {
262 return strtotime($this->notices[0]->created);
269 * An entity tag for this stream
271 * Returns an Etag based on the action name, language, user ID, and
272 * timestamps of the first and last notice in the timeline
274 * @return string etag
278 if (!empty($this->notices) && (count($this->notices) > 0)) {
279 $last = count($this->notices) - 1;
281 return '"' . implode(
283 array($this->arg('action'),
284 common_user_cache_hash($this->auth_user),
287 strtotime($this->notices[0]->created),
288 strtotime($this->notices[$last]->created))
296 function handlePost()
298 if (empty($this->auth_user) ||
299 $this->auth_user->id != $this->user->id) {
300 $this->clientError(_("Only the user can add to their own timeline."));
304 if ($this->format != 'atom') {
305 // Only handle posts for Atom
306 $this->clientError(_("Only accept AtomPub for atom feeds."));
310 $xml = file_get_contents('php://input');
312 $dom = DOMDocument::loadXML($xml);
314 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
315 $dom->documentElement->localName != 'entry') {
316 $this->clientError(_('Atom post must be an Atom entry.'));
320 $activity = new Activity($dom->documentElement);
322 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
324 if ($activity->verb != ActivityVerb::POST) {
325 $this->clientError(_('Can only handle post activities.'));
329 $note = $activity->objects[0];
331 if (!in_array($note->type, array(ActivityObject::NOTE,
332 ActivityObject::BLOGENTRY,
333 ActivityObject::STATUS))) {
334 $this->clientError(sprintf(_('Cannot handle activity object type "%s"',
339 $saved = $this->postNote($activity);
341 Event::handle('EndAtomPubNewActivity', array($activity, $saved));
344 if (!empty($saved)) {
345 header("Location: " . common_local_url('ApiStatusesShow', array('notice_id' => $saved->id,
346 'format' => 'atom')));
347 $this->showSingleAtomStatus($saved);
351 function postNote($activity)
353 $note = $activity->objects[0];
355 // Use summary as fallback for content
357 if (!empty($note->content)) {
358 $sourceContent = $note->content;
359 } else if (!empty($note->summary)) {
360 $sourceContent = $note->summary;
361 } else if (!empty($note->title)) {
362 $sourceContent = $note->title;
364 // @fixme fetch from $sourceUrl?
365 // @todo i18n FIXME: use sprintf and add i18n.
366 $this->clientError("No content for notice {$note->id}.");
370 // Get (safe!) HTML and text versions of the content
372 $rendered = $this->purify($sourceContent);
373 $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
375 $shortened = common_shorten_links($content);
377 $options = array('is_local' => Notice::LOCAL_PUBLIC,
378 'rendered' => $rendered,
379 'replies' => array(),
384 // accept remote URI (not necessarily a good idea)
386 common_debug("Note ID is {$note->id}");
388 if (!empty($note->id)) {
389 $notice = Notice::staticGet('uri', trim($note->id));
391 if (!empty($notice)) {
392 $this->clientError(sprintf(_('Notice with URI "%s" already exists.'),
396 common_log(LOG_NOTICE, "Saving client-supplied notice URI '$note->id'");
397 $options['uri'] = $note->id;
400 // accept remote create time (also maybe not such a good idea)
402 if (!empty($activity->time)) {
403 common_log(LOG_NOTICE, "Saving client-supplied create time {$activity->time}");
404 $options['created'] = common_sql_date($activity->time);
407 // Check for optional attributes...
409 if (!empty($activity->context)) {
411 foreach ($activity->context->attention as $uri) {
413 $profile = Profile::fromURI($uri);
415 if (!empty($profile)) {
416 $options['replies'] = $uri;
418 $group = User_group::staticGet('uri', $uri);
419 if (!empty($group)) {
420 $options['groups'] = $uri;
422 // @fixme: hook for discovery here
423 common_log(LOG_WARNING, sprintf(_('AtomPub post with unknown attention URI %s'), $uri));
428 // Maintain direct reply associations
429 // @fixme what about conversation ID?
431 if (!empty($activity->context->replyToID)) {
432 $orig = Notice::staticGet('uri',
433 $activity->context->replyToID);
435 $options['reply_to'] = $orig->id;
439 $location = $activity->context->location;
442 $options['lat'] = $location->lat;
443 $options['lon'] = $location->lon;
444 if ($location->location_id) {
445 $options['location_ns'] = $location->location_ns;
446 $options['location_id'] = $location->location_id;
451 // Atom categories <-> hashtags
453 foreach ($activity->categories as $cat) {
455 $term = common_canonical_tag($cat->term);
457 $options['tags'][] = $term;
462 // Atom enclosures -> attachment URLs
463 foreach ($activity->enclosures as $href) {
464 // @fixme save these locally or....?
465 $options['urls'][] = $href;
468 $saved = Notice::saveNew($this->user->id,
470 'atompub', // TODO: deal with this
476 function purify($content)
478 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
480 $config = array('safe' => 1,
481 'deny_attribute' => 'id,style,on*');
482 return htmLawed($content, $config);