apitimelinegroup.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a group's 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 Jeffery To <jeffery.to@gmail.com>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009-2010 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('STATUSNET')) {
  34. exit(1);
  35. }
  36. /**
  37. * Returns the most recent notices (default 20) posted to the group specified by ID
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Jeffery To <jeffery.to@gmail.com>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiTimelineGroupAction extends ApiPrivateAuthAction
  49. {
  50. var $group = null;
  51. var $notices = null;
  52. /**
  53. * Take arguments for running
  54. *
  55. * @param array $args $_REQUEST args
  56. *
  57. * @return boolean success flag
  58. *
  59. */
  60. protected function prepare(array $args=array())
  61. {
  62. parent::prepare($args);
  63. $this->group = $this->getTargetGroup($this->arg('id'));
  64. return true;
  65. }
  66. /**
  67. * Handle the request
  68. *
  69. * Just show the notices
  70. *
  71. * @return void
  72. */
  73. protected function handle()
  74. {
  75. parent::handle();
  76. if (empty($this->group)) {
  77. // TRANS: Client error displayed requesting most recent notices to a group for a non-existing group.
  78. $this->clientError(_('Group not found.'), 404);
  79. }
  80. $this->notices = $this->getNotices();
  81. $this->showTimeline();
  82. }
  83. /**
  84. * Show the timeline of notices
  85. *
  86. * @return void
  87. */
  88. function showTimeline()
  89. {
  90. // We'll pull common formatting out of this for other formats
  91. $atom = new AtomGroupNoticeFeed($this->group, $this->auth_user);
  92. $self = $this->getSelfUri();
  93. $link = common_local_url('showgroup',
  94. array('nickname' => $this->group->nickname));
  95. switch($this->format) {
  96. case 'xml':
  97. $this->showXmlTimeline($this->notices);
  98. break;
  99. case 'rss':
  100. $this->showRssTimeline(
  101. $this->notices,
  102. $atom->title,
  103. $this->group->homeUrl(),
  104. $atom->subtitle,
  105. null,
  106. $atom->logo,
  107. $self
  108. );
  109. break;
  110. case 'atom':
  111. header('Content-Type: application/atom+xml; charset=utf-8');
  112. $atom->addEntryFromNotices($this->notices);
  113. $this->raw($atom->getString());
  114. break;
  115. case 'json':
  116. $this->showJsonTimeline($this->notices);
  117. break;
  118. case 'as':
  119. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  120. $doc = new ActivityStreamJSONDocument($this->auth_user);
  121. $doc->setTitle($atom->title);
  122. $doc->addLink($link, 'alternate', 'text/html');
  123. $doc->addItemsFromNotices($this->notices);
  124. $this->raw($doc->asString());
  125. break;
  126. default:
  127. // TRANS: Client error displayed when trying to handle an unknown API method.
  128. $this->clientError(_('API method not found.'), 404);
  129. }
  130. }
  131. /**
  132. * Get notices
  133. *
  134. * @return array notices
  135. */
  136. function getNotices()
  137. {
  138. $notices = array();
  139. $notice = $this->group->getNotices(
  140. ($this->page-1) * $this->count,
  141. $this->count,
  142. $this->since_id,
  143. $this->max_id
  144. );
  145. while ($notice->fetch()) {
  146. $notices[] = clone($notice);
  147. }
  148. return $notices;
  149. }
  150. /**
  151. * Is this action read only?
  152. *
  153. * @param array $args other arguments
  154. *
  155. * @return boolean true
  156. */
  157. function isReadOnly($args)
  158. {
  159. return true;
  160. }
  161. /**
  162. * When was this feed last modified?
  163. *
  164. * @return string datestamp of the latest notice in the stream
  165. */
  166. function lastModified()
  167. {
  168. if (!empty($this->notices) && (count($this->notices) > 0)) {
  169. return strtotime($this->notices[0]->created);
  170. }
  171. return null;
  172. }
  173. /**
  174. * An entity tag for this stream
  175. *
  176. * Returns an Etag based on the action name, language, group ID and
  177. * timestamps of the first and last notice in the timeline
  178. *
  179. * @return string etag
  180. */
  181. function etag()
  182. {
  183. if (!empty($this->notices) && (count($this->notices) > 0)) {
  184. $last = count($this->notices) - 1;
  185. return '"' . implode(
  186. ':',
  187. array($this->arg('action'),
  188. common_user_cache_hash($this->auth_user),
  189. common_language(),
  190. $this->group->id,
  191. strtotime($this->notices[0]->created),
  192. strtotime($this->notices[$last]->created))
  193. )
  194. . '"';
  195. }
  196. return null;
  197. }
  198. }