From 7486ebdc10715b7204b70ce262052d28cb61c69e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 11 Oct 2023 09:20:49 -0400 Subject: [PATCH] Move jsonError out of Factory\Api\Mastodon\Error->UnprocessableEntity --- src/Factory/Api/Mastodon/Error.php | 7 ++----- src/Module/Api/Friendica/Statuses/Dislike.php | 2 +- src/Module/Api/Friendica/Statuses/DislikedBy.php | 2 +- src/Module/Api/Friendica/Statuses/Undislike.php | 2 +- src/Module/Api/Mastodon/Accounts.php | 2 +- src/Module/Api/Mastodon/Accounts/Block.php | 2 +- src/Module/Api/Mastodon/Accounts/Follow.php | 2 +- src/Module/Api/Mastodon/Accounts/Followers.php | 2 +- src/Module/Api/Mastodon/Accounts/Following.php | 2 +- src/Module/Api/Mastodon/Accounts/Lists.php | 2 +- src/Module/Api/Mastodon/Accounts/Mute.php | 2 +- src/Module/Api/Mastodon/Accounts/Note.php | 2 +- src/Module/Api/Mastodon/Accounts/Relationships.php | 2 +- src/Module/Api/Mastodon/Accounts/Statuses.php | 2 +- src/Module/Api/Mastodon/Accounts/Unblock.php | 2 +- src/Module/Api/Mastodon/Accounts/Unfollow.php | 2 +- src/Module/Api/Mastodon/Accounts/Unmute.php | 2 +- src/Module/Api/Mastodon/Apps.php | 2 +- src/Module/Api/Mastodon/Conversations.php | 2 +- src/Module/Api/Mastodon/Conversations/Read.php | 2 +- src/Module/Api/Mastodon/Lists.php | 6 +++--- src/Module/Api/Mastodon/Lists/Accounts.php | 6 +++--- src/Module/Api/Mastodon/Markers.php | 2 +- src/Module/Api/Mastodon/Media.php | 8 ++++---- src/Module/Api/Mastodon/Mutes.php | 2 +- src/Module/Api/Mastodon/Notifications/Dismiss.php | 2 +- src/Module/Api/Mastodon/Polls.php | 2 +- src/Module/Api/Mastodon/ScheduledStatuses.php | 2 +- src/Module/Api/Mastodon/Search.php | 2 +- src/Module/Api/Mastodon/Statuses.php | 4 ++-- src/Module/Api/Mastodon/Statuses/Bookmark.php | 4 ++-- src/Module/Api/Mastodon/Statuses/Card.php | 2 +- src/Module/Api/Mastodon/Statuses/Context.php | 2 +- src/Module/Api/Mastodon/Statuses/Favourite.php | 2 +- src/Module/Api/Mastodon/Statuses/FavouritedBy.php | 2 +- src/Module/Api/Mastodon/Statuses/Mute.php | 4 ++-- src/Module/Api/Mastodon/Statuses/Pin.php | 2 +- src/Module/Api/Mastodon/Statuses/Reblog.php | 7 +++++-- src/Module/Api/Mastodon/Statuses/RebloggedBy.php | 2 +- src/Module/Api/Mastodon/Statuses/Source.php | 2 +- src/Module/Api/Mastodon/Statuses/Unbookmark.php | 4 ++-- src/Module/Api/Mastodon/Statuses/Unfavourite.php | 2 +- src/Module/Api/Mastodon/Statuses/Unmute.php | 4 ++-- src/Module/Api/Mastodon/Statuses/Unpin.php | 2 +- src/Module/Api/Mastodon/Statuses/Unreblog.php | 7 +++++-- src/Module/Api/Mastodon/Tags.php | 2 +- src/Module/Api/Mastodon/Tags/Follow.php | 2 +- src/Module/Api/Mastodon/Tags/Unfollow.php | 2 +- src/Module/Api/Mastodon/Timelines/ListTimeline.php | 2 +- src/Module/Api/Mastodon/Timelines/Tag.php | 2 +- src/Module/OAuth/Authorize.php | 8 ++++---- src/Module/OAuth/Token.php | 2 +- 52 files changed, 76 insertions(+), 73 deletions(-) diff --git a/src/Factory/Api/Mastodon/Error.php b/src/Factory/Api/Mastodon/Error.php index dec56ba90f..1dad4d9c8e 100644 --- a/src/Factory/Api/Mastodon/Error.php +++ b/src/Factory/Api/Mastodon/Error.php @@ -57,14 +57,11 @@ class Error extends BaseFactory return new \Friendica\Object\Api\Mastodon\Error($error, $error_description); } - public function UnprocessableEntity(string $error = '') + public function UnprocessableEntity(string $error = ''): \Friendica\Object\Api\Mastodon\Error { $error = $error ?: $this->l10n->t('Unprocessable Entity'); $error_description = ''; - $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - - $this->logError(422, $error); - $this->jsonError(422, $errorObj->toArray()); + return new \Friendica\Object\Api\Mastodon\Error($error, $error_description); } public function Unauthorized(string $error = '', string $error_description = '') diff --git a/src/Module/Api/Friendica/Statuses/Dislike.php b/src/Module/Api/Friendica/Statuses/Dislike.php index a0dcd7077d..99c8eb857a 100644 --- a/src/Module/Api/Friendica/Statuses/Dislike.php +++ b/src/Module/Api/Friendica/Statuses/Dislike.php @@ -39,7 +39,7 @@ class Dislike extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Friendica/Statuses/DislikedBy.php b/src/Module/Api/Friendica/Statuses/DislikedBy.php index fcae9990ce..3c13120873 100644 --- a/src/Module/Api/Friendica/Statuses/DislikedBy.php +++ b/src/Module/Api/Friendica/Statuses/DislikedBy.php @@ -41,7 +41,7 @@ class DislikedBy extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Friendica/Statuses/Undislike.php b/src/Module/Api/Friendica/Statuses/Undislike.php index f304fb9db9..5387f1b105 100644 --- a/src/Module/Api/Friendica/Statuses/Undislike.php +++ b/src/Module/Api/Friendica/Statuses/Undislike.php @@ -39,7 +39,7 @@ class Undislike extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Mastodon/Accounts.php b/src/Module/Api/Mastodon/Accounts.php index 4ebd0d4bd3..6b88b84ef3 100644 --- a/src/Module/Api/Mastodon/Accounts.php +++ b/src/Module/Api/Mastodon/Accounts.php @@ -40,7 +40,7 @@ class Accounts extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id']) && empty($this->parameters['name'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!empty($this->parameters['id'])) { diff --git a/src/Module/Api/Mastodon/Accounts/Block.php b/src/Module/Api/Mastodon/Accounts/Block.php index 1ddafda569..1582f3299c 100644 --- a/src/Module/Api/Mastodon/Accounts/Block.php +++ b/src/Module/Api/Mastodon/Accounts/Block.php @@ -38,7 +38,7 @@ class Block extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Contact\User::setBlocked($this->parameters['id'], $uid, true); diff --git a/src/Module/Api/Mastodon/Accounts/Follow.php b/src/Module/Api/Mastodon/Accounts/Follow.php index a666f2f91d..272b989ee4 100644 --- a/src/Module/Api/Mastodon/Accounts/Follow.php +++ b/src/Module/Api/Mastodon/Accounts/Follow.php @@ -37,7 +37,7 @@ class Follow extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $request = $this->getRequest([ diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index 22b99ee4d6..19f2a43883 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -41,7 +41,7 @@ class Followers extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index 0fde1404e3..061e22663d 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -41,7 +41,7 @@ class Following extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Accounts/Lists.php b/src/Module/Api/Mastodon/Accounts/Lists.php index f0187a2861..8fdcade664 100644 --- a/src/Module/Api/Mastodon/Accounts/Lists.php +++ b/src/Module/Api/Mastodon/Accounts/Lists.php @@ -41,7 +41,7 @@ class Lists extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Accounts/Mute.php b/src/Module/Api/Mastodon/Accounts/Mute.php index 4169cb90fa..ea8d0ce309 100644 --- a/src/Module/Api/Mastodon/Accounts/Mute.php +++ b/src/Module/Api/Mastodon/Accounts/Mute.php @@ -37,7 +37,7 @@ class Mute extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Contact\User::setIgnored($this->parameters['id'], $uid, true); diff --git a/src/Module/Api/Mastodon/Accounts/Note.php b/src/Module/Api/Mastodon/Accounts/Note.php index 52c76ab693..72a7e0838d 100644 --- a/src/Module/Api/Mastodon/Accounts/Note.php +++ b/src/Module/Api/Mastodon/Accounts/Note.php @@ -38,7 +38,7 @@ class Note extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $request = $this->getRequest([ diff --git a/src/Module/Api/Mastodon/Accounts/Relationships.php b/src/Module/Api/Mastodon/Accounts/Relationships.php index 4ae5b06690..f757151724 100644 --- a/src/Module/Api/Mastodon/Accounts/Relationships.php +++ b/src/Module/Api/Mastodon/Accounts/Relationships.php @@ -44,7 +44,7 @@ class Relationships extends BaseApi ], $request); if (empty($request['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!is_array($request['id'])) { diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index 599a2eff8a..fd8fbcdab1 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -47,7 +47,7 @@ class Statuses extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Accounts/Unblock.php b/src/Module/Api/Mastodon/Accounts/Unblock.php index 8d8fb3e39c..f2e2f2e411 100644 --- a/src/Module/Api/Mastodon/Accounts/Unblock.php +++ b/src/Module/Api/Mastodon/Accounts/Unblock.php @@ -37,7 +37,7 @@ class Unblock extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Contact\User::setBlocked($this->parameters['id'], $uid, false); diff --git a/src/Module/Api/Mastodon/Accounts/Unfollow.php b/src/Module/Api/Mastodon/Accounts/Unfollow.php index cfe3b3cb1e..6a3c68cb32 100644 --- a/src/Module/Api/Mastodon/Accounts/Unfollow.php +++ b/src/Module/Api/Mastodon/Accounts/Unfollow.php @@ -37,7 +37,7 @@ class Unfollow extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $cdata = Contact::getPublicAndUserContactID($this->parameters['id'], $uid); diff --git a/src/Module/Api/Mastodon/Accounts/Unmute.php b/src/Module/Api/Mastodon/Accounts/Unmute.php index 7708e1472b..651233daed 100644 --- a/src/Module/Api/Mastodon/Accounts/Unmute.php +++ b/src/Module/Api/Mastodon/Accounts/Unmute.php @@ -37,7 +37,7 @@ class Unmute extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Contact\User::setIgnored($this->parameters['id'], $uid, false); diff --git a/src/Module/Api/Mastodon/Apps.php b/src/Module/Api/Mastodon/Apps.php index d51a8e7b0a..9d6729216c 100644 --- a/src/Module/Api/Mastodon/Apps.php +++ b/src/Module/Api/Mastodon/Apps.php @@ -70,7 +70,7 @@ class Apps extends BaseApi } if (empty($request['client_name']) || empty($request['redirect_uris'])) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Missing parameters'))); } $client_id = bin2hex(random_bytes(32)); diff --git a/src/Module/Api/Mastodon/Conversations.php b/src/Module/Api/Mastodon/Conversations.php index c45144c98d..532db4bcda 100644 --- a/src/Module/Api/Mastodon/Conversations.php +++ b/src/Module/Api/Mastodon/Conversations.php @@ -38,7 +38,7 @@ class Conversations extends BaseApi $uid = self::getCurrentUserID(); if (!empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } DBA::delete('conv', ['id' => $this->parameters['id'], 'uid' => $uid]); diff --git a/src/Module/Api/Mastodon/Conversations/Read.php b/src/Module/Api/Mastodon/Conversations/Read.php index 86a5090e37..2e876fcb5d 100644 --- a/src/Module/Api/Mastodon/Conversations/Read.php +++ b/src/Module/Api/Mastodon/Conversations/Read.php @@ -38,7 +38,7 @@ class Read extends BaseApi $uid = self::getCurrentUserID(); if (!empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } DBA::update('mail', ['seen' => true], ['convid' => $this->parameters['id'], 'uid' => $uid]); diff --git a/src/Module/Api/Mastodon/Lists.php b/src/Module/Api/Mastodon/Lists.php index 9d9e6955b1..beb1a3b9a6 100644 --- a/src/Module/Api/Mastodon/Lists.php +++ b/src/Module/Api/Mastodon/Lists.php @@ -37,7 +37,7 @@ class Lists extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!Circle::exists($this->parameters['id'], $uid)) { @@ -61,7 +61,7 @@ class Lists extends BaseApi ], $request); if (empty($request['title'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Circle::create($uid, $request['title']); @@ -82,7 +82,7 @@ class Lists extends BaseApi ], $request); if (empty($request['title']) || empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Circle::update($this->parameters['id'], $request['title']); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index ffa63caf44..f72f8cad82 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -43,7 +43,7 @@ class Accounts extends BaseApi ], $request); if (empty($request['account_ids']) || empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } return Circle::removeMembers($this->parameters['id'], $request['account_ids']); @@ -58,7 +58,7 @@ class Accounts extends BaseApi ], $request); if (empty($request['account_ids']) || empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Circle::addMembers($this->parameters['id'], $request['account_ids']); @@ -73,7 +73,7 @@ class Accounts extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Markers.php b/src/Module/Api/Mastodon/Markers.php index eb19acf8eb..25b56d72ea 100644 --- a/src/Module/Api/Mastodon/Markers.php +++ b/src/Module/Api/Mastodon/Markers.php @@ -48,7 +48,7 @@ class Markers extends BaseApi } if (empty($timeline) || empty($last_read_id) || empty($application['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $condition = ['application-id' => $application['id'], 'uid' => $uid, 'timeline' => $timeline]; diff --git a/src/Module/Api/Mastodon/Media.php b/src/Module/Api/Mastodon/Media.php index 91c88ee181..738feab7c8 100644 --- a/src/Module/Api/Mastodon/Media.php +++ b/src/Module/Api/Mastodon/Media.php @@ -48,12 +48,12 @@ class Media extends BaseApi Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]); if (empty($_FILES['file'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $media = Photo::upload($uid, $_FILES['file'], '', null, null, '', '', $request['description']); if (empty($media)) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } Logger::info('Uploaded photo', ['media' => $media]); @@ -74,7 +74,7 @@ class Media extends BaseApi ], $request); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]); @@ -104,7 +104,7 @@ class Media extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Mutes.php b/src/Module/Api/Mastodon/Mutes.php index 71b1949f57..6557b770b8 100644 --- a/src/Module/Api/Mastodon/Mutes.php +++ b/src/Module/Api/Mastodon/Mutes.php @@ -40,7 +40,7 @@ class Mutes extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Notifications/Dismiss.php b/src/Module/Api/Mastodon/Notifications/Dismiss.php index 50d1be27b5..cc89479726 100644 --- a/src/Module/Api/Mastodon/Notifications/Dismiss.php +++ b/src/Module/Api/Mastodon/Notifications/Dismiss.php @@ -38,7 +38,7 @@ class Dismiss extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $condition = ['id' => $this->parameters['id']]; diff --git a/src/Module/Api/Mastodon/Polls.php b/src/Module/Api/Mastodon/Polls.php index c43bdf510f..a5e4fb33a1 100644 --- a/src/Module/Api/Mastodon/Polls.php +++ b/src/Module/Api/Mastodon/Polls.php @@ -39,7 +39,7 @@ class Polls extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $this->jsonExit(DI::mstdnPoll()->createFromId($this->parameters['id'], $uid)); diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index f136c2708f..cedafb8ab7 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -47,7 +47,7 @@ class ScheduledStatuses extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!DBA::exists('delayed-post', ['id' => $this->parameters['id'], 'uid' => $uid])) { diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index ec4527f00d..fb222beff2 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -60,7 +60,7 @@ class Search extends BaseApi ], $request); if (empty($request['q'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $limit = min($request['limit'], 40); diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 0940befbad..def4d51f63 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -319,7 +319,7 @@ class Statuses extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => $uid]); @@ -342,7 +342,7 @@ class Statuses extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $this->jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes(), false)); diff --git a/src/Module/Api/Mastodon/Statuses/Bookmark.php b/src/Module/Api/Mastodon/Statuses/Bookmark.php index 1ba0d6e040..cdbdcb6e6b 100644 --- a/src/Module/Api/Mastodon/Statuses/Bookmark.php +++ b/src/Module/Api/Mastodon/Statuses/Bookmark.php @@ -39,7 +39,7 @@ class Bookmark extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginal(['uid', 'id', 'uri-id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]); @@ -48,7 +48,7 @@ class Bookmark extends BaseApi } if ($item['gravity'] != Item::GRAVITY_PARENT) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be bookmarked')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Only starting posts can be bookmarked'))); } if ($item['uid'] == 0) { diff --git a/src/Module/Api/Mastodon/Statuses/Card.php b/src/Module/Api/Mastodon/Statuses/Card.php index 193bb7b0cd..ea5705186c 100644 --- a/src/Module/Api/Mastodon/Statuses/Card.php +++ b/src/Module/Api/Mastodon/Statuses/Card.php @@ -40,7 +40,7 @@ class Card extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!$post = Post::selectOriginal(['uri-id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) { diff --git a/src/Module/Api/Mastodon/Statuses/Context.php b/src/Module/Api/Mastodon/Statuses/Context.php index c171c0ccae..dbce15058c 100644 --- a/src/Module/Api/Mastodon/Statuses/Context.php +++ b/src/Module/Api/Mastodon/Statuses/Context.php @@ -41,7 +41,7 @@ class Context extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $request = $this->getRequest([ diff --git a/src/Module/Api/Mastodon/Statuses/Favourite.php b/src/Module/Api/Mastodon/Statuses/Favourite.php index 384abc3d82..103de86790 100644 --- a/src/Module/Api/Mastodon/Statuses/Favourite.php +++ b/src/Module/Api/Mastodon/Statuses/Favourite.php @@ -39,7 +39,7 @@ class Favourite extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['id', 'uri-id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Mastodon/Statuses/FavouritedBy.php b/src/Module/Api/Mastodon/Statuses/FavouritedBy.php index bce95610a9..d91eff0b80 100644 --- a/src/Module/Api/Mastodon/Statuses/FavouritedBy.php +++ b/src/Module/Api/Mastodon/Statuses/FavouritedBy.php @@ -41,7 +41,7 @@ class FavouritedBy extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!$post = Post::selectOriginal(['uri-id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) { diff --git a/src/Module/Api/Mastodon/Statuses/Mute.php b/src/Module/Api/Mastodon/Statuses/Mute.php index 68491c8b70..0931cd733d 100644 --- a/src/Module/Api/Mastodon/Statuses/Mute.php +++ b/src/Module/Api/Mastodon/Statuses/Mute.php @@ -39,7 +39,7 @@ class Mute extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['uri-id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); @@ -48,7 +48,7 @@ class Mute extends BaseApi } if ($item['gravity'] != Item::GRAVITY_PARENT) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be muted')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Only starting posts can be muted'))); } Post\ThreadUser::setIgnored($item['uri-id'], $uid, true); diff --git a/src/Module/Api/Mastodon/Statuses/Pin.php b/src/Module/Api/Mastodon/Statuses/Pin.php index 99f097740f..fbd2f00cee 100644 --- a/src/Module/Api/Mastodon/Statuses/Pin.php +++ b/src/Module/Api/Mastodon/Statuses/Pin.php @@ -38,7 +38,7 @@ class Pin extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['uri-id', 'gravity', 'author-id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Mastodon/Statuses/Reblog.php b/src/Module/Api/Mastodon/Statuses/Reblog.php index 3e37a94a7d..54ada1d1f9 100644 --- a/src/Module/Api/Mastodon/Statuses/Reblog.php +++ b/src/Module/Api/Mastodon/Statuses/Reblog.php @@ -42,7 +42,7 @@ class Reblog extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['id', 'uri-id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); @@ -53,7 +53,10 @@ class Reblog extends BaseApi if ($item['network'] == Protocol::DIASPORA) { Diaspora::performReshare($this->parameters['id'], $uid); } elseif (!in_array($item['network'], [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::TWITTER])) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t("Posts from %s can't be shared", ContactSelector::networkToName($item['network']))); + $this->logErrorAndJsonExit( + 422, + $this->errorFactory->UnprocessableEntity($this->t("Posts from %s can't be shared", ContactSelector::networkToName($item['network']))) + ); } else { Item::performActivity($item['id'], 'announce', $uid); } diff --git a/src/Module/Api/Mastodon/Statuses/RebloggedBy.php b/src/Module/Api/Mastodon/Statuses/RebloggedBy.php index 9c508e42a9..eab40c665e 100644 --- a/src/Module/Api/Mastodon/Statuses/RebloggedBy.php +++ b/src/Module/Api/Mastodon/Statuses/RebloggedBy.php @@ -41,7 +41,7 @@ class RebloggedBy extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if (!$post = Post::selectOriginal(['uri-id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) { diff --git a/src/Module/Api/Mastodon/Statuses/Source.php b/src/Module/Api/Mastodon/Statuses/Source.php index 408df3aad5..1e72f33a02 100644 --- a/src/Module/Api/Mastodon/Statuses/Source.php +++ b/src/Module/Api/Mastodon/Statuses/Source.php @@ -41,7 +41,7 @@ class Source extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $id = $this->parameters['id']; diff --git a/src/Module/Api/Mastodon/Statuses/Unbookmark.php b/src/Module/Api/Mastodon/Statuses/Unbookmark.php index 4eccc97b2e..c64a6c8a77 100644 --- a/src/Module/Api/Mastodon/Statuses/Unbookmark.php +++ b/src/Module/Api/Mastodon/Statuses/Unbookmark.php @@ -39,7 +39,7 @@ class Unbookmark extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginal(['uid', 'id', 'uri-id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]); @@ -48,7 +48,7 @@ class Unbookmark extends BaseApi } if ($item['gravity'] != Item::GRAVITY_PARENT) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be unbookmarked')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Only starting posts can be unbookmarked'))); } if ($item['uid'] == 0) { diff --git a/src/Module/Api/Mastodon/Statuses/Unfavourite.php b/src/Module/Api/Mastodon/Statuses/Unfavourite.php index ada587b2fe..78e9fb350f 100644 --- a/src/Module/Api/Mastodon/Statuses/Unfavourite.php +++ b/src/Module/Api/Mastodon/Statuses/Unfavourite.php @@ -39,7 +39,7 @@ class Unfavourite extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['id', 'uri-id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Mastodon/Statuses/Unmute.php b/src/Module/Api/Mastodon/Statuses/Unmute.php index 511ae15407..e54380dbe5 100644 --- a/src/Module/Api/Mastodon/Statuses/Unmute.php +++ b/src/Module/Api/Mastodon/Statuses/Unmute.php @@ -39,7 +39,7 @@ class Unmute extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['uri-id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); @@ -48,7 +48,7 @@ class Unmute extends BaseApi } if ($item['gravity'] != Item::GRAVITY_PARENT) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be unmuted')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Only starting posts can be unmuted'))); } Post\ThreadUser::setIgnored($item['uri-id'], $uid, false); diff --git a/src/Module/Api/Mastodon/Statuses/Unpin.php b/src/Module/Api/Mastodon/Statuses/Unpin.php index c11f89f6e0..f04ff5306e 100644 --- a/src/Module/Api/Mastodon/Statuses/Unpin.php +++ b/src/Module/Api/Mastodon/Statuses/Unpin.php @@ -38,7 +38,7 @@ class Unpin extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['uri-id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); diff --git a/src/Module/Api/Mastodon/Statuses/Unreblog.php b/src/Module/Api/Mastodon/Statuses/Unreblog.php index c99011ae43..50931cecd7 100644 --- a/src/Module/Api/Mastodon/Statuses/Unreblog.php +++ b/src/Module/Api/Mastodon/Statuses/Unreblog.php @@ -41,7 +41,7 @@ class Unreblog extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $item = Post::selectOriginalForUser($uid, ['id', 'uri-id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]); @@ -59,7 +59,10 @@ class Unreblog extends BaseApi $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound()); } } elseif (!in_array($item['network'], [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::TWITTER])) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t("Posts from %s can't be unshared", ContactSelector::networkToName($item['network']))); + $this->logErrorAndJsonExit( + 422, + $this->errorFactory->UnprocessableEntity($this->t("Posts from %s can't be unshared", ContactSelector::networkToName($item['network']))) + ); } else { Item::performActivity($item['id'], 'unannounce', $uid); } diff --git a/src/Module/Api/Mastodon/Tags.php b/src/Module/Api/Mastodon/Tags.php index d636dd9126..d61ee88acf 100644 --- a/src/Module/Api/Mastodon/Tags.php +++ b/src/Module/Api/Mastodon/Tags.php @@ -40,7 +40,7 @@ class Tags extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['hashtag'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $tag = ltrim($this->parameters['hashtag'], '#'); diff --git a/src/Module/Api/Mastodon/Tags/Follow.php b/src/Module/Api/Mastodon/Tags/Follow.php index a450b2248e..03844a7ddc 100644 --- a/src/Module/Api/Mastodon/Tags/Follow.php +++ b/src/Module/Api/Mastodon/Tags/Follow.php @@ -37,7 +37,7 @@ class Follow extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['hashtag'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $fields = ['uid' => $uid, 'term' => '#' . ltrim($this->parameters['hashtag'], '#')]; diff --git a/src/Module/Api/Mastodon/Tags/Unfollow.php b/src/Module/Api/Mastodon/Tags/Unfollow.php index b4a1a7e73d..07b93d95a2 100644 --- a/src/Module/Api/Mastodon/Tags/Unfollow.php +++ b/src/Module/Api/Mastodon/Tags/Unfollow.php @@ -37,7 +37,7 @@ class Unfollow extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['hashtag'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $term = ['uid' => $uid, 'term' => '#' . ltrim($this->parameters['hashtag'], '#')]; diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index 2834b63fc3..baed01e0ec 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -45,7 +45,7 @@ class ListTimeline extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['id'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } $request = $this->getRequest([ diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 9bbafd587e..60bdab8373 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -45,7 +45,7 @@ class Tag extends BaseApi $uid = self::getCurrentUserID(); if (empty($this->parameters['hashtag'])) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } /** diff --git a/src/Module/OAuth/Authorize.php b/src/Module/OAuth/Authorize.php index ea91de5a04..e9ef07c62f 100644 --- a/src/Module/OAuth/Authorize.php +++ b/src/Module/OAuth/Authorize.php @@ -51,17 +51,17 @@ class Authorize extends BaseApi if ($request['response_type'] != 'code') { Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]); - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing response type')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Unsupported or missing response type'))); } if (empty($request['client_id']) || empty($request['redirect_uri'])) { Logger::warning('Incomplete request data', ['request' => $_REQUEST]); - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Incomplete request data'))); } $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']); if (empty($application)) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } // @todo Compare the application scope and requested scope @@ -87,7 +87,7 @@ class Authorize extends BaseApi $token = OAuth::createTokenForUser($application, $uid, $request['scope']); if (!$token) { - DI::mstdnError()->UnprocessableEntity(); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity()); } if ($application['redirect_uri'] != 'urn:ietf:wg:oauth:2.0:oob') { diff --git a/src/Module/OAuth/Token.php b/src/Module/OAuth/Token.php index 61a7f22889..ab9cd3ec26 100644 --- a/src/Module/OAuth/Token.php +++ b/src/Module/OAuth/Token.php @@ -105,7 +105,7 @@ class Token extends BaseApi $me = $owner['url']; } else { Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]); - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type')); + $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Unsupported or missing grant type'))); } $object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at'], $me); -- 2.39.5