summaryrefslogtreecommitdiff
blob: 31b34a975502f47eafb8711030adf6017d2787ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace MediaWiki\Extension\Notifications\Push\Api;

use ApiBase;
use ApiMain;
use EchoServices;
use MediaWiki\Extension\Notifications\Push\SubscriptionManager;
use MediaWiki\Extension\Notifications\Push\Utils;
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;
	}

}