]> git.mxchange.org Git - friendica.git/commitdiff
Implement time based paging for Mastodon Home Timeline Endpoint
authorHank Grabowski <hankgrabowski@gmail.com>
Tue, 21 Feb 2023 20:37:32 +0000 (15:37 -0500)
committerHank Grabowski <hankgrabowski@gmail.com>
Tue, 21 Feb 2023 20:37:32 +0000 (15:37 -0500)
src/Module/Api/Mastodon/Timelines/Home.php
src/Module/BaseApi.php
src/Object/Api/Mastodon/Status.php
src/Object/Api/Mastodon/TimelineOrderByTypes.php [new file with mode: 0644]

index 5efaf4930ebbe380c26c38e857917b152f0f4040..379e175d453a9040ce8e12bf6fec266e10083f79 100644 (file)
@@ -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);
        }
 }
index f8524b524ee48c694f2532515aab0cdfe06e3e71..1deb81735ab2b8bcf5a4ab1f40461d72b9844ef7 100644 (file)
 
 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);
                }
index 30f554bdf5d61433cba6936a43e0a4d61761a2d4..3fbd009a6efb74d419706e5b3d84fac57f989506 100644 (file)
@@ -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 (file)
index 0000000..a42d2c0
--- /dev/null
@@ -0,0 +1,32 @@
+<?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';
+}