apitimelinehome.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show the home timeline
  6. *
  7. * PHP version 5
  8. *
  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.
  13. *
  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.
  18. *
  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/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  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/
  35. */
  36. if (!defined('STATUSNET')) {
  37. exit(1);
  38. }
  39. /**
  40. * Returns the most recent notices (default 20) posted by the target user.
  41. * This is the equivalent of 'You and friends' page accessed via Web.
  42. *
  43. * @category API
  44. * @package StatusNet
  45. * @author Craig Andrews <candrews@integralblue.com>
  46. * @author Evan Prodromou <evan@status.net>
  47. * @author Jeffery To <jeffery.to@gmail.com>
  48. * @author mac65 <mac65@mac65.com>
  49. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  50. * @author Robin Millette <robin@millette.info>
  51. * @author Zach Copley <zach@status.net>
  52. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  53. * @link http://status.net/
  54. */
  55. class ApiTimelineHomeAction extends ApiBareAuthAction
  56. {
  57. var $notices = null;
  58. /**
  59. * Take arguments for running
  60. *
  61. * @param array $args $_REQUEST args
  62. *
  63. * @return boolean success flag
  64. */
  65. protected function prepare(array $args=array())
  66. {
  67. parent::prepare($args);
  68. $this->target = $this->getTargetProfile($this->arg('id'));
  69. if (!($this->target instanceof Profile)) {
  70. // TRANS: Client error displayed when requesting most recent dents by user and friends for a non-existing user.
  71. $this->clientError(_('No such user.'), 404);
  72. }
  73. $this->notices = $this->getNotices();
  74. return true;
  75. }
  76. /**
  77. * Handle the request
  78. *
  79. * Just show the notices
  80. *
  81. * @return void
  82. */
  83. protected function handle()
  84. {
  85. parent::handle();
  86. $this->showTimeline();
  87. }
  88. /**
  89. * Show the timeline of notices
  90. *
  91. * @return void
  92. */
  93. function showTimeline()
  94. {
  95. $sitename = common_config('site', 'name');
  96. // TRANS: Timeline title for user and friends. %s is a user nickname.
  97. $title = sprintf(_("%s and friends"), $this->target->nickname);
  98. $taguribase = TagURI::base();
  99. $id = "tag:$taguribase:HomeTimeline:" . $this->target->id;
  100. $subtitle = sprintf(
  101. // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
  102. _('Updates from %1$s and friends on %2$s!'),
  103. $this->target->nickname, $sitename
  104. );
  105. $logo = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
  106. $link = common_local_url('all',
  107. array('nickname' => $this->target->nickname));
  108. $self = $this->getSelfUri();
  109. switch($this->format) {
  110. case 'xml':
  111. $this->showXmlTimeline($this->notices);
  112. break;
  113. case 'rss':
  114. $this->showRssTimeline(
  115. $this->notices,
  116. $title,
  117. $link,
  118. $subtitle,
  119. null,
  120. $logo,
  121. $self
  122. );
  123. break;
  124. case 'atom':
  125. header('Content-Type: application/atom+xml; charset=utf-8');
  126. $atom = new AtomNoticeFeed($this->auth_user);
  127. $atom->setId($id);
  128. $atom->setTitle($title);
  129. $atom->setSubtitle($subtitle);
  130. $atom->setLogo($logo);
  131. $atom->setUpdated('now');
  132. $atom->addLink($link);
  133. $atom->setSelfLink($self);
  134. $atom->addEntryFromNotices($this->notices);
  135. $this->raw($atom->getString());
  136. break;
  137. case 'json':
  138. $this->showJsonTimeline($this->notices);
  139. break;
  140. case 'as':
  141. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  142. $doc = new ActivityStreamJSONDocument($this->auth_user);
  143. $doc->setTitle($title);
  144. $doc->addLink($link, 'alternate', 'text/html');
  145. $doc->addItemsFromNotices($this->notices);
  146. $this->raw($doc->asString());
  147. break;
  148. default:
  149. // TRANS: Client error displayed when coming across a non-supported API method.
  150. $this->clientError(_('API method not found.'), 404);
  151. }
  152. }
  153. /**
  154. * Get notices
  155. *
  156. * @return array notices
  157. */
  158. function getNotices()
  159. {
  160. $notices = array();
  161. $stream = new InboxNoticeStream($this->target, $this->scoped);
  162. $notice = $stream->getNotices(($this->page-1) * $this->count,
  163. $this->count,
  164. $this->since_id,
  165. $this->max_id);
  166. while ($notice->fetch()) {
  167. $notices[] = clone($notice);
  168. }
  169. return $notices;
  170. }
  171. /**
  172. * Is this action read only?
  173. *
  174. * @param array $args other arguments
  175. *
  176. * @return boolean true
  177. */
  178. function isReadOnly($args)
  179. {
  180. return true;
  181. }
  182. /**
  183. * When was this feed last modified?
  184. *
  185. * @return string datestamp of the latest notice in the stream
  186. */
  187. function lastModified()
  188. {
  189. if (!empty($this->notices) && (count($this->notices) > 0)) {
  190. return strtotime($this->notices[0]->created);
  191. }
  192. return null;
  193. }
  194. /**
  195. * An entity tag for this stream
  196. *
  197. * Returns an Etag based on the action name, language, user ID, and
  198. * timestamps of the first and last notice in the timeline
  199. *
  200. * @return string etag
  201. */
  202. function etag()
  203. {
  204. if (!empty($this->notices) && (count($this->notices) > 0)) {
  205. $last = count($this->notices) - 1;
  206. return '"' . implode(
  207. ':',
  208. array($this->arg('action'),
  209. common_user_cache_hash($this->auth_user),
  210. common_language(),
  211. $this->target->id,
  212. strtotime($this->notices[0]->created),
  213. strtotime($this->notices[$last]->created))
  214. )
  215. . '"';
  216. }
  217. return null;
  218. }
  219. }