| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Push\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Extension\Notifications\Push\SubscriptionManager; |
| 8 |
use MediaWiki\Extension\Notifications\Push\Utils; |
| 9 |
use MediaWiki\Extension\Notifications\Services; |
| 10 |
use Wikimedia\ParamValidator\ParamValidator; |
| 11 |
|
| 12 |
class ApiEchoPushSubscriptionsCreate extends ApiBase { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private const PROVIDERS = [ 'fcm', 'apns' ]; |
| 20 |
|
| 21 |
|
| 22 |
private $parent; |
| 23 |
|
| 24 |
|
| 25 |
private $subscriptionManager; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsCreate { |
| 34 |
$subscriptionManger = Services::getInstance()->getPushSubscriptionManager(); |
| 35 |
$module = new self( $parent->getMain(), $name, $subscriptionManger ); |
| 36 |
$module->parent = $parent; |
| 37 |
return $module; |
| 38 |
} |
| 39 |
|
| 40 |
public function __construct( |
| 41 |
ApiMain $mainModule, |
| 42 |
string $moduleName, |
| 43 |
SubscriptionManager $subscriptionManager |
| 44 |
) { |
| 45 |
parent::__construct( $mainModule, $moduleName ); |
| 46 |
$this->subscriptionManager = $subscriptionManager; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function execute(): void { |
| 54 |
$provider = $this->getParameter( 'provider' ); |
| 55 |
$token = $this->getParameter( 'providertoken' ); |
| 56 |
$topic = null; |
| 57 |
|
| 58 |
if ( $provider === 'apns' ) { |
| 59 |
$topic = $this->getParameter( 'topic' ); |
| 60 |
if ( !$topic ) { |
| 61 |
$this->dieWithError( 'apierror-echo-push-topic-required' ); |
| 62 |
} |
| 63 |
} |
| 64 |
$userId = Utils::getPushUserId( $this->getUser() ); |
| 65 |
$success = $this->subscriptionManager->create( $provider, $token, $userId, $topic ); |
| 66 |
if ( !$success ) { |
| 67 |
$this->dieWithError( 'apierror-echo-push-token-exists' ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getParent(): ApiBase { |
| 76 |
return $this->parent; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
protected function getAllowedParams(): array { |
| 81 |
return [ |
| 82 |
'provider' => [ |
| 83 |
ParamValidator::PARAM_TYPE => self::PROVIDERS, |
| 84 |
ParamValidator::PARAM_REQUIRED => true, |
| 85 |
], |
| 86 |
'providertoken' => [ |
| 87 |
ParamValidator::PARAM_TYPE => 'string', |
| 88 |
ParamValidator::PARAM_REQUIRED => true, |
| 89 |
], |
| 90 |
'topic' => [ |
| 91 |
ParamValidator::PARAM_TYPE => 'string', |
| 92 |
ParamValidator::PARAM_REQUIRED => false, |
| 93 |
], |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
protected function getExamplesMessages(): array { |
| 99 |
return [ |
| 100 |
"action=echopushsubscriptions&command=create&provider=fcm&providertoken=ABC123" => |
| 101 |
"apihelp-echopushsubscriptions+create-example" |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function isWriteMode(): bool { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
public function mustBePosted(): bool { |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
public function isInternal(): bool { |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
} |