apitimelinementions.php 7.5 KB

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