]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/BaseApi.php
Channels: Larger fields, better error handling
[friendica.git] / src / Module / BaseApi.php
index 3b940e3ae3af14c2df4b92103be3efd7e9ad5fe3..c73f90aba62676a1eecd7f05c483d74b4bac9550 100644 (file)
@@ -129,7 +129,18 @@ class BaseApi extends BaseModule
                                $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", intval($request['min_id'])]);
                        }
                } else {
-                       $order_field = $requested_order;
+                       switch ($requested_order) {
+                               case TimelineOrderByTypes::RECEIVED:
+                               case TimelineOrderByTypes::CHANGED:
+                               case TimelineOrderByTypes::EDITED:
+                               case TimelineOrderByTypes::CREATED:
+                               case TimelineOrderByTypes::COMMENTED:
+                                       $order_field = $requested_order;
+                                       break;
+                               default:
+                                       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)]);
                        }
@@ -159,19 +170,11 @@ class BaseApi extends BaseModule
                $requested_order = $request['friendica_order'];
                switch ($requested_order) {
                        case TimelineOrderByTypes::CHANGED:
-                               $order_field = 'changed';
-                               break;
                        case TimelineOrderByTypes::CREATED:
-                               $order_field = 'created';
-                               break;
                        case TimelineOrderByTypes::COMMENTED:
-                               $order_field = 'commented';
-                               break;
                        case TimelineOrderByTypes::EDITED:
-                               $order_field = 'edited';
-                               break;
                        case TimelineOrderByTypes::RECEIVED:
-                               $order_field = 'received';
+                               $order_field = $requested_order;
                                break;
                        case TimelineOrderByTypes::ID:
                        default:
@@ -218,8 +221,8 @@ class BaseApi extends BaseModule
                                        }
                                        break;
                                case TimelineOrderByTypes::EDITED:
-                                       if (!empty($status->friendicaExtension()->editedAt())) {
-                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->friendicaExtension()->editedAt(), DateTimeFormat::JSON)));
+                                       if (!empty($status->editedAt())) {
+                                               self::setBoundaries(new DateTime(DateTimeFormat::utc($status->editedAt(), DateTimeFormat::JSON)));
                                        }
                                        break;
                                case TimelineOrderByTypes::RECEIVED:
@@ -278,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 '';
@@ -369,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']));
        }
 
@@ -431,7 +434,7 @@ class BaseApi extends BaseModule
                }
        }
 
-       public static function checkThrottleLimit()
+       public function checkThrottleLimit()
        {
                $uid = self::getCurrentUserID();
 
@@ -444,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());
                        }
                }
 
@@ -461,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());
                        }
                }
 
@@ -477,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());
                        }
                }
        }