123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?php
- defined('GNUSOCIAL') || die();
- class ApiTimelineUserAction extends ApiBareAuthAction
- {
- public $notices = null;
- public $next_id = null;
-
- public function isReadOnly($args)
- {
- return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
- }
-
- public function lastModified()
- {
- if (!empty($this->notices) && (count($this->notices) > 0)) {
- return strtotime($this->notices[0]->created);
- }
- return null;
- }
-
- public function etag()
- {
- if (!empty($this->notices) && (count($this->notices) > 0)) {
- $last = count($this->notices) - 1;
- return '"' . implode(':', [
- $this->arg('action'),
- common_user_cache_hash($this->scoped),
- common_language(),
- $this->target->getID(),
- strtotime($this->notices[0]->created),
- strtotime($this->notices[$last]->created),
- ]) . '"';
- }
- return null;
- }
-
- protected function prepare(array $args = [])
- {
- parent::prepare($args);
- $this->target = $this->getTargetProfile($this->arg('id'));
- if (!($this->target instanceof Profile)) {
-
- $this->clientError(_('No such user.'), 404);
- }
- if (!$this->target->isLocal()) {
- $this->serverError(_('Remote user timelines are not available here yet.'), 501);
- }
- $this->notices = $this->getNotices();
- return true;
- }
-
- public function getNotices()
- {
- $notices = [];
- $notice = $this->target->getNotices(
- ($this->page - 1) * $this->count,
- $this->count + 1,
- $this->since_id,
- $this->max_id,
- $this->scoped
- );
- while ($notice->fetch()) {
- if (count($notices) < $this->count) {
- $notices[] = clone($notice);
- } else {
- $this->next_id = $notice->id;
- break;
- }
- }
- return $notices;
- }
-
- protected function handle()
- {
- parent::handle();
- if ($this->isPost()) {
- $this->handlePost();
- } else {
- $this->showTimeline();
- }
- }
- public function handlePost()
- {
- if (!$this->scoped instanceof Profile ||
- !$this->target->sameAs($this->scoped)) {
-
- $this->clientError(_('Only the user can add to their own timeline.'), 403);
- }
-
- if ($this->format != 'atom') {
-
- $this->clientError(_('Only accept AtomPub for Atom feeds.'));
- }
- $xml = trim(file_get_contents('php://input'));
- if (empty($xml)) {
-
- $this->clientError(_('Atom post must not be empty.'));
- }
- $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
- $dom = new DOMDocument();
- $ok = $dom->loadXML($xml);
- error_reporting($old);
- if (!$ok) {
-
- $this->clientError(_('Atom post must be well-formed XML.'));
- }
- if ($dom->documentElement->namespaceURI != Activity::ATOM ||
- $dom->documentElement->localName != 'entry') {
-
- $this->clientError(_('Atom post must be an Atom entry.'));
- }
- $activity = new Activity($dom->documentElement);
- common_debug('AtomPub: Ignoring right now, but this POST was made to collection: ' . $activity->id);
-
-
-
- $activity->id = null;
- $activity->actor = null;
- $activity->objects[0]->id = null;
- $stored = null;
- if (Event::handle('StartAtomPubNewActivity', array($activity, $this->target, &$stored))) {
-
- throw new ClientException(_('Could not handle this Atom Activity.'));
- }
- if (!$stored instanceof Notice) {
- throw new ServerException('Server did not create a Notice object from handled AtomPub activity.');
- }
- Event::handle('EndAtomPubNewActivity', array($activity, $this->target, $stored));
- http_response_code(201);
- header("Location: " . common_local_url('ApiStatusesShow', array('id' => $stored->getID(),
- 'format' => 'atom')));
- $this->showSingleAtomStatus($stored);
- }
-
- public function showTimeline()
- {
-
-
- $atom = new AtomUserNoticeFeed($this->target->getUser(), $this->scoped);
- $link = common_local_url(
- 'showstream',
- array('nickname' => $this->target->getNickname())
- );
- $self = $this->getSelfUri();
-
-
- $suplink = common_local_url('sup', null, null, $this->target->getID());
- header('X-SUP-ID: ' . $suplink);
-
- $nextUrl = !empty($this->next_id)
- ? common_local_url(
- 'ApiTimelineUser',
- array('format' => $this->format,
- 'id' => $this->target->getID()),
- array('max_id' => $this->next_id)
- )
- : null;
- $prevExtra = [];
- if (!empty($this->notices)) {
- assert($this->notices[0] instanceof Notice);
- $prevExtra['since_id'] = $this->notices[0]->id;
- }
- $prevUrl = common_local_url(
- 'ApiTimelineUser',
- array('format' => $this->format,
- 'id' => $this->target->getID()),
- $prevExtra
- );
- $firstUrl = common_local_url(
- 'ApiTimelineUser',
- array('format' => $this->format,
- 'id' => $this->target->getID())
- );
- switch ($this->format) {
- case 'xml':
- $this->showXmlTimeline($this->notices);
- break;
- case 'rss':
- $this->showRssTimeline(
- $this->notices,
- $atom->title,
- $link,
- $atom->subtitle,
- $suplink,
- $atom->logo,
- $self
- );
- break;
- case 'atom':
- header('Content-Type: application/atom+xml; charset=utf-8');
- $atom->setId($self);
- $atom->setSelfLink($self);
-
-
-
- if (!empty($this->next_id)) {
- $atom->addLink(
- $nextUrl,
- array('rel' => 'next',
- 'type' => 'application/atom+xml')
- );
- }
- if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
- $atom->addLink(
- $prevUrl,
- array('rel' => 'prev',
- 'type' => 'application/atom+xml')
- );
- }
- if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
- $atom->addLink(
- $firstUrl,
- array('rel' => 'first',
- 'type' => 'application/atom+xml')
- );
- }
- $atom->addEntryFromNotices($this->notices);
- $this->raw($atom->getString());
- break;
- case 'json':
- $this->showJsonTimeline($this->notices);
- break;
- case 'as':
- header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
- $doc = new ActivityStreamJSONDocument($this->scoped);
- $doc->setTitle($atom->title);
- $doc->addLink($link, 'alternate', 'text/html');
- $doc->addItemsFromNotices($this->notices);
- if (!empty($this->next_id)) {
- $doc->addLink(
- $nextUrl,
- array('rel' => 'next',
- 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
- );
- }
- if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
- $doc->addLink(
- $prevUrl,
- array('rel' => 'prev',
- 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
- );
- }
- if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
- $doc->addLink(
- $firstUrl,
- array('rel' => 'first',
- 'type' => ActivityStreamJSONDocument::CONTENT_TYPE)
- );
- }
- $this->raw($doc->asString());
- break;
- default:
-
- $this->clientError(_('API method not found.'), 404);
- }
- }
- }
|