From a67fe069e88a497b9d07c6b147b1279f21c7dad5 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Wed, 22 Feb 2023 11:27:54 -0500 Subject: [PATCH] Add order/limit building func and fix reverse order requirement on min_id --- src/Module/Api/Mastodon/Timelines/Home.php | 3 +- src/Module/BaseApi.php | 40 +++++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index 2d646e428..f219f0a2c 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -57,11 +57,12 @@ class Home extends BaseApi '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); + $params = $this->buildOrderAndLimitParams($request); if ($request['local']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index bdcced59f..5f1156d77 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -105,12 +105,11 @@ class BaseApi extends BaseModule } /** - * Processes data from GET requests and sets defaults + * Processes data from GET requests and sets paging conditions * - * @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 + * @param array $request Custom REQUEST array + * @param array $condition Existing conditions to merge + * @return array paging data condition parameters data * @throws \Exception */ public function addPagingConditions(array $request, array $condition): array @@ -151,6 +150,37 @@ class BaseApi extends BaseModule return $condition; } + /** + * Processes data from GET requests and sets paging conditions + * + * @param array $request Custom REQUEST array + * @param array $params Existing $params element to build on + * @return array ordering data added to the params blocks that was passed in + * @throws \Exception + */ + public function buildOrderAndLimitParams(array $request, array $params = []): array + { + $requested_order = $request['friendica_order']; + switch ($requested_order) { + case TimelineOrderByTypes::CREATED: + $order_field = 'created'; + break; + case TimelineOrderByTypes::ID: + default: + $order_field = 'uri-id'; + } + + if(!empty($request['min_id'])) { + $params['order'] = [$order_field]; + } else { + $params['order'] = [$order_field => true]; + } + + $params['limit']= $request['limit']; + + return $params; + } + /** * Processes data from GET requests and sets defaults *