2020-05-15 17:19:03 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-08 00:47:34 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Push\Api;
|
2020-05-15 17:19:03 +00:00
|
|
|
|
2024-10-19 22:55:03 +00:00
|
|
|
use MediaWiki\Api\ApiBase;
|
|
|
|
use MediaWiki\Api\ApiMain;
|
2022-04-08 00:38:27 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Push\SubscriptionManager;
|
|
|
|
use MediaWiki\Extension\Notifications\Push\Utils;
|
2022-11-13 07:53:42 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Services;
|
2020-05-15 17:19:03 +00:00
|
|
|
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
|
|
|
|
*/
|
2021-07-24 04:10:29 +00:00
|
|
|
public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsCreate {
|
2022-11-13 07:53:42 +00:00
|
|
|
$subscriptionManger = Services::getInstance()->getPushSubscriptionManager();
|
2020-05-15 17:19:03 +00:00
|
|
|
$module = new self( $parent->getMain(), $name, $subscriptionManger );
|
|
|
|
$module->parent = $parent;
|
|
|
|
return $module;
|
|
|
|
}
|
|
|
|
|
|
|
|
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' );
|
2020-08-03 18:27:47 +00:00
|
|
|
$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' );
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 20:34:21 +00:00
|
|
|
$userId = Utils::getPushUserId( $this->getUser() );
|
2021-07-23 13:23:31 +00:00
|
|
|
$success = $this->subscriptionManager->create( $provider, $token, $userId, $topic );
|
2020-06-09 23:08:14 +00:00
|
|
|
if ( !$success ) {
|
2020-05-15 17:19:03 +00:00
|
|
|
$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,
|
|
|
|
],
|
2020-08-03 18:27:47 +00:00
|
|
|
'topic' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
],
|
2020-05-15 17:19:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|