apitimelinebookmarks.php 7.3 KB

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