apiconversation.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Show a stream of notices in a particular conversation
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category API
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Show a stream of notices in a particular conversation
  37. *
  38. * @category API
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ApiconversationAction extends ApiAuthAction
  46. {
  47. protected $conversation = null;
  48. protected $notices = null;
  49. protected function prepare(array $args=array())
  50. {
  51. parent::prepare($args);
  52. $convId = $this->trimmed('id');
  53. if (empty($convId)) {
  54. // TRANS: Client exception thrown when no conversation ID is given.
  55. throw new ClientException(_('No conversation ID.'));
  56. }
  57. $this->conversation = Conversation::getKV('id', $convId);
  58. if (empty($this->conversation)) {
  59. // TRANS: Client exception thrown when referring to a non-existing conversation ID (%d).
  60. throw new ClientException(sprintf(_('No conversation with ID %d.'), $convId),
  61. 404);
  62. }
  63. $stream = new ConversationNoticeStream($convId, $this->scoped);
  64. $notice = $stream->getNotices(($this->page-1) * $this->count,
  65. $this->count,
  66. $this->since_id,
  67. $this->max_id);
  68. $this->notices = $notice->fetchAll();
  69. return true;
  70. }
  71. /**
  72. * Handler method
  73. *
  74. * @param array $argarray is ignored since it's now passed in in prepare()
  75. *
  76. * @return void
  77. */
  78. function handle($argarray=null)
  79. {
  80. $sitename = common_config('site', 'name');
  81. // TRANS: Title for conversion timeline.
  82. $title = _m('TITLE', 'Conversation');
  83. $id = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
  84. $link = common_local_url('conversation', array('id' => $this->conversation->id));
  85. $self = $id;
  86. switch($this->format) {
  87. case 'xml':
  88. $this->showXmlTimeline($this->notices);
  89. break;
  90. case 'rss':
  91. $this->showRssTimeline(
  92. $this->notices,
  93. $title,
  94. $link,
  95. null,
  96. null,
  97. null,
  98. $self
  99. );
  100. break;
  101. case 'atom':
  102. header('Content-Type: application/atom+xml; charset=utf-8');
  103. $atom = new AtomNoticeFeed($this->auth_user);
  104. $atom->setId($id);
  105. $atom->setTitle($title);
  106. $atom->setUpdated('now');
  107. $atom->addLink($link);
  108. $atom->setSelfLink($self);
  109. $atom->addEntryFromNotices($this->notices);
  110. $this->raw($atom->getString());
  111. break;
  112. case 'json':
  113. $this->showJsonTimeline($this->notices);
  114. break;
  115. case 'as':
  116. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  117. $doc = new ActivityStreamJSONDocument($this->auth_user);
  118. $doc->setTitle($title);
  119. $doc->addLink($link, 'alternate', 'text/html');
  120. $doc->addItemsFromNotices($this->notices);
  121. $this->raw($doc->asString());
  122. break;
  123. default:
  124. // TRANS: Client error displayed when coming across a non-supported API method.
  125. $this->clientError(_('API method not found.'), $code = 404);
  126. break;
  127. }
  128. }
  129. /**
  130. * Return true if read only.
  131. *
  132. * MAY override
  133. *
  134. * @param array $args other arguments
  135. *
  136. * @return boolean is read only action?
  137. */
  138. function isReadOnly($args)
  139. {
  140. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  141. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147. /**
  148. * Return last modified, if applicable.
  149. *
  150. * MAY override
  151. *
  152. * @return string last modified http header
  153. */
  154. function lastModified()
  155. {
  156. if (!empty($this->notices) && (count($this->notices) > 0)) {
  157. return strtotime($this->notices[0]->created);
  158. }
  159. return null;
  160. }
  161. /**
  162. * Return etag, if applicable.
  163. *
  164. * MAY override
  165. *
  166. * @return string etag http header
  167. */
  168. function etag()
  169. {
  170. if (!empty($this->notices) && (count($this->notices) > 0)) {
  171. $last = count($this->notices) - 1;
  172. return '"' . implode(
  173. ':',
  174. array($this->arg('action'),
  175. common_user_cache_hash($this->auth_user),
  176. common_language(),
  177. $this->user->id,
  178. strtotime($this->notices[0]->created),
  179. strtotime($this->notices[$last]->created))
  180. )
  181. . '"';
  182. }
  183. return null;
  184. }
  185. /**
  186. * Does this require authentication?
  187. *
  188. * @return boolean true if delete, else false
  189. */
  190. function requiresAuth()
  191. {
  192. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  193. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  194. return false;
  195. } else {
  196. return true;
  197. }
  198. }
  199. }