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
This commit is contained in:
Hypolite Petovan 2023-09-21 12:27:15 -04:00
parent 81279dad9e
commit 46180d7d5b
11 changed files with 48 additions and 26 deletions

View file

@ -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']);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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());
}
}
}

View file

@ -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()]);
}
}

View file

@ -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']);
}
}

View file

@ -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 = [

View file

@ -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, []);
}
}
}