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/
'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]);
$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]);
}
$statuses = array_reverse($statuses);
}
- self::setLinkHeader();
+
+ self::setLinkHeader($request['friendica_order'] != TimelineOrderByTypes::ID);
System::jsonExit($statuses);
}
}
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;
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;
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
*
* 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 '';
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();
* 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);
}
--- /dev/null
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace Friendica\Object\Api\Mastodon;
+
+/**
+ * Enumeration of order types that can be requested
+ */
+abstract class TimelineOrderByTypes
+{
+ const ID = 'id';
+ const CREATED_AT = 'created_at';
+}