The official, canonical postActiv repository. http://www.postactiv.com

apitimelinetag.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /* ============================================================================
  3. * Title: APITimelineTag
  4. * Show the latest notices for a given tag
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * Show the latest notices for a given tag
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Zach Copley
  40. * o Evan Prodromou
  41. * o Robin Millette <robin@millette.info>
  42. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  43. * o Craig Andrews <candrews@integralblue.com>
  44. * o Brion Vibber <brion@pobox.com>
  45. * o Mikael Nordfeldth <mmn@hethane.se>
  46. * o Hannes Mannerheim <h@nnesmannerhe.im>
  47. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  48. *
  49. * Web:
  50. * o postActiv <http://www.postactiv.com>
  51. * o GNU social <https://www.gnu.org/s/social/>
  52. * ============================================================================
  53. */
  54. // This file is formatted so that it provides useful documentation output in
  55. // NaturalDocs. Please be considerate of this before changing formatting.
  56. if (!defined('POSTACTIV')) { exit(1); }
  57. /**
  58. * Returns the 20 most recent notices tagged by a given tag
  59. */
  60. class ApiTimelineTagAction extends ApiPrivateAuthAction
  61. {
  62. var $notices = null;
  63. protected function prepare(array $args=array())
  64. {
  65. parent::prepare($args);
  66. $this->tag = $this->arg('tag');
  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 timeline with lastest notices with a given tag.
  92. // TRANS: %s is the tag.
  93. $title = sprintf(_("Notices tagged with %s"), $this->tag);
  94. $subtitle = sprintf(
  95. // TRANS: Subtitle for timeline with lastest notices with a given tag.
  96. // TRANS: %1$s is the tag, $2$s is the StatusNet sitename.
  97. _('Updates tagged with %1$s on %2$s!'),
  98. $this->tag,
  99. $sitename
  100. );
  101. $taguribase = TagURI::base();
  102. $id = "tag:$taguribase:TagTimeline:".$this->tag;
  103. $link = common_local_url(
  104. 'tag',
  105. array('tag' => $this->tag)
  106. );
  107. $self = $this->getSelfUri();
  108. switch($this->format) {
  109. case 'xml':
  110. $this->showXmlTimeline($this->notices);
  111. break;
  112. case 'rss':
  113. $this->showRssTimeline(
  114. $this->notices,
  115. $title,
  116. $link,
  117. $subtitle,
  118. null,
  119. $sitelogo,
  120. $self
  121. );
  122. break;
  123. case 'atom':
  124. header('Content-Type: application/atom+xml; charset=utf-8');
  125. $atom = new AtomNoticeFeed($this->auth_user);
  126. $atom->setId($id);
  127. $atom->setTitle($title);
  128. $atom->setSubtitle($subtitle);
  129. $atom->setLogo($sitelogo);
  130. $atom->setUpdated('now');
  131. $atom->addLink($link);
  132. $atom->setSelfLink($self);
  133. $atom->addEntryFromNotices($this->notices);
  134. $this->raw($atom->getString());
  135. break;
  136. case 'json':
  137. $this->showJsonTimeline($this->notices);
  138. break;
  139. case 'as':
  140. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  141. $doc = new ActivityStreamJSONDocument($this->auth_user);
  142. $doc->setTitle($title);
  143. $doc->addLink($link, 'alternate', 'text/html');
  144. $doc->addItemsFromNotices($this->notices);
  145. $this->raw($doc->asString());
  146. break;
  147. default:
  148. // TRANS: Client error displayed when coming across a non-supported API method.
  149. $this->clientError(_('API method not found.'), $code = 404);
  150. break;
  151. }
  152. }
  153. /**
  154. * Get notices
  155. *
  156. * @return array notices
  157. */
  158. function getNotices()
  159. {
  160. $notice = Notice_tag::getStream($this->tag)->getNotices(($this->page - 1) * $this->count,
  161. $this->count + 1,
  162. $this->since_id,
  163. $this->max_id);
  164. return $notice->fetchAll();
  165. }
  166. /**
  167. * Is this action read only?
  168. *
  169. * @param array $args other arguments
  170. *
  171. * @return boolean true
  172. */
  173. function isReadOnly($args)
  174. {
  175. return true;
  176. }
  177. /**
  178. * When was this feed last modified?
  179. *
  180. * @return string datestamp of the latest notice in the stream
  181. */
  182. function lastModified()
  183. {
  184. if (!empty($this->notices) && (count($this->notices) > 0)) {
  185. return strtotime($this->notices[0]->created);
  186. }
  187. return null;
  188. }
  189. /**
  190. * An entity tag for this stream
  191. *
  192. * Returns an Etag based on the action name, language, and
  193. * timestamps of the first and last notice in the timeline
  194. *
  195. * @return string etag
  196. */
  197. function etag()
  198. {
  199. if (!empty($this->notices) && (count($this->notices) > 0)) {
  200. $last = count($this->notices) - 1;
  201. return '"' . implode(
  202. ':',
  203. array($this->arg('action'),
  204. common_user_cache_hash($this->auth_user),
  205. common_language(),
  206. $this->tag,
  207. strtotime($this->notices[0]->created),
  208. strtotime($this->notices[$last]->created))
  209. )
  210. . '"';
  211. }
  212. return null;
  213. }
  214. }
  215. // END OF FILE
  216. // ============================================================================
  217. ?>