apitimelineuser.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Show a user's timeline
  18. *
  19. * @category API
  20. * @package GNUsocial
  21. * @author Craig Andrews <candrews@integralblue.com>
  22. * @author Evan Prodromou <evan@status.net>
  23. * @author Jeffery To <jeffery.to@gmail.com>
  24. * @author mac65 <mac65@mac65.com>
  25. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  26. * @author Robin Millette <robin@millette.info>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. defined('GNUSOCIAL') || die();
  33. /**
  34. * Returns the most recent notices (default 20) posted by the authenticating
  35. * user. Another user's timeline can be requested via the id parameter. This
  36. * is the API equivalent of the user profile web page.
  37. *
  38. * @category API
  39. * @package GNUsocial
  40. * @author Craig Andrews <candrews@integralblue.com>
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Jeffery To <jeffery.to@gmail.com>
  43. * @author mac65 <mac65@mac65.com>
  44. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  45. * @author Robin Millette <robin@millette.info>
  46. * @author Zach Copley <zach@status.net>
  47. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  48. */
  49. class ApiTimelineUserAction extends ApiBareAuthAction
  50. {
  51. public $notices = null;
  52. public $next_id = null;
  53. /**
  54. * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
  55. *
  56. * @param array $args other arguments
  57. *
  58. * @return boolean true
  59. */
  60. public function isReadOnly($args)
  61. {
  62. return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
  63. }
  64. /**
  65. * When was this feed last modified?
  66. *
  67. * @return string datestamp of the latest notice in the stream
  68. */
  69. public function lastModified()
  70. {
  71. if (!empty($this->notices) && (count($this->notices) > 0)) {
  72. return strtotime($this->notices[0]->created);
  73. }
  74. return null;
  75. }
  76. /**
  77. * An entity tag for this stream
  78. *
  79. * Returns an Etag based on the action name, language, user ID, and
  80. * timestamps of the first and last notice in the timeline
  81. *
  82. * @return string etag
  83. */
  84. public function etag()
  85. {
  86. if (!empty($this->notices) && (count($this->notices) > 0)) {
  87. $last = count($this->notices) - 1;
  88. return '"' . implode(':', [
  89. $this->arg('action'),
  90. common_user_cache_hash($this->scoped),
  91. common_language(),
  92. $this->target->getID(),
  93. strtotime($this->notices[0]->created),
  94. strtotime($this->notices[$last]->created),
  95. ]) . '"';
  96. }
  97. return null;
  98. }
  99. /**
  100. * Take arguments for running
  101. *
  102. * @param array $args $_REQUEST args
  103. *
  104. * @return boolean success flag
  105. * @throws AuthorizationException
  106. * @throws ClientException
  107. */
  108. protected function prepare(array $args = [])
  109. {
  110. parent::prepare($args);
  111. $this->target = $this->getTargetProfile($this->arg('id'));
  112. if (!($this->target instanceof Profile)) {
  113. // TRANS: Client error displayed requesting most recent notices for a non-existing user.
  114. $this->clientError(_('No such user.'), 404);
  115. }
  116. if (!$this->target->isLocal()) {
  117. $this->serverError(_('Remote user timelines are not available here yet.'), 501);
  118. }
  119. $this->notices = $this->getNotices();
  120. return true;
  121. }
  122. /**
  123. * Get notices
  124. *
  125. * @return array notices
  126. */
  127. public function getNotices()
  128. {
  129. $notices = [];
  130. $notice = $this->target->getNotices(
  131. ($this->page - 1) * $this->count,
  132. $this->count + 1,
  133. $this->since_id,
  134. $this->max_id,
  135. $this->scoped
  136. );
  137. while ($notice->fetch()) {
  138. if (count($notices) < $this->count) {
  139. $notices[] = clone($notice);
  140. } else {
  141. $this->next_id = $notice->id;
  142. break;
  143. }
  144. }
  145. return $notices;
  146. }
  147. /**
  148. * Handle the request
  149. *
  150. * Just show the notices
  151. *
  152. * @return void
  153. * @throws ClientException
  154. * @throws ServerException
  155. */
  156. protected function handle()
  157. {
  158. parent::handle();
  159. if ($this->isPost()) {
  160. $this->handlePost();
  161. } else {
  162. $this->showTimeline();
  163. }
  164. }
  165. public function handlePost()
  166. {
  167. if (!$this->scoped instanceof Profile ||
  168. !$this->target->sameAs($this->scoped)) {
  169. // TRANS: Client error displayed trying to add a notice to another user's timeline.
  170. $this->clientError(_('Only the user can add to their own timeline.'), 403);
  171. }
  172. // Only handle posts for Atom
  173. if ($this->format != 'atom') {
  174. // TRANS: Client error displayed when using another format than AtomPub.
  175. $this->clientError(_('Only accept AtomPub for Atom feeds.'));
  176. }
  177. $xml = trim(file_get_contents('php://input'));
  178. if (empty($xml)) {
  179. // TRANS: Client error displayed attempting to post an empty API notice.
  180. $this->clientError(_('Atom post must not be empty.'));
  181. }
  182. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
  183. $dom = new DOMDocument();
  184. $ok = $dom->loadXML($xml);
  185. error_reporting($old);
  186. if (!$ok) {
  187. // TRANS: Client error displayed attempting to post an API that is not well-formed XML.
  188. $this->clientError(_('Atom post must be well-formed XML.'));
  189. }
  190. if ($dom->documentElement->namespaceURI != Activity::ATOM ||
  191. $dom->documentElement->localName != 'entry') {
  192. // TRANS: Client error displayed when not using an Atom entry.
  193. $this->clientError(_('Atom post must be an Atom entry.'));
  194. }
  195. $activity = new Activity($dom->documentElement);
  196. common_debug('AtomPub: Ignoring right now, but this POST was made to collection: ' . $activity->id);
  197. // Reset activity data so we can handle it in the same functions as with OStatus
  198. // because we don't let clients set their own UUIDs... Not sure what AtomPub thinks
  199. // about that though.
  200. $activity->id = null;
  201. $activity->actor = null; // not used anyway, we use $this->target
  202. $activity->objects[0]->id = null;
  203. $stored = null;
  204. if (Event::handle('StartAtomPubNewActivity', array($activity, $this->target, &$stored))) {
  205. // TRANS: Client error displayed when not using the POST verb. Do not translate POST.
  206. throw new ClientException(_('Could not handle this Atom Activity.'));
  207. }
  208. if (!$stored instanceof Notice) {
  209. throw new ServerException('Server did not create a Notice object from handled AtomPub activity.');
  210. }
  211. Event::handle('EndAtomPubNewActivity', array($activity, $this->target, $stored));
  212. http_response_code(201);
  213. header("Location: " . common_local_url('ApiStatusesShow', array('id' => $stored->getID(),
  214. 'format' => 'atom')));
  215. $this->showSingleAtomStatus($stored);
  216. }
  217. /**
  218. * Show the timeline of notices
  219. *
  220. * @return void
  221. * @throws ClientException
  222. * @throws ServerException
  223. * @throws UserNoProfileException
  224. */
  225. public function showTimeline()
  226. {
  227. // We'll use the shared params from the Atom stub
  228. // for other feed types.
  229. $atom = new AtomUserNoticeFeed($this->target->getUser(), $this->scoped);
  230. $link = common_local_url(
  231. 'showstream',
  232. array('nickname' => $this->target->getNickname())
  233. );
  234. $self = $this->getSelfUri();
  235. // FriendFeed's SUP protocol
  236. // Also added RSS and Atom feeds
  237. $suplink = common_local_url('sup', null, null, $this->target->getID());
  238. header('X-SUP-ID: ' . $suplink);
  239. // paging links
  240. $nextUrl = !empty($this->next_id)
  241. ? common_local_url(
  242. 'ApiTimelineUser',
  243. array('format' => $this->format,
  244. 'id' => $this->target->getID()),
  245. array('max_id' => $this->next_id)
  246. )
  247. : null;
  248. $prevExtra = [];
  249. if (!empty($this->notices)) {
  250. assert($this->notices[0] instanceof Notice);
  251. $prevExtra['since_id'] = $this->notices[0]->id;
  252. }
  253. $prevUrl = common_local_url(
  254. 'ApiTimelineUser',
  255. array('format' => $this->format,
  256. 'id' => $this->target->getID()),
  257. $prevExtra
  258. );
  259. $firstUrl = common_local_url(
  260. 'ApiTimelineUser',
  261. array('format' => $this->format,
  262. 'id' => $this->target->getID())
  263. );
  264. switch ($this->format) {
  265. case 'xml':
  266. $this->showXmlTimeline($this->notices);
  267. break;
  268. case 'rss':
  269. $this->showRssTimeline(
  270. $this->notices,
  271. $atom->title,
  272. $link,
  273. $atom->subtitle,
  274. $suplink,
  275. $atom->logo,
  276. $self
  277. );
  278. break;
  279. case 'atom':
  280. header('Content-Type: application/atom+xml; charset=utf-8');
  281. $atom->setId($self);
  282. $atom->setSelfLink($self);
  283. // Add navigation links: next, prev, first
  284. // Note: we use IDs rather than pages for navigation; page boundaries
  285. // change too quickly!
  286. if (!empty($this->next_id)) {
  287. $atom->addLink(
  288. $nextUrl,
  289. array('rel' => 'next',
  290. 'type' => 'application/atom+xml')
  291. );
  292. }
  293. if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
  294. $atom->addLink(
  295. $prevUrl,
  296. array('rel' => 'prev',
  297. 'type' => 'application/atom+xml')
  298. );
  299. }
  300. if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
  301. $atom->addLink(
  302. $firstUrl,
  303. array('rel' => 'first',
  304. 'type' => 'application/atom+xml')
  305. );
  306. }
  307. $atom->addEntryFromNotices($this->notices);
  308. $this->raw($atom->getString());
  309. break;
  310. case 'json':
  311. $this->showJsonTimeline($this->notices);
  312. break;
  313. case 'as':
  314. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  315. $doc = new ActivityStreamJSONDocument($this->scoped);
  316. $doc->setTitle($atom->title);
  317. $doc->addLink($link, 'alternate', 'text/html');
  318. $doc->addItemsFromNotices($this->notices);
  319. if (!empty($this->next_id)) {
  320. $doc->addLink(
  321. $nextUrl,
  322. array('rel' => 'next',
  323. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
  324. );
  325. }
  326. if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
  327. $doc->addLink(
  328. $prevUrl,
  329. array('rel' => 'prev',
  330. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
  331. );
  332. }
  333. if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
  334. $doc->addLink(
  335. $firstUrl,
  336. array('rel' => 'first',
  337. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
  338. );
  339. }
  340. $this->raw($doc->asString());
  341. break;
  342. default:
  343. // TRANS: Client error displayed when coming across a non-supported API method.
  344. $this->clientError(_('API method not found.'), 404);
  345. }
  346. }
  347. }