From 46180d7d5b27e7544c2bf8a258857112cf44a3a0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 Sep 2023 12:27:15 -0400 Subject: [PATCH] Move System::jsonError to BaseModule->jsonError - This will ensure headers set in BaseModule->run will be carried in jsonError scenarios - Make BaseApi->checkThrottleLimit an object method to use BaseModule->jsonError - Deprecate jsonError() method in Core\System --- src/BaseModule.php | 19 +++++++++++++++++++ src/Core/System.php | 3 +++ src/Factory/Api/Mastodon/Error.php | 10 +++++----- src/Module/Api/Mastodon/Proofs.php | 2 +- src/Module/Api/Mastodon/Statuses.php | 2 +- src/Module/Api/Twitter/Statuses/Update.php | 2 +- src/Module/BaseApi.php | 22 +++++++++++----------- src/Module/Circle.php | 2 +- src/Module/Friendica.php | 2 +- src/Module/NoScrape.php | 4 ++-- src/Module/Profile/Profile.php | 6 +++--- 11 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/BaseModule.php b/src/BaseModule.php index 463954f000..0e645edf6a 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -510,4 +510,23 @@ abstract class BaseModule implements ICanHandleRequests { $this->httpExit(json_encode($content, $options), ICanCreateResponses::TYPE_JSON, $content_type); } + + /** + * Display a non-200 HTTP code response using JSON to encode the content and exit + * + * @param int $httpCode + * @param mixed $content + * @param string $content_type + * @return void + * @throws HTTPException\InternalServerErrorException + */ + public function jsonError(int $httpCode, $content, string $content_type = 'application/json') + { + if ($httpCode >= 400) { + $this->logger->debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']); + } + + $this->response->setStatus($httpCode); + $this->jsonExit($content, $content_type); + } } diff --git a/src/Core/System.php b/src/Core/System.php index 6af4186a37..89363349cf 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -373,6 +373,9 @@ class System self::exit(); } + /** + * @deprecated since 2023.09 Use BaseModule->jsonError instead + */ public static function jsonError($httpCode, $content, $content_type = 'application/json') { if ($httpCode >= 400) { diff --git a/src/Factory/Api/Mastodon/Error.php b/src/Factory/Api/Mastodon/Error.php index 32ca03a62a..68172d6323 100644 --- a/src/Factory/Api/Mastodon/Error.php +++ b/src/Factory/Api/Mastodon/Error.php @@ -57,7 +57,7 @@ class Error extends BaseFactory $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); $this->logError(404, $error); - System::jsonError(404, $errorObj->toArray()); + $this->jsonError(404, $errorObj->toArray()); } public function UnprocessableEntity(string $error = '') @@ -67,7 +67,7 @@ class Error extends BaseFactory $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); $this->logError(422, $error); - System::jsonError(422, $errorObj->toArray()); + $this->jsonError(422, $errorObj->toArray()); } public function Unauthorized(string $error = '', string $error_description = '') @@ -76,7 +76,7 @@ class Error extends BaseFactory $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); $this->logError(401, $error); - System::jsonError(401, $errorObj->toArray()); + $this->jsonError(401, $errorObj->toArray()); } public function Forbidden(string $error = '') @@ -86,7 +86,7 @@ class Error extends BaseFactory $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); $this->logError(403, $error); - System::jsonError(403, $errorObj->toArray()); + $this->jsonError(403, $errorObj->toArray()); } public function InternalError(string $error = '') @@ -96,6 +96,6 @@ class Error extends BaseFactory $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); $this->logError(500, $error); - System::jsonError(500, $errorObj->toArray()); + $this->jsonError(500, $errorObj->toArray()); } } diff --git a/src/Module/Api/Mastodon/Proofs.php b/src/Module/Api/Mastodon/Proofs.php index 1cbe1c03d7..df878e5bb2 100644 --- a/src/Module/Api/Mastodon/Proofs.php +++ b/src/Module/Api/Mastodon/Proofs.php @@ -34,6 +34,6 @@ class Proofs extends BaseApi */ protected function rawContent(array $request = []) { - System::jsonError(404, ['error' => 'Record not found']); + $this->jsonError(404, ['error' => 'Record not found']); } } diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index b916f4b463..0369d2026c 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -263,7 +263,7 @@ class Statuses extends BaseApi $item['gravity'] = Item::GRAVITY_COMMENT; $item['object-type'] = Activity\ObjectType::COMMENT; } else { - self::checkThrottleLimit(); + $this->checkThrottleLimit(); $item['gravity'] = Item::GRAVITY_PARENT; $item['object-type'] = Activity\ObjectType::NOTE; diff --git a/src/Module/Api/Twitter/Statuses/Update.php b/src/Module/Api/Twitter/Statuses/Update.php index 397e3bbd27..2151dabd68 100644 --- a/src/Module/Api/Twitter/Statuses/Update.php +++ b/src/Module/Api/Twitter/Statuses/Update.php @@ -121,7 +121,7 @@ class Update extends BaseApi $item['gravity'] = Item::GRAVITY_COMMENT; $item['object-type'] = Activity\ObjectType::COMMENT; } else { - self::checkThrottleLimit(); + $this->checkThrottleLimit(); $item['gravity'] = Item::GRAVITY_PARENT; $item['object-type'] = Activity\ObjectType::NOTE; diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index d6e5f748f2..c73f90aba6 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -434,7 +434,7 @@ class BaseApi extends BaseModule } } - public static function checkThrottleLimit() + public function checkThrottleLimit() { $uid = self::getCurrentUserID(); @@ -447,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()); } } @@ -464,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()); } } @@ -480,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()); } } } diff --git a/src/Module/Circle.php b/src/Module/Circle.php index e2b6f89778..6fd80a27be 100644 --- a/src/Module/Circle.php +++ b/src/Module/Circle.php @@ -135,7 +135,7 @@ class Circle extends BaseModule $this->jsonExit(['status' => 'OK', 'message' => $message]); } catch (\Exception $e) { DI::sysmsg()->addNotice($e->getMessage()); - System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]); + $this->jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]); } } diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 10a07f6e65..e2a9d5ee65 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -144,7 +144,7 @@ class Friendica extends BaseModule header('Cache-Control: max-age=23200, stale-while-revalidate=23200'); $this->jsonExit($data, 'application/activity+json'); } catch (HTTPException\NotFoundException $e) { - System::jsonError(404, ['error' => 'Record not found']); + $this->jsonError(404, ['error' => 'Record not found']); } } diff --git a/src/Module/NoScrape.php b/src/Module/NoScrape.php index 8751b30bea..cef7cdce72 100644 --- a/src/Module/NoScrape.php +++ b/src/Module/NoScrape.php @@ -45,13 +45,13 @@ class NoScrape extends BaseModule // view infos about a known profile (needs a login) $which = $a->getLoggedInUserNickname(); } else { - System::jsonError(403, 'Authentication required'); + $this->jsonError(403, 'Authentication required'); } $owner = User::getOwnerDataByNick($which); if (empty($owner['uid'])) { - System::jsonError(404, 'Profile not found'); + $this->jsonError(404, 'Profile not found'); } $json_info = [ diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index 023cc38a5a..e639c6801b 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -89,7 +89,7 @@ class Profile extends BaseProfile header('Cache-Control: max-age=23200, stale-while-revalidate=23200'); $this->jsonExit($data, 'application/activity+json'); } catch (HTTPException\NotFoundException $e) { - System::jsonError(404, ['error' => 'Record not found']); + $this->jsonError(404, ['error' => 'Record not found']); } } @@ -97,10 +97,10 @@ class Profile extends BaseProfile // Known deleted user $data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']); - System::jsonError(410, $data); + $this->jsonError(410, $data); } else { // Any other case (unknown, blocked, nverified, expired, no profile, no self contact) - System::jsonError(404, []); + $this->jsonError(404, []); } } } -- 2.39.5