diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index 5efaf4930e..379e175d45 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -29,6 +29,7 @@ use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; +use Friendica\Object\Api\Mastodon\TimelineOrderByTypes; /** * @see https://docs.joinmastodon.org/methods/timelines/ @@ -53,30 +54,21 @@ class Home extends BaseApi 'only_media' => false, // Show only statuses with media attached? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. 'exclude_replies' => false, // Don't show comments + 'min_time' => '', + 'max_time' => '', + 'friendica_order' => TimelineOrderByTypes::ID, ], $request); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'uid' => $uid]; + $condition = $this->addPagingConditions($request, $condition); + if ($request['local']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } - if (!empty($request['max_id'])) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); - } - - if (!empty($request['since_id'])) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); - } - - if (!empty($request['min_id'])) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); - - $params['order'] = ['uri-id']; - } - if ($request['only_media']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); @@ -96,9 +88,17 @@ class Home extends BaseApi $statuses = []; while ($item = Post::fetch($items)) { - self::setBoundaries($item['uri-id']); try { - $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); + $status = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); + switch ($request['friendica_order']) { + case TimelineOrderByTypes::CREATED_AT: + self::setBoundaries($status->createdAtTimestamp()); + break; + case TimelineOrderByTypes::ID: + default: + self::setBoundaries($item['uri-id']); + } + $statuses[] = $status; } catch (\Throwable $th) { Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); } @@ -109,7 +109,8 @@ class Home extends BaseApi $statuses = array_reverse($statuses); } - self::setLinkHeader(); + + self::setLinkHeader($request['friendica_order'] != TimelineOrderByTypes::ID); System::jsonExit($statuses); } } diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index f8524b524e..1deb81735a 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -21,12 +21,14 @@ namespace Friendica\Module; +use DateTime; use Friendica\App; use Friendica\App\Router; use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\System; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Item; @@ -35,6 +37,7 @@ use Friendica\Model\User; use Friendica\Module\Api\ApiResponse; use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Network\HTTPException; +use Friendica\Object\Api\Mastodon\TimelineOrderByTypes; use Friendica\Security\BasicAuth; use Friendica\Security\OAuth; use Friendica\Util\DateTimeFormat; @@ -101,6 +104,53 @@ class BaseApi extends BaseModule return parent::run($httpException, $request); } + /** + * Processes data from GET requests and sets defaults + * + * @param array $defaults Associative array of expected request keys and their default typed value. A null + * value will remove the request key from the resulting value array. + * @param array $request Custom REQUEST array, superglobal instead + * @return array request data + * @throws \Exception + */ + public function addPagingConditions(array $request, array $condition): array + { + $requested_order = $request['friendica_order']; + if ($requested_order == TimelineOrderByTypes::ID) { + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); + } + + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); + } + + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); + } + } else { + switch ($requested_order) { + case TimelineOrderByTypes::CREATED_AT: + $order_field = 'created'; + break; + default: + $order_field = 'uri-id'; + } + if (!empty($request['max_time'])) { + $condition = DBA::mergeConditions($condition, ["`$order_field` < ?", DateTimeFormat::convert($request['max_time'], DateTimeFormat::MYSQL)]); + } + + + if (!empty($request['min_time'])) { + $condition = DBA::mergeConditions($condition, ["`$order_field` > ?", DateTimeFormat::convert($request['min_time'], DateTimeFormat::MYSQL)]); + + $params['order'] = [$order_field]; + } + } + + return $condition; + } + /** * Processes data from GET requests and sets defaults * @@ -143,7 +193,7 @@ class BaseApi extends BaseModule * Get the "link" header with "next" and "prev" links * @return string */ - protected static function getLinkHeader(): string + protected static function getLinkHeader(bool $asDate): string { if (empty(self::$boundaries)) { return ''; @@ -154,11 +204,22 @@ class BaseApi extends BaseModule unset($request['min_id']); unset($request['max_id']); unset($request['since_id']); + unset($request['min_time']); + unset($request['max_time']); $prev_request = $next_request = $request; - $prev_request['min_id'] = self::$boundaries['max']; - $next_request['max_id'] = self::$boundaries['min']; + if ($asDate) { + $max_date = new DateTime(); + $max_date->setTimestamp(self::$boundaries['max']); + $min_date = new DateTime(); + $min_date->setTimestamp(self::$boundaries['min']); + $prev_request['min_time'] = $max_date->format(DateTimeFormat::JSON); + $next_request['max_time'] = $min_date->format(DateTimeFormat::JSON); + } else { + $prev_request['min_id'] = self::$boundaries['max']; + $next_request['max_id'] = self::$boundaries['min']; + } $command = DI::baseUrl() . '/' . DI::args()->getCommand(); @@ -200,9 +261,9 @@ class BaseApi extends BaseModule * Set the "link" header with "next" and "prev" links * @return void */ - protected static function setLinkHeader() + protected static function setLinkHeader(bool $asDate = false) { - $header = self::getLinkHeader(); + $header = self::getLinkHeader($asDate); if (!empty($header)) { header($header); } diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 30f554bdf5..3fbd009a6e 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -154,6 +154,21 @@ class Status extends BaseDataTransferObject $this->friendica = new FriendicaExtension($item['title'], $counts->dislikes); } + /** + * Returns the current created_at DateTime as an integer timestamp + * @return int + * @throws \Exception + */ + public function createdAtTimestamp(): int + { + $result = strtotime($this->created_at); + if (!$result) { + throw new \Exception('Unknown date-time format'); + } + + return $result; + } + /** * Returns the current entity as an array * diff --git a/src/Object/Api/Mastodon/TimelineOrderByTypes.php b/src/Object/Api/Mastodon/TimelineOrderByTypes.php new file mode 100644 index 0000000000..a42d2c0605 --- /dev/null +++ b/src/Object/Api/Mastodon/TimelineOrderByTypes.php @@ -0,0 +1,32 @@ +. + * + */ + + +namespace Friendica\Object\Api\Mastodon; + +/** + * Enumeration of order types that can be requested + */ +abstract class TimelineOrderByTypes +{ + const ID = 'id'; + const CREATED_AT = 'created_at'; +}