mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-28 09:40:41 +00:00
a3674974f1
Adds DB tables for storing push subscriptions, some DB interaction code for retrieving them within MediaWiki, and a set of API modules for managing them from the outside world. When testing this patch, be sure to run maintenance/update.php to create the new tables, and set $wgEchoEnablePush = true in LocalSettings.php to enable the API new API module. N.B. The current DB schema is centered on app push subscriptions. Web push subscriptions require slightly different handling, since they are provided by browsers as a JSON blob rather than a token string. How to handle web push subscriptions is a question we can defer until the time comes to add web push support. Subscription data is stored in the echo_push_subscription table, with provider names normalized into the echo_push_provider table. We expect to be looking up subscriptions by central user ID, so that column is indexed. The subscription data also includes a column to store SHA256 digests of the subscriber tokens. This is for use as a unique key constraint, since we expect every push token to be univerally unique, and the token values themselves may be too large to reasonably index in MySQL. Bug: T252899 Change-Id: I3928761b3fba12e54ff4850e9a05c68ec7772f62
118 lines
2.7 KiB
PHP
118 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace EchoPush\Api;
|
|
|
|
use ApiBase;
|
|
use ApiMain;
|
|
use DBError;
|
|
use EchoPush\SubscriptionManager;
|
|
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' );
|
|
try {
|
|
$this->subscriptionManager->create( $this->getUser(), $provider, $token );
|
|
} catch ( DBError $e ) {
|
|
$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,
|
|
],
|
|
];
|
|
}
|
|
|
|
/** @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;
|
|
}
|
|
|
|
}
|