apitimelinepublicandexternal.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
  3. · ·
  4. · ·
  5. · Q V I T T E R ·
  6. · ·
  7. · https://git.gnu.io/h2p/Qvitter ·
  8. · ·
  9. · ·
  10. · <o) ·
  11. · /_//// ·
  12. · (____/ ·
  13. · (o< ·
  14. · o> \\\\_\ ·
  15. · \\) \____) ·
  16. · ·
  17. · ·
  18. · ·
  19. · Qvitter is free software: you can redistribute it and / or modify it ·
  20. · under the terms of the GNU Affero General Public License as published by ·
  21. · the Free Software Foundation, either version three of the License or (at ·
  22. · your option) any later version. ·
  23. · ·
  24. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  25. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  26. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  27. · more details. ·
  28. · ·
  29. · You should have received a copy of the GNU Affero General Public License ·
  30. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  31. · ·
  32. · Contact h@nnesmannerhe.im if you have any questions. ·
  33. · ·
  34. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  35. if (!defined('STATUSNET')) {
  36. exit(1);
  37. }
  38. /**
  39. * Returns the most recent notices (default 20) posted by everybody
  40. *
  41. * @category API
  42. * @package StatusNet
  43. * @author Craig Andrews <candrews@integralblue.com>
  44. * @author Evan Prodromou <evan@status.net>
  45. * @author Jeffery To <jeffery.to@gmail.com>
  46. * @author mac65 <mac65@mac65.com>
  47. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  48. * @author Robin Millette <robin@millette.info>
  49. * @author Zach Copley <zach@status.net>
  50. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  51. * @link http://status.net/
  52. */
  53. class ApiTimelinePublicAndExternalAction extends ApiPrivateAuthAction
  54. {
  55. var $notices = null;
  56. /**
  57. * Take arguments for running
  58. *
  59. * @param array $args $_REQUEST args
  60. *
  61. * @return boolean success flag
  62. *
  63. */
  64. protected function prepare(array $args=array())
  65. {
  66. parent::prepare($args);
  67. $this->notices = $this->getNotices();
  68. return true;
  69. }
  70. /**
  71. * Handle the request
  72. *
  73. * Just show the notices
  74. *
  75. * @return void
  76. */
  77. protected function handle()
  78. {
  79. parent::handle();
  80. $this->showTimeline();
  81. }
  82. /**
  83. * Show the timeline of notices
  84. *
  85. * @return void
  86. */
  87. function showTimeline()
  88. {
  89. $sitename = common_config('site', 'name');
  90. $sitelogo = (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png');
  91. // TRANS: Title for site timeline. %s is the StatusNet sitename.
  92. $title = sprintf(_("%s public and external timeline"), $sitename);
  93. $taguribase = TagURI::base();
  94. $id = "tag:$taguribase:PublicAndExternalTimeline";
  95. $link = common_local_url('public');
  96. $self = $this->getSelfUri();
  97. // TRANS: Subtitle for site timeline. %s is the StatusNet sitename.
  98. $subtitle = sprintf(_("%s updates from the whole known network!"), $sitename);
  99. switch($this->format) {
  100. case 'xml':
  101. $this->showXmlTimeline($this->notices);
  102. break;
  103. case 'rss':
  104. $this->showRssTimeline(
  105. $this->notices,
  106. $title,
  107. $link,
  108. $subtitle,
  109. null,
  110. $sitelogo,
  111. $self
  112. );
  113. break;
  114. case 'atom':
  115. header('Content-Type: application/atom+xml; charset=utf-8');
  116. $atom = new AtomNoticeFeed($this->auth_user);
  117. $atom->setId($id);
  118. $atom->setTitle($title);
  119. $atom->setSubtitle($subtitle);
  120. $atom->setLogo($sitelogo);
  121. $atom->setUpdated('now');
  122. $atom->addLink(common_local_url('public'));
  123. $atom->setSelfLink($self);
  124. $atom->addEntryFromNotices($this->notices);
  125. $this->raw($atom->getString());
  126. break;
  127. case 'json':
  128. $this->showJsonTimeline($this->notices);
  129. break;
  130. case 'as':
  131. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  132. $doc = new ActivityStreamJSONDocument($this->auth_user);
  133. $doc->setTitle($title);
  134. $doc->addLink($link, 'alternate', 'text/html');
  135. $doc->addItemsFromNotices($this->notices);
  136. $this->raw($doc->asString());
  137. break;
  138. default:
  139. // TRANS: Client error displayed when coming across a non-supported API method.
  140. $this->clientError(_('API method not found.'), $code = 404);
  141. break;
  142. }
  143. }
  144. /**
  145. * Get notices
  146. *
  147. * @return array notices
  148. */
  149. function getNotices()
  150. {
  151. $notices = array();
  152. $profile = ($this->auth_user) ? $this->auth_user->getProfile() : null;
  153. $stream = new PublicAndExternalNoticeStream($profile);
  154. $notice = $stream->getNotices(($this->page - 1) * $this->count,
  155. $this->count,
  156. $this->since_id,
  157. $this->max_id);
  158. $notices = $notice->fetchAll();
  159. NoticeList::prefill($notices);
  160. return $notices;
  161. }
  162. /**
  163. * Is this action read only?
  164. *
  165. * @param array $args other arguments
  166. *
  167. * @return boolean true
  168. */
  169. function isReadOnly($args)
  170. {
  171. return true;
  172. }
  173. /**
  174. * When was this feed last modified?
  175. *
  176. * @return string datestamp of the latest notice in the stream
  177. */
  178. function lastModified()
  179. {
  180. if (!empty($this->notices) && (count($this->notices) > 0)) {
  181. return strtotime($this->notices[0]->created);
  182. }
  183. return null;
  184. }
  185. /**
  186. * An entity tag for this stream
  187. *
  188. * Returns an Etag based on the action name, language, and
  189. * timestamps of the first and last notice in the timeline
  190. *
  191. * @return string etag
  192. */
  193. function etag()
  194. {
  195. if (!empty($this->notices) && (count($this->notices) > 0)) {
  196. $last = count($this->notices) - 1;
  197. return '"' . implode(
  198. ':',
  199. array($this->arg('action'),
  200. common_user_cache_hash($this->auth_user),
  201. common_language(),
  202. strtotime($this->notices[0]->created),
  203. strtotime($this->notices[$last]->created))
  204. )
  205. . '"';
  206. }
  207. return null;
  208. }
  209. }