From 63fc315ea01e8bcaad2067b7f8ef8b9566c2c071 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 12 Nov 2022 20:00:02 -0500 Subject: [PATCH] Add support for Mastodon /reports API call --- src/Module/Api/Mastodon/Reports.php | 74 +++++++++++++++++++++++++++++ static/routes.config.php | 2 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/Module/Api/Mastodon/Reports.php diff --git a/src/Module/Api/Mastodon/Reports.php b/src/Module/Api/Mastodon/Reports.php new file mode 100644 index 0000000000..6ae54eb6f6 --- /dev/null +++ b/src/Module/Api/Mastodon/Reports.php @@ -0,0 +1,74 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Core\System; +use Friendica\Model\Contact; +use Friendica\Module\Api\ApiResponse; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * @see https://docs.joinmastodon.org/methods/accounts/reports/ + */ +class Reports extends BaseApi +{ + /** @var \Friendica\Moderation\Factory\Report */ + private $reportFactory; + /** @var \Friendica\Moderation\Repository\Report */ + private $reportRepo; + + public function __construct(\Friendica\Moderation\Repository\Report $reportRepo, \Friendica\Moderation\Factory\Report $reportFactory, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->reportFactory = $reportFactory; + $this->reportRepo = $reportRepo; + } + + public function post(array $request = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + + $request = $this->getRequest([ + 'account_id' => '', // ID of the account to report + 'status_ids' => [], // Array of Statuses to attach to the report, for context + 'comment' => '', // Reason for the report (default max 1000 characters) + 'forward' => false, // If the account is remote, should the report be forwarded to the remote admin? + ], $request); + + $contact = Contact::getById($request['account_id'], ['id']); + if (empty($contact)) { + throw new HTTPException\NotFoundException('Account ' . $request['account_id'] . ' not found'); + } + + $report = $this->reportFactory->createFromReportsRequest(self::getCurrentUserID(), $request['account_id'], $request['comment'], $request['forward'], $request['status_ids']); + + $this->reportRepo->save($report); + + System::jsonExit([]); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 0958ee877b..43fafb1672 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -256,7 +256,7 @@ return [ '/polls/{id:\d+}/votes' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], // not supported '/preferences' => [Module\Api\Mastodon\Preferences::class, [R::GET ]], '/push/subscription' => [Module\Api\Mastodon\PushSubscription::class, [R::GET, R::POST, R::PUT, R::DELETE]], - '/reports' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], // not supported + '/reports' => [Module\Api\Mastodon\Reports::class, [ R::POST]], '/scheduled_statuses' => [Module\Api\Mastodon\ScheduledStatuses::class, [R::GET ]], '/scheduled_statuses/{id:\d+}' => [Module\Api\Mastodon\ScheduledStatuses::class, [R::GET, R::PUT, R::DELETE]], '/statuses' => [Module\Api\Mastodon\Statuses::class, [ R::POST]], -- 2.39.5