Merge "Add API to get the status of topic subscriptions on a page"

This commit is contained in:
jenkins-bot 2021-09-21 04:43:03 +00:00 committed by Gerrit Code Review
commit c50b1630d1
5 changed files with 103 additions and 0 deletions

View file

@ -375,6 +375,13 @@
"DiscussionTools.SubscriptionStore",
"ConfigFactory"
]
},
"discussiontoolsgetsubscriptions": {
"class": "MediaWiki\\Extension\\DiscussionTools\\ApiDiscussionToolsGetSubscriptions",
"services": [
"DiscussionTools.SubscriptionStore",
"ConfigFactory"
]
}
},
"Hooks": {

View file

@ -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",

View file

@ -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}}",

View file

@ -0,0 +1,83 @@
<?php
namespace MediaWiki\Extension\DiscussionTools;
use ApiBase;
use ApiMain;
use ConfigFactory;
use Wikimedia\ParamValidator\ParamValidator;
class ApiDiscussionToolsGetSubscriptions extends ApiBase {
/** @var SubscriptionStore */
private $subscriptionStore;
/** @var ConfigFactory */
private $configFactory;
/**
* @param ApiMain $main
* @param string $name
* @param SubscriptionStore $subscriptionStore
* @param ConfigFactory $configFactory
*/
public function __construct(
ApiMain $main,
$name,
SubscriptionStore $subscriptionStore,
ConfigFactory $configFactory
) {
parent::__construct( $main, $name );
$this->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;
}
}

View file

@ -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
*