atompubfavoritefeed.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Feed of ActivityStreams 'favorite' actions
  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 AtomPub
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  31. /**
  32. * Feed of ActivityStreams 'favorite' actions
  33. *
  34. * @category AtomPub
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @copyright 2010 StatusNet, Inc.
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  39. * @link http://status.net/
  40. */
  41. class AtompubfavoritefeedAction extends ApiAuthAction
  42. {
  43. private $_profile = null;
  44. private $_faves = null;
  45. protected function prepare(array $args=array())
  46. {
  47. parent::prepare($args);
  48. $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
  49. if (!$this->_profile instanceof Profile) {
  50. // TRANS: Client exception thrown when requesting a favorite feed for a non-existing profile.
  51. throw new ClientException(_('No such profile.'), 404);
  52. }
  53. $offset = ($this->page-1) * $this->count;
  54. $limit = $this->count + 1;
  55. $this->_faves = Fave::byProfile($this->_profile->id,
  56. $offset,
  57. $limit);
  58. return true;
  59. }
  60. protected function handle()
  61. {
  62. parent::handle();
  63. switch ($_SERVER['REQUEST_METHOD']) {
  64. case 'HEAD':
  65. case 'GET':
  66. $this->showFeed();
  67. break;
  68. case 'POST':
  69. $this->addFavorite();
  70. break;
  71. default:
  72. // TRANS: Client exception thrown when using an unsupported HTTP method.
  73. throw new ClientException(_('HTTP method not supported.'), 405);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Show a feed of favorite activity streams objects
  79. *
  80. * @return void
  81. */
  82. function showFeed()
  83. {
  84. header('Content-Type: application/atom+xml; charset=utf-8');
  85. $url = common_local_url('AtomPubFavoriteFeed',
  86. array('profile' => $this->_profile->id));
  87. $feed = new Atom10Feed(true);
  88. $feed->addNamespace('activity',
  89. 'http://activitystrea.ms/spec/1.0/');
  90. $feed->addNamespace('poco',
  91. 'http://portablecontacts.net/spec/1.0');
  92. $feed->addNamespace('media',
  93. 'http://purl.org/syndication/atommedia');
  94. $feed->id = $url;
  95. $feed->setUpdated('now');
  96. $feed->addAuthor($this->_profile->getBestName(),
  97. $this->_profile->getUri());
  98. // TRANS: Title for Atom favorites feed.
  99. // TRANS: %s is a user nickname.
  100. $feed->setTitle(sprintf(_("%s favorites"),
  101. $this->_profile->getBestName()));
  102. // TRANS: Subtitle for Atom favorites feed.
  103. // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename.
  104. $feed->setSubtitle(sprintf(_('Notices %1$s has favorited on %2$s'),
  105. $this->_profile->getBestName(),
  106. common_config('site', 'name')));
  107. $feed->addLink(common_local_url('showfavorites',
  108. array('nickname' =>
  109. $this->_profile->nickname)));
  110. $feed->addLink($url,
  111. array('rel' => 'self',
  112. 'type' => 'application/atom+xml'));
  113. // If there's more...
  114. if ($this->page > 1) {
  115. $feed->addLink($url,
  116. array('rel' => 'first',
  117. 'type' => 'application/atom+xml'));
  118. $feed->addLink(common_local_url('AtomPubFavoriteFeed',
  119. array('profile' =>
  120. $this->_profile->id),
  121. array('page' =>
  122. $this->page - 1)),
  123. array('rel' => 'prev',
  124. 'type' => 'application/atom+xml'));
  125. }
  126. if ($this->_faves->N > $this->count) {
  127. $feed->addLink(common_local_url('AtomPubFavoriteFeed',
  128. array('profile' =>
  129. $this->_profile->id),
  130. array('page' =>
  131. $this->page + 1)),
  132. array('rel' => 'next',
  133. 'type' => 'application/atom+xml'));
  134. }
  135. $i = 0;
  136. while ($this->_faves->fetch()) {
  137. // We get one more than needed; skip that one
  138. $i++;
  139. if ($i > $this->count) {
  140. break;
  141. }
  142. $act = $this->_faves->asActivity();
  143. $feed->addEntryRaw($act->asString(false, false, false));
  144. }
  145. $this->raw($feed->getString());
  146. }
  147. /**
  148. * add a new favorite
  149. *
  150. * @return void
  151. */
  152. function addFavorite()
  153. {
  154. // XXX: Refactor this; all the same for atompub
  155. if (!$this->scoped instanceof Profile ||
  156. $this->scoped->id != $this->_profile->id) {
  157. // TRANS: Client exception thrown when trying to set a favorite for another user.
  158. throw new ClientException(_("Cannot add someone else's subscription."), 403);
  159. }
  160. $xml = file_get_contents('php://input');
  161. $dom = DOMDocument::loadXML($xml);
  162. if ($dom->documentElement->namespaceURI != Activity::ATOM ||
  163. $dom->documentElement->localName != 'entry') {
  164. // TRANS: Client error displayed when not using an Atom entry.
  165. throw new ClientException(_('Atom post must be an Atom entry.'));
  166. }
  167. $activity = new Activity($dom->documentElement);
  168. $notice = null;
  169. // Favorite plugin handles these as ActivityHandlerPlugin through Notice->saveActivity
  170. // which in turn uses "StoreActivityObject" event.
  171. Event::handle('StartAtomPubNewActivity', array(&$activity, $this->scoped, &$notice));
  172. assert($notice instanceof Notice);
  173. $act = $notice->asActivity();
  174. header('Content-Type: application/atom+xml; charset=utf-8');
  175. header('Content-Location: ' . $act->selfLink);
  176. $this->startXML();
  177. $this->raw($act->asString(true, true, true));
  178. $this->endXML();
  179. }
  180. /**
  181. * Return true if read only.
  182. *
  183. * MAY override
  184. *
  185. * @param array $args other arguments
  186. *
  187. * @return boolean is read only action?
  188. */
  189. function isReadOnly($args)
  190. {
  191. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  192. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. /**
  199. * Return last modified, if applicable.
  200. *
  201. * MAY override
  202. *
  203. * @return string last modified http header
  204. */
  205. function lastModified()
  206. {
  207. // For comparison with If-Last-Modified
  208. // If not applicable, return null
  209. return null;
  210. }
  211. /**
  212. * Return etag, if applicable.
  213. *
  214. * MAY override
  215. *
  216. * @return string etag http header
  217. */
  218. function etag()
  219. {
  220. return null;
  221. }
  222. /**
  223. * Does this require authentication?
  224. *
  225. * @return boolean true if delete, else false
  226. */
  227. function requiresAuth()
  228. {
  229. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  230. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  231. return false;
  232. } else {
  233. return true;
  234. }
  235. }
  236. }