From ad62265d0838622f902c1bc4f4f5da83e63b7bd1 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 25 Sep 2022 21:15:14 +0200 Subject: [PATCH 1/6] Fix /api/v1/push/subscription --- src/Module/Api/Mastodon/PushSubscription.php | 59 +++++++++++++------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 945ca6392..225d61b68 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -21,19 +21,35 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Core\Logger; -use Friendica\Core\System; -use Friendica\DI; +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Factory\Api\Mastodon\Error; use Friendica\Model\Subscription; +use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; use Friendica\Object\Api\Mastodon\Notification; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; /** * @see https://docs.joinmastodon.org/methods/notifications/push/ */ class PushSubscription extends BaseApi { - protected function post(array $request = []) + /** @var \Friendica\Factory\Api\Mastodon\Subscription */ + protected $subscriptionFac; + /** @var Error */ + protected $errorFac; + + public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, \Friendica\Factory\Api\Mastodon\Subscription $subscriptionFac, Error $errorFac, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->subscriptionFac = $subscriptionFac; + $this->errorFac = $errorFac; + } + + protected function post(array $request = []): void { self::checkAllowedScope(self::SCOPE_PUSH); $uid = self::getCurrentUserID(); @@ -61,12 +77,13 @@ class PushSubscription extends BaseApi $ret = Subscription::replace($subscription); - Logger::info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]); + $this->logger->info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]); - return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray(); + $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid); + $this->response->exitWithJson($subscriptionObj->toArray()); } - public function put(array $request = []) + public function put(array $request = []): void { self::checkAllowedScope(self::SCOPE_PUSH); $uid = self::getCurrentUserID(); @@ -78,8 +95,8 @@ class PushSubscription extends BaseApi $subscription = Subscription::select($application['id'], $uid, ['id']); if (empty($subscription)) { - Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); - DI::mstdnError()->RecordNotFound(); + $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); + $this->errorFac->RecordNotFound(); } $fields = [ @@ -94,12 +111,14 @@ class PushSubscription extends BaseApi $ret = Subscription::update($application['id'], $uid, $fields); - Logger::info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid, 'fields' => $fields]); + $this->logger->info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'], + 'uid' => $uid, 'fields' => $fields]); - return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray(); + $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid); + $this->response->exitWithJson($subscriptionObj->toArray()); } - protected function delete(array $request = []) + protected function delete(array $request = []): void { self::checkAllowedScope(self::SCOPE_PUSH); $uid = self::getCurrentUserID(); @@ -107,24 +126,26 @@ class PushSubscription extends BaseApi $ret = Subscription::delete($application['id'], $uid); - Logger::info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid]); + $this->logger->info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'], + 'uid' => $uid]); - System::jsonExit([]); + $this->response->exitWithJson([]); } - protected function rawContent(array $request = []) + protected function rawContent(array $request = []): void { self::checkAllowedScope(self::SCOPE_PUSH); $uid = self::getCurrentUserID(); $application = self::getCurrentApplication(); if (!Subscription::exists($application['id'], $uid)) { - Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); - DI::mstdnError()->RecordNotFound(); + $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); + $this->errorFac->RecordNotFound(); } - Logger::info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]); + $this->logger->info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]); - return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray(); + $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid); + $this->response->exitWithJson($subscriptionObj->toArray()); } } From 006dbcf4cec84b98a60432ed2913a8be515b7898 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 25 Sep 2022 21:16:56 +0200 Subject: [PATCH 2/6] some code-beautification --- src/Module/Api/Mastodon/PushSubscription.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 225d61b68..4facfe191 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -24,6 +24,7 @@ namespace Friendica\Module\Api\Mastodon; use Friendica\App; use Friendica\Core\L10n; use Friendica\Factory\Api\Mastodon\Error; +use Friendica\Factory\Api\Mastodon\Subscription as SubscriptionFactory; use Friendica\Model\Subscription; use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; @@ -36,12 +37,12 @@ use Psr\Log\LoggerInterface; */ class PushSubscription extends BaseApi { - /** @var \Friendica\Factory\Api\Mastodon\Subscription */ + /** @var SubscriptionFactory */ protected $subscriptionFac; /** @var Error */ protected $errorFac; - public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, \Friendica\Factory\Api\Mastodon\Subscription $subscriptionFac, Error $errorFac, array $server, array $parameters = []) + public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, SubscriptionFactory $subscriptionFac, Error $errorFac, array $server, array $parameters = []) { parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); @@ -111,8 +112,10 @@ class PushSubscription extends BaseApi $ret = Subscription::update($application['id'], $uid, $fields); - $this->logger->info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'], - 'uid' => $uid, 'fields' => $fields]); + $this->logger->info('Subscription updated', [ + 'result' => $ret, 'application-id' => $application['id'], + 'uid' => $uid, 'fields' => $fields + ]); $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid); $this->response->exitWithJson($subscriptionObj->toArray()); @@ -126,8 +129,10 @@ class PushSubscription extends BaseApi $ret = Subscription::delete($application['id'], $uid); - $this->logger->info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'], - 'uid' => $uid]); + $this->logger->info('Subscription deleted', [ + 'result' => $ret, 'application-id' => $application['id'], + 'uid' => $uid + ]); $this->response->exitWithJson([]); } From 60b1cac1667cf41ce12a48b5c8014eeedc246084 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 25 Sep 2022 21:36:32 +0200 Subject: [PATCH 3/6] Add empty testcase for later .. would work if we could mock applications --- .../Api/Mastodon/PushSubscriptionTest.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/src/Module/Api/Mastodon/PushSubscriptionTest.php diff --git a/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php b/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php new file mode 100644 index 000000000..ec0df8c98 --- /dev/null +++ b/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php @@ -0,0 +1,62 @@ +. + * + */ + +namespace Friendica\Test\src\Module\Api\Mastodon; + +use Friendica\Test\src\Module\Api\ApiTest; + +class PushSubscriptionTest extends ApiTest +{ + /** + * Test the api_account_verify_credentials() function. + * + * @return void + */ + public function testApiAccountVerifyCredentials() + { + $this->markTestIncomplete('Needs mocking of whole applictaions/Apps first'); + + // $this->useHttpMethod(Router::POST); + // + // $response = (new PushSubscription(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), DI::mstdnSubscription(), DI::mstdnError(), [])) + // ->run(); + // + // $json = $this->toJson($response); + // print_r($json); + // + // $this->assertEquals(1,1); + } + + /** + * Test the api_account_verify_credentials() function without an authenticated user. + * + * @return void + */ + public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser() + { + self::markTestIncomplete('Needs dynamic BasicAuth first'); + + // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); + // BasicAuth::setCurrentUserID(); + // $_SESSION['authenticated'] = false; + // api_account_verify_credentials('json'); + } +} From 2d4598a4db7f58f1b86ace064918cc13d7f2617c Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 25 Sep 2022 23:17:37 +0200 Subject: [PATCH 4/6] adapt return type --- tests/src/Module/Api/Mastodon/PushSubscriptionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php b/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php index ec0df8c98..ae453e458 100644 --- a/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php +++ b/tests/src/Module/Api/Mastodon/PushSubscriptionTest.php @@ -30,7 +30,7 @@ class PushSubscriptionTest extends ApiTest * * @return void */ - public function testApiAccountVerifyCredentials() + public function testApiAccountVerifyCredentials(): void { $this->markTestIncomplete('Needs mocking of whole applictaions/Apps first'); @@ -50,7 +50,7 @@ class PushSubscriptionTest extends ApiTest * * @return void */ - public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser() + public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser(): void { self::markTestIncomplete('Needs dynamic BasicAuth first'); From 5c888137753ccd8a31b7502a6339044482f6a2b0 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 26 Sep 2022 07:24:55 +0200 Subject: [PATCH 5/6] Update src/Module/Api/Mastodon/PushSubscription.php Co-authored-by: Hypolite Petovan --- src/Module/Api/Mastodon/PushSubscription.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 4facfe191..fc786f40e 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -113,8 +113,10 @@ class PushSubscription extends BaseApi $ret = Subscription::update($application['id'], $uid, $fields); $this->logger->info('Subscription updated', [ - 'result' => $ret, 'application-id' => $application['id'], - 'uid' => $uid, 'fields' => $fields + 'result' => $ret, + 'application-id' => $application['id'], + 'uid' => $uid, + 'fields' => $fields, ]); $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid); From 883a2ee71b9ca3dd5e80bcf3f5a2d7f3593aaeef Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 26 Sep 2022 07:25:04 +0200 Subject: [PATCH 6/6] Update src/Module/Api/Mastodon/PushSubscription.php Co-authored-by: Hypolite Petovan --- src/Module/Api/Mastodon/PushSubscription.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index fc786f40e..31e1b0eb1 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -132,8 +132,9 @@ class PushSubscription extends BaseApi $ret = Subscription::delete($application['id'], $uid); $this->logger->info('Subscription deleted', [ - 'result' => $ret, 'application-id' => $application['id'], - 'uid' => $uid + 'result' => $ret, + 'application-id' => $application['id'], + 'uid' => $uid, ]); $this->response->exitWithJson([]);