]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/BaseApi.php
Channels: Larger fields, better error handling
[friendica.git] / src / Module / BaseApi.php
index 82f0ad05968e87751648ed68eb0c92796ac8990f..c73f90aba62676a1eecd7f05c483d74b4bac9550 100644 (file)
@@ -37,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\Status;
 use Friendica\Object\Api\Mastodon\TimelineOrderByTypes;
 use Friendica\Security\BasicAuth;
 use Friendica\Security\OAuth;
@@ -129,12 +130,17 @@ class BaseApi extends BaseModule
                        }
                } else {
                        switch ($requested_order) {
+                               case TimelineOrderByTypes::RECEIVED:
+                               case TimelineOrderByTypes::CHANGED:
+                               case TimelineOrderByTypes::EDITED:
                                case TimelineOrderByTypes::CREATED:
-                                       $order_field = 'created';
+                               case TimelineOrderByTypes::COMMENTED:
+                                       $order_field = $requested_order;
                                        break;
                                default:
-                                       $order_field = 'uri-id';
+                                       throw new \Exception("Unrecognized request order: $requested_order");
                        }
+
                        if (!empty($request['max_id'])) {
                                $condition = DBA::mergeConditions($condition, ["`$order_field` < ?", DateTimeFormat::convert($request['max_id'], DateTimeFormat::MYSQL)]);
                        }
@@ -163,8 +169,12 @@ class BaseApi extends BaseModule
        {
                $requested_order = $request['friendica_order'];
                switch ($requested_order) {
+                       case TimelineOrderByTypes::CHANGED:
                        case TimelineOrderByTypes::CREATED:
-                               $order_field = 'created';
+                       case TimelineOrderByTypes::COMMENTED:
+                       case TimelineOrderByTypes::EDITED:
+                       case TimelineOrderByTypes::RECEIVED:
+                               $order_field = $requested_order;
                                break;
                        case TimelineOrderByTypes::ID:
                        default:
@@ -177,11 +187,58 @@ class BaseApi extends BaseModule
                        $params['order'] = [$order_field => true];
                }
 
-               $params['limit']= $request['limit'];
+               $params['limit'] = $request['limit'];
 
                return $params;
        }
 
+       /**
+        * Update the ID/time boundaries for this result set. Used for building Link Headers
+        *
+        * @param Status $status
+        * @param array $post_item
+        * @param string $order
+        * @return void
+        * @throws \Exception
+        */
+       protected function updateBoundaries(Status $status, array $post_item, string $order)
+       {
+               try {
+                       switch ($order) {
+                               case TimelineOrderByTypes::CHANGED:
+                                       if (!empty($status->friendicaExtension()->changedAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->friendicaExtension()->changedAt(), DateTimeFormat::JSON)));
+                                       }
+                                       break;
+                               case TimelineOrderByTypes::CREATED:
+                                       if (!empty($status->createdAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->createdAt(), DateTimeFormat::JSON)));
+                                       }
+                                       break;
+                               case TimelineOrderByTypes::COMMENTED:
+                                       if (!empty($status->friendicaExtension()->commentedAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->friendicaExtension()->commentedAt(), DateTimeFormat::JSON)));
+                                       }
+                                       break;
+                               case TimelineOrderByTypes::EDITED:
+                                       if (!empty($status->editedAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->editedAt(), DateTimeFormat::JSON)));
+                                       }
+                                       break;
+                               case TimelineOrderByTypes::RECEIVED:
+                                       if (!empty($status->friendicaExtension()->receivedAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->friendicaExtension()->receivedAt(), DateTimeFormat::JSON)));
+                                       }
+                                       break;
+                               case TimelineOrderByTypes::ID:
+                               default:
+                                       self::setBoundaries($post_item['uri-id']);
+                       }
+               } catch (\Exception $e) {
+                       Logger::debug('Error processing page boundary calculation, skipping', ['error' => $e]);
+               }
+       }
+
        /**
         * Processes data from GET requests and sets defaults
         *
@@ -224,7 +281,7 @@ class BaseApi extends BaseModule
         * Get the "link" header with "next" and "prev" links
         * @return string
         */
-       protected static function getLinkHeader(bool $asDate): string
+       protected static function getLinkHeader(bool $asDate = false): string
        {
                if (empty(self::$boundaries)) {
                        return '';
@@ -315,7 +372,7 @@ class BaseApi extends BaseModule
         */
        public static function appSupportsQuotes(): bool
        {
-               $token = self::getCurrentApplication();
+               $token = OAuth::getCurrentApplicationToken();
                return (!empty($token['name']) && in_array($token['name'], ['Fedilab']));
        }
 
@@ -377,7 +434,7 @@ class BaseApi extends BaseModule
                }
        }
 
-       public static function checkThrottleLimit()
+       public function checkThrottleLimit()
        {
                $uid = self::getCurrentUserID();
 
@@ -390,11 +447,11 @@ class BaseApi extends BaseModule
                        $posts_day = Post::countThread($condition);
 
                        if ($posts_day > $throttle_day) {
-                               Logger::notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
+                               $this->logger->notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
 
@@ -407,10 +464,10 @@ class BaseApi extends BaseModule
 
                        if ($posts_week > $throttle_week) {
                                Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
 
@@ -423,10 +480,10 @@ class BaseApi extends BaseModule
 
                        if ($posts_month > $throttle_month) {
                                Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
        }