From 8bb36bd782ce4136bb443a5c97c2fa69cf9aec45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Thu, 19 Aug 2021 22:35:32 +0200 Subject: [PATCH] Add API to get the status of topic subscriptions on a page (Split off from Ic0fabda0de4ebbc5e424f49641e6b03ebb4b7e6a) Bug: T290185 Change-Id: Iacc0f92bedfcb49e8a05d98af4d8170d08b1c8de --- extension.json | 7 ++ i18n/api/en.json | 2 + i18n/api/qqq.json | 2 + .../ApiDiscussionToolsGetSubscriptions.php | 83 +++++++++++++++++++ includes/SubscriptionItem.php | 9 ++ 5 files changed, 103 insertions(+) create mode 100644 includes/ApiDiscussionToolsGetSubscriptions.php diff --git a/extension.json b/extension.json index 244d2d614..a960e0f17 100644 --- a/extension.json +++ b/extension.json @@ -375,6 +375,13 @@ "DiscussionTools.SubscriptionStore", "ConfigFactory" ] + }, + "discussiontoolsgetsubscriptions": { + "class": "MediaWiki\\Extension\\DiscussionTools\\ApiDiscussionToolsGetSubscriptions", + "services": [ + "DiscussionTools.SubscriptionStore", + "ConfigFactory" + ] } }, "Hooks": { diff --git a/i18n/api/en.json b/i18n/api/en.json index 1a41440e4..033e964aa 100644 --- a/i18n/api/en.json +++ b/i18n/api/en.json @@ -18,6 +18,8 @@ "apihelp-discussiontoolsedit-paramvalue-paction-addcomment": "Add a new comment as a reply to an existing comment.", "apihelp-discussiontoolsedit-paramvalue-paction-addtopic": "Add a new discussion section and the first comment in it.", "apihelp-discussiontoolsedit-summary": "Post a message on a discussion page.", + "apihelp-discussiontoolsgetsubscriptions-param-commentname": "Names of the topics to check", + "apihelp-discussiontoolsgetsubscriptions-summary": "Get the subscription statuses of given topics.", "apihelp-discussiontoolssubscribe-param-page": "A page on which the topic appears", "apihelp-discussiontoolssubscribe-param-commentname": "Name of the topic to subscribe to (or unsubscribe from)", "apihelp-discussiontoolssubscribe-param-subscribe": "True to subscribe, false to unsubscribe", diff --git a/i18n/api/qqq.json b/i18n/api/qqq.json index 7a31efc88..8651208f6 100644 --- a/i18n/api/qqq.json +++ b/i18n/api/qqq.json @@ -20,6 +20,8 @@ "apihelp-discussiontoolsedit-paramvalue-paction-addcomment": "{{doc-apihelp-paramvalue|discussiontoolsedit|paction|addcomment}}", "apihelp-discussiontoolsedit-paramvalue-paction-addtopic": "{{doc-apihelp-paramvalue|discussiontoolsedit|paction|addtopic}}", "apihelp-discussiontoolsedit-summary": "{{doc-apihelp-summary|discussiontoolsedit}}", + "apihelp-discussiontoolsgetsubscriptions-param-commentname": "{{doc-apihelp-param|discussiontoolsgetsubscriptions|commentname}}", + "apihelp-discussiontoolsgetsubscriptions-summary": "{{doc-apihelp-summary|discussiontoolsgetsubscriptions}}", "apihelp-discussiontoolssubscribe-param-page": "{{doc-apihelp-param|discussiontoolssubscribe|page}}", "apihelp-discussiontoolssubscribe-param-commentname": "{{doc-apihelp-param|discussiontoolssubscribe|commentname}}", "apihelp-discussiontoolssubscribe-param-subscribe": "{{doc-apihelp-param|discussiontoolssubscribe|subscribe}}", diff --git a/includes/ApiDiscussionToolsGetSubscriptions.php b/includes/ApiDiscussionToolsGetSubscriptions.php new file mode 100644 index 000000000..c1bedf5e4 --- /dev/null +++ b/includes/ApiDiscussionToolsGetSubscriptions.php @@ -0,0 +1,83 @@ +subscriptionStore = $subscriptionStore; + $this->configFactory = $configFactory; + } + + /** + * @inheritDoc + */ + public function execute() { + $dtConfig = $this->configFactory->makeConfig( 'discussiontools' ); + if ( !$dtConfig->get( 'DiscussionToolsEnableTopicSubscriptionBackend' ) ) { + $this->dieWithError( [ 'apierror-moduledisabled', $this->getModuleName() ] ); + } + + $user = $this->getUser(); + if ( !$user->isRegistered() ) { + $this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' ); + } + + $params = $this->extractRequestParams(); + $itemNames = $params['commentname']; + $items = $this->subscriptionStore->getSubscriptionItemsForUser( + $user, + $itemNames + ); + + // Ensure consistent formatting in JSON and XML formats + $this->getResult()->addIndexedTagName( 'subscriptions', 'subscription' ); + $this->getResult()->addArrayType( 'subscriptions', 'kvp', 'name' ); + + foreach ( $items as $item ) { + $this->getResult()->addValue( 'subscriptions', $item->getItemName(), $item->getState() ); + } + } + + /** + * @inheritDoc + */ + public function getAllowedParams() { + return [ + 'commentname' => [ + ParamValidator::PARAM_REQUIRED => true, + ApiBase::PARAM_ISMULTI => true, + ], + ]; + } + + /** + * @inheritDoc + */ + public function isInternal() { + return true; + } +} diff --git a/includes/SubscriptionItem.php b/includes/SubscriptionItem.php index aeed848c9..57b3624f0 100644 --- a/includes/SubscriptionItem.php +++ b/includes/SubscriptionItem.php @@ -80,6 +80,15 @@ class SubscriptionItem { return $this->notifiedTimestamp; } + /** + * Get the subscription status of this entry. + * + * @return int One of SubscriptionStore::STATE_* constants + */ + public function getState(): int { + return $this->state; + } + /** * Check if the notification is muted *