mediawiki-extensions-Echo/includes/api/Push/ApiEchoPushSubscriptions.php
Michael Holloway a3674974f1 Add push subscription management
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
2020-06-02 13:40:00 -04:00

104 lines
2.4 KiB
PHP

<?php
namespace EchoPush\Api;
use ApiBase;
use ApiModuleManager;
use ApiUsageException;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
/**
* API parent module for administering push subscriptions.
* Each operation (command) is implemented as a submodule. This module just performs some basic
* checks and dispatches the execute() call.
*/
class ApiEchoPushSubscriptions extends ApiBase {
/** array Module name => module class */
private const SUBMODULES = [
'create' => ApiEchoPushSubscriptionsCreate::class,
'delete' => ApiEchoPushSubscriptionsDelete::class,
];
/** @var ApiModuleManager */
private $moduleManager;
/** @inheritDoc */
public function execute(): void {
$this->checkLoginState();
$this->checkUserRightsAny( 'editmyprivateinfo' );
$command = $this->getParameter( 'command' );
$module = $this->moduleManager->getModule( $command, 'command' );
$module->execute();
$module->getResult()->addValue(
null,
$module->getModuleName(),
[ 'result' => 'Success' ]
);
}
/** @inheritDoc */
public function getModuleManager(): ApiModuleManager {
if ( !$this->moduleManager ) {
$submodules = array_map( function ( $class ) {
return [
'class' => $class,
'factory' => "$class::factory",
];
}, self::SUBMODULES );
$this->moduleManager = new ApiModuleManager(
$this,
MediaWikiServices::getInstance()->getObjectFactory()
);
$this->moduleManager->addModules( $submodules, 'command' );
}
return $this->moduleManager;
}
/** @inheritDoc */
protected function getAllowedParams(): array {
return [
'command' => [
ParamValidator::PARAM_TYPE => 'submodule',
ParamValidator::PARAM_REQUIRED => true,
],
];
}
/**
* Bail out with an API error if the user is not logged in.
* @throws ApiUsageException
*/
private function checkLoginState(): void {
if ( $this->getUser()->isAnon() ) {
$this->dieWithError(
[ 'apierror-mustbeloggedin', $this->msg( 'action-editmyprivateinfo' ) ],
'notloggedin'
);
}
}
/** @inheritDoc */
public function getHelpUrls(): string {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:Echo#API';
}
/** @inheritDoc */
public function isWriteMode(): bool {
return true;
}
/** @inheritDoc */
public function needsToken(): string {
return 'csrf';
}
/** @inheritDoc */
public function isInternal(): bool {
// experimental!
return true;
}
}