apiconversation.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /**
  50. * For initializing members of the class.
  51. *
  52. * @param array $argarray misc. arguments
  53. *
  54. * @return boolean true
  55. */
  56. function prepare($argarray)
  57. {
  58. parent::prepare($argarray);
  59. $convId = $this->trimmed('id');
  60. if (empty($convId)) {
  61. // TRANS: Client exception thrown when no conversation ID is given.
  62. throw new ClientException(_('No conversation ID.'));
  63. }
  64. $this->conversation = Conversation::getKV('id', $convId);
  65. if (empty($this->conversation)) {
  66. // TRANS: Client exception thrown when referring to a non-existing conversation ID (%d).
  67. throw new ClientException(sprintf(_('No conversation with ID %d.'), $convId),
  68. 404);
  69. }
  70. $stream = new ConversationNoticeStream($convId, $this->scoped);
  71. $notice = $stream->getNotices(($this->page-1) * $this->count,
  72. $this->count,
  73. $this->since_id,
  74. $this->max_id);
  75. $this->notices = $notice->fetchAll();
  76. return true;
  77. }
  78. /**
  79. * Handler method
  80. *
  81. * @param array $argarray is ignored since it's now passed in in prepare()
  82. *
  83. * @return void
  84. */
  85. function handle($argarray=null)
  86. {
  87. $sitename = common_config('site', 'name');
  88. // TRANS: Title for conversion timeline.
  89. $title = _m('TITLE', 'Conversation');
  90. $id = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
  91. $link = common_local_url('conversation', array('id' => $this->conversation->id));
  92. $self = $id;
  93. switch($this->format) {
  94. case 'xml':
  95. $this->showXmlTimeline($this->notices);
  96. break;
  97. case 'rss':
  98. $this->showRssTimeline(
  99. $this->notices,
  100. $title,
  101. $link,
  102. null,
  103. null,
  104. null,
  105. $self
  106. );
  107. break;
  108. case 'atom':
  109. header('Content-Type: application/atom+xml; charset=utf-8');
  110. $atom = new AtomNoticeFeed($this->auth_user);
  111. $atom->setId($id);
  112. $atom->setTitle($title);
  113. $atom->setUpdated('now');
  114. $atom->addLink($link);
  115. $atom->setSelfLink($self);
  116. $atom->addEntryFromNotices($this->notices);
  117. $this->raw($atom->getString());
  118. break;
  119. case 'json':
  120. $this->showJsonTimeline($this->notices);
  121. break;
  122. case 'as':
  123. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  124. $doc = new ActivityStreamJSONDocument($this->auth_user);
  125. $doc->setTitle($title);
  126. $doc->addLink($link, 'alternate', 'text/html');
  127. $doc->addItemsFromNotices($this->notices);
  128. $this->raw($doc->asString());
  129. break;
  130. default:
  131. // TRANS: Client error displayed when coming across a non-supported API method.
  132. $this->clientError(_('API method not found.'), $code = 404);
  133. break;
  134. }
  135. }
  136. /**
  137. * Return true if read only.
  138. *
  139. * MAY override
  140. *
  141. * @param array $args other arguments
  142. *
  143. * @return boolean is read only action?
  144. */
  145. function isReadOnly($args)
  146. {
  147. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  148. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. /**
  155. * Return last modified, if applicable.
  156. *
  157. * MAY override
  158. *
  159. * @return string last modified http header
  160. */
  161. function lastModified()
  162. {
  163. if (!empty($this->notices) && (count($this->notices) > 0)) {
  164. return strtotime($this->notices[0]->created);
  165. }
  166. return null;
  167. }
  168. /**
  169. * Return etag, if applicable.
  170. *
  171. * MAY override
  172. *
  173. * @return string etag http header
  174. */
  175. function etag()
  176. {
  177. if (!empty($this->notices) && (count($this->notices) > 0)) {
  178. $last = count($this->notices) - 1;
  179. return '"' . implode(
  180. ':',
  181. array($this->arg('action'),
  182. common_user_cache_hash($this->auth_user),
  183. common_language(),
  184. $this->user->id,
  185. strtotime($this->notices[0]->created),
  186. strtotime($this->notices[$last]->created))
  187. )
  188. . '"';
  189. }
  190. return null;
  191. }
  192. /**
  193. * Does this require authentication?
  194. *
  195. * @return boolean true if delete, else false
  196. */
  197. function requiresAuth()
  198. {
  199. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  200. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  201. return false;
  202. } else {
  203. return true;
  204. }
  205. }
  206. }