apitimelinefavorites.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a user's favorite notices
  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 20 most recent favorite notices 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 ApiTimelineFavoritesAction 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. protected function prepare(array $args=array())
  58. {
  59. parent::prepare($args);
  60. $this->target = $this->getTargetProfile($this->arg('id'));
  61. if (!($this->target instanceof Profile)) {
  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);
  64. }
  65. $this->notices = $this->getNotices();
  66. return true;
  67. }
  68. /**
  69. * Handle the request
  70. *
  71. * Just show the notices
  72. *
  73. * @return void
  74. */
  75. protected function handle()
  76. {
  77. parent::handle();
  78. $this->showTimeline();
  79. }
  80. /**
  81. * Show the timeline of notices
  82. *
  83. * @return void
  84. */
  85. function showTimeline()
  86. {
  87. $sitename = common_config('site', 'name');
  88. $title = sprintf(
  89. // TRANS: Title for timeline of most recent favourite notices by a user.
  90. // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
  91. _('%1$s / Favorites from %2$s'),
  92. $sitename,
  93. $this->target->nickname
  94. );
  95. $taguribase = TagURI::base();
  96. $id = "tag:$taguribase:Favorites:" . $this->target->id;
  97. $subtitle = sprintf(
  98. // TRANS: Subtitle for timeline of most recent favourite notices by a user.
  99. // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
  100. // TRANS: %3$s is a user nickname.
  101. _('%1$s updates favorited by %2$s / %3$s.'),
  102. $sitename,
  103. $this->target->getBestName(),
  104. $this->target->nickname
  105. );
  106. $logo = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
  107. $link = common_local_url('showfavorites',
  108. array('nickname' => $this->target->nickname));
  109. $self = $this->getSelfUri();
  110. switch($this->format) {
  111. case 'xml':
  112. $this->showXmlTimeline($this->notices);
  113. break;
  114. case 'rss':
  115. $this->showRssTimeline(
  116. $this->notices,
  117. $title,
  118. $link,
  119. $subtitle,
  120. null,
  121. $logo,
  122. $self
  123. );
  124. break;
  125. case 'atom':
  126. header('Content-Type: application/atom+xml; charset=utf-8');
  127. $atom = new AtomNoticeFeed($this->auth_user);
  128. $atom->setId($id);
  129. $atom->setTitle($title);
  130. $atom->setSubtitle($subtitle);
  131. $atom->setLogo($logo);
  132. $atom->setUpdated('now');
  133. $atom->addLink($link);
  134. $atom->setSelfLink($self);
  135. $atom->addEntryFromNotices($this->notices);
  136. $this->raw($atom->getString());
  137. break;
  138. case 'json':
  139. $this->showJsonTimeline($this->notices);
  140. break;
  141. case 'as':
  142. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  143. $doc = new ActivityStreamJSONDocument($this->auth_user);
  144. $doc->setTitle($title);
  145. $doc->addLink($link,'alternate', 'text/html');
  146. $doc->addItemsFromNotices($this->notices);
  147. $this->raw($doc->asString());
  148. break;
  149. default:
  150. // TRANS: Client error displayed when coming across a non-supported API method.
  151. $this->clientError(_('API method not found.'), 404);
  152. }
  153. }
  154. /**
  155. * Get notices
  156. *
  157. * @return array notices
  158. */
  159. function getNotices()
  160. {
  161. $notices = array();
  162. common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
  163. $notice = Fave::stream($this->target->id,
  164. ($this->page-1) * $this->count, // offset
  165. $this->count, // limit
  166. (!empty($this->auth_user) && $this->auth_user->id == $this->target->id), // own feed?
  167. $this->since_id,
  168. $this->max_id
  169. );
  170. while ($notice->fetch()) {
  171. $notices[] = clone($notice);
  172. }
  173. return $notices;
  174. }
  175. /**
  176. * Is this action read only?
  177. *
  178. * @param array $args other arguments
  179. *
  180. * @return boolean true
  181. */
  182. function isReadOnly($args)
  183. {
  184. return true;
  185. }
  186. /**
  187. * When was this feed last modified?
  188. *
  189. * @return string datestamp of the latest notice in the stream
  190. */
  191. function lastModified()
  192. {
  193. if (!empty($this->notices) && (count($this->notices) > 0)) {
  194. return strtotime($this->notices[0]->created);
  195. }
  196. return null;
  197. }
  198. /**
  199. * An entity tag for this stream
  200. *
  201. * Returns an Etag based on the action name, language, user ID, and
  202. * timestamps of the first and last notice in the timeline
  203. *
  204. * @return string etag
  205. */
  206. function etag()
  207. {
  208. if (!empty($this->notices) && (count($this->notices) > 0)) {
  209. $last = count($this->notices) - 1;
  210. return '"' . implode(
  211. ':',
  212. array($this->arg('action'),
  213. common_user_cache_hash($this->auth_user),
  214. common_language(),
  215. $this->target->id,
  216. strtotime($this->notices[0]->created),
  217. strtotime($this->notices[$last]->created))
  218. )
  219. . '"';
  220. }
  221. return null;
  222. }
  223. }