From 74f3cbc3835c742c48723ea7b0360504ccdfebe1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 15 Aug 2021 07:28:26 +0000 Subject: [PATCH] Improved error check, added "put" --- doc/API-Mastodon.md | 2 +- src/Factory/Api/Mastodon/Subscription.php | 2 +- src/Model/Subscription.php | 45 +++++++++++++++++++- src/Module/Api/Mastodon/PushSubscription.php | 38 +++++++++++++++-- 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index dd82a5144..cec90bea4 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -96,6 +96,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`DELETE /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/) - [`GET /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/) - [`PUSH /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/) +- [`PUT /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/) - [`GET /api/v1/scheduled_statuses`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`DELETE /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`GET /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) @@ -178,7 +179,6 @@ They refer to features or data that don't exist in Friendica yet. - [`POST /api/v1/markers`](https://docs.joinmastodon.org/methods/timelines/markers/) - [`GET /api/v1/polls/:id`](https://docs.joinmastodon.org/methods/statuses/polls/) - [`POST /api/v1/polls/:id/votes`](https://docs.joinmastodon.org/methods/statuses/polls/) -- [`PUT /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/) - [`POST /api/v1/reports`](https://docs.joinmastodon.org/methods/accounts/reports/) - [`PUT /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`GET /api/v1/streaming`](https://docs.joinmastodon.org/methods/timelines/streaming/) diff --git a/src/Factory/Api/Mastodon/Subscription.php b/src/Factory/Api/Mastodon/Subscription.php index 3b8f343e1..08dbcf0d1 100644 --- a/src/Factory/Api/Mastodon/Subscription.php +++ b/src/Factory/Api/Mastodon/Subscription.php @@ -35,7 +35,7 @@ class Subscription extends BaseFactory */ public function createForApplicationIdAndUserId(int $applicationid, int $uid): \Friendica\Object\Api\Mastodon\Subscription { - $subscription = DBA::selectFirst('subscription', ['application-id' => $applicationid, 'uid' => $uid]); + $subscription = DBA::selectFirst('subscription', [], ['application-id' => $applicationid, 'uid' => $uid]); return new \Friendica\Object\Api\Mastodon\Subscription($subscription, ModelSubscription::getVapidKey()); } } diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index fac585e99..8d3da36ab 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -28,7 +28,48 @@ use Friendica\Util\Crypto; class Subscription { /** - * Insert an Subscription record + * Select a subscription record exists + * + * @param int $applicationid + * @param int $uid + * @param array $fields + * + * @return bool Does it exist? + */ + public static function select(int $applicationid, int $uid, array $fields = []) + { + return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]); + } + + /** + * Check if a subscription record exists + * + * @param int $applicationid + * @param int $uid + * + * @return bool Does it exist? + */ + public static function exists(int $applicationid, int $uid) + { + return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]); + } + + /** + * Update a subscription record + * + * @param int $applicationid + * @param int $uid + * @param array $fields subscription fields + * + * @return bool result of update + */ + public static function update(int $applicationid, int $uid, array $fields) + { + return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]); + } + + /** + * Insert or replace a subscription record * * @param array $fields subscription fields * @@ -41,6 +82,7 @@ class Subscription /** * Delete a subscription record + * * @param int $applicationid * @param int $uid * @return bool @@ -52,6 +94,7 @@ class Subscription /** * Fetch a VAPID key + * * @return string */ public static function getVapidKey(): string diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 68b1b46b1..411d5391f 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -21,7 +21,6 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\App\Router; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; @@ -72,7 +71,31 @@ class PushSubscription extends BaseApi $uid = self::getCurrentUserID(); $application = self::getCurrentApplication(); - self::unsupported(Router::PUT); + $request = self::getRequest([ + 'data' => [], + ]); + + $subscription = Subscription::select($application['id'], $uid, ['id']); + if (empty($subscription)) { + Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]); + DI::mstdnError()->RecordNotFound(); + } + + $fields = [ + 'follow' => $request['data']['alerts']['follow'] ?? false, + 'favourite' => $request['data']['alerts']['favourite'] ?? false, + 'reblog' => $request['data']['alerts']['reblog'] ?? false, + 'mention' => $request['data']['alerts']['mention'] ?? false, + 'poll' => $request['data']['alerts']['poll'] ?? false, + 'follow_request' => $request['data']['alerts']['follow_request'] ?? false, + 'status' => $request['data']['alerts']['status'] ?? false, + ]; + + $ret = Subscription::update($application['id'], $uid, $fields); + + Logger::info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid, 'fields' => $fields]); + + return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray(); } public static function delete(array $parameters = []) @@ -81,7 +104,9 @@ class PushSubscription extends BaseApi $uid = self::getCurrentUserID(); $application = self::getCurrentApplication(); - Subscription::delete($application['id'], $uid); + $ret = Subscription::delete($application['id'], $uid); + + Logger::info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid]); System::jsonExit([]); } @@ -92,6 +117,13 @@ class PushSubscription extends BaseApi $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(); + } + + Logger::info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]); + return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray(); } }