1
1
Fork 0

Merge pull request #11933 from nupplaphil/bug/module_returns

Fix /api/v1/push/subscription
This commit is contained in:
Hypolite Petovan 2022-09-26 07:32:24 -04:00 committed by GitHub
commit e56e543a79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 19 deletions

View file

@ -21,19 +21,36 @@
namespace Friendica\Module\Api\Mastodon; namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\Logger; use Friendica\App;
use Friendica\Core\System; use Friendica\Core\L10n;
use Friendica\DI; use Friendica\Factory\Api\Mastodon\Error;
use Friendica\Factory\Api\Mastodon\Subscription as SubscriptionFactory;
use Friendica\Model\Subscription; use Friendica\Model\Subscription;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Object\Api\Mastodon\Notification; use Friendica\Object\Api\Mastodon\Notification;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* @see https://docs.joinmastodon.org/methods/notifications/push/ * @see https://docs.joinmastodon.org/methods/notifications/push/
*/ */
class PushSubscription extends BaseApi class PushSubscription extends BaseApi
{ {
protected function post(array $request = []) /** @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, SubscriptionFactory $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); self::checkAllowedScope(self::SCOPE_PUSH);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
@ -61,12 +78,13 @@ class PushSubscription extends BaseApi
$ret = Subscription::replace($subscription); $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); self::checkAllowedScope(self::SCOPE_PUSH);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
@ -78,8 +96,8 @@ class PushSubscription extends BaseApi
$subscription = Subscription::select($application['id'], $uid, ['id']); $subscription = Subscription::select($application['id'], $uid, ['id']);
if (empty($subscription)) { if (empty($subscription)) {
Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
DI::mstdnError()->RecordNotFound(); $this->errorFac->RecordNotFound();
} }
$fields = [ $fields = [
@ -94,12 +112,18 @@ class PushSubscription extends BaseApi
$ret = Subscription::update($application['id'], $uid, $fields); $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); self::checkAllowedScope(self::SCOPE_PUSH);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
@ -107,24 +131,29 @@ class PushSubscription extends BaseApi
$ret = Subscription::delete($application['id'], $uid); $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); self::checkAllowedScope(self::SCOPE_PUSH);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
$application = self::getCurrentApplication(); $application = self::getCurrentApplication();
if (!Subscription::exists($application['id'], $uid)) { if (!Subscription::exists($application['id'], $uid)) {
Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
DI::mstdnError()->RecordNotFound(); $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());
} }
} }

View file

@ -0,0 +1,62 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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(): void
{
$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(): void
{
self::markTestIncomplete('Needs dynamic BasicAuth first');
// $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
// BasicAuth::setCurrentUserID();
// $_SESSION['authenticated'] = false;
// api_account_verify_credentials('json');
}
}