mediawiki-extensions-Echo/includes/api/Push/ApiEchoPushSubscriptionsCreate.php
Dmitry Brant 6c5a88107c Turn push notification token list into a circular buffer.
At the moment we support a maximum of 10 tokens per user for subscribing
to push notifications, stored as a basic list that runs out when the
limit is reached.  There may, however, be some edge cases where an app
registers a token and then forgets to unregister it (and repeats this 10
times), after which time it will be unable to register any new token.

This changes the token list to behave more like a circular buffer, by
simply deleting the oldest token before inserting the new one. This way
an app could register a new token even in the rare case of forgetting
the previous ten.

Change-Id: I387de63460882e4e56d1aa6db1f78d73a0495208
2021-07-29 17:22:57 -04:00

130 lines
3.1 KiB
PHP

<?php
namespace EchoPush\Api;
use ApiBase;
use ApiMain;
use EchoPush\SubscriptionManager;
use EchoPush\Utils;
use EchoServices;
use Wikimedia\ParamValidator\ParamValidator;
class ApiEchoPushSubscriptionsCreate extends ApiBase {
/**
* Supported push notification providers:
* (1) fcm: Firebase Cloud Messaging
* (2) apns: Apple Push Notification Service
*/
private const PROVIDERS = [ 'fcm', 'apns' ];
/** @var ApiBase */
private $parent;
/** @var SubscriptionManager */
private $subscriptionManager;
/**
* Static entry point for initializing the module
* @param ApiBase $parent Parent module
* @param string $name Module name
* @return ApiEchoPushSubscriptionsCreate
*/
public static function factory( ApiBase $parent, string $name ):
ApiEchoPushSubscriptionsCreate {
$subscriptionManger = EchoServices::getInstance()->getPushSubscriptionManager();
$module = new self( $parent->getMain(), $name, $subscriptionManger );
$module->parent = $parent;
return $module;
}
/**
* @param ApiMain $mainModule
* @param string $moduleName
* @param SubscriptionManager $subscriptionManager
*/
public function __construct(
ApiMain $mainModule,
string $moduleName,
SubscriptionManager $subscriptionManager
) {
parent::__construct( $mainModule, $moduleName );
$this->subscriptionManager = $subscriptionManager;
}
/**
* Entry point for executing the module.
* @inheritDoc
*/
public function execute(): void {
$provider = $this->getParameter( 'provider' );
$token = $this->getParameter( 'providertoken' );
$topic = null;
// check if metadata is a JSON string correctly encoded
if ( $provider === 'apns' ) {
$topic = $this->getParameter( 'topic' );
if ( !$topic ) {
$this->dieWithError( 'apierror-echo-push-topic-required' );
}
}
$userId = Utils::getPushUserId( $this->getUser() );
$success = $this->subscriptionManager->create( $provider, $token, $userId, $topic );
if ( !$success ) {
$this->dieWithError( 'apierror-echo-push-token-exists' );
}
}
/**
* Get the parent module.
* @return ApiBase
*/
public function getParent(): ApiBase {
return $this->parent;
}
/** @inheritDoc */
protected function getAllowedParams(): array {
return [
'provider' => [
ParamValidator::PARAM_TYPE => self::PROVIDERS,
ParamValidator::PARAM_REQUIRED => true,
],
'providertoken' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true,
],
'topic' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false,
],
];
}
/** @inheritDoc */
protected function getExamplesMessages(): array {
return [
"action=echopushsubscriptions&command=create&provider=fcm&providertoken=ABC123" =>
"apihelp-echopushsubscriptions+create-example"
];
}
// The parent module already enforces these but they make documentation nicer.
/** @inheritDoc */
public function isWriteMode(): bool {
return true;
}
/** @inheritDoc */
public function mustBePosted(): bool {
return true;
}
/** @inheritDoc */
public function isInternal(): bool {
// experimental!
return true;
}
}