]> git.mxchange.org Git - friendica.git/commitdiff
Add order/limit building func and fix reverse order requirement on min_id
authorHank Grabowski <hankgrabowski@gmail.com>
Wed, 22 Feb 2023 16:27:54 +0000 (11:27 -0500)
committerHank Grabowski <hankgrabowski@gmail.com>
Wed, 22 Feb 2023 16:27:54 +0000 (11:27 -0500)
src/Module/Api/Mastodon/Timelines/Home.php
src/Module/BaseApi.php

index 2d646e428a2c2f9b7f42a6aaaaa87a515e56e517..f219f0a2c78fc853f59545300318425231e69837 100644 (file)
@@ -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`)"]);
index bdcced59f43b630907857c9bb6796fd8625c53f1..5f1156d7710e49d2bb9948b16396882aab0a4509 100644 (file)
@@ -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
         *