Improved error check, added "put"

This commit is contained in:
Michael 2021-08-15 07:28:26 +00:00
parent 8b6ace6629
commit 74f3cbc383
4 changed files with 81 additions and 6 deletions

View File

@ -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/)

View File

@ -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());
}
}

View File

@ -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

View File

@ -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();
}
}