2021-01-26 23:18:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2022-10-28 18:24:02 +00:00
|
|
|
use ApiUsageException;
|
2021-01-26 23:18:22 +00:00
|
|
|
use Title;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
class ApiDiscussionToolsSubscribe extends ApiBase {
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private SubscriptionStore $subscriptionStore;
|
2021-09-07 15:25:56 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
2022-09-02 02:05:02 +00:00
|
|
|
string $name,
|
2022-02-04 18:22:10 +00:00
|
|
|
SubscriptionStore $subscriptionStore
|
2021-09-07 15:25:56 +00:00
|
|
|
) {
|
2021-01-26 23:18:22 +00:00
|
|
|
parent::__construct( $main, $name );
|
|
|
|
$this->subscriptionStore = $subscriptionStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2022-10-28 18:24:02 +00:00
|
|
|
* @throws ApiUsageException
|
2021-01-26 23:18:22 +00:00
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$user = $this->getUser();
|
2023-06-02 19:11:36 +00:00
|
|
|
if ( !$user->isNamed() ) {
|
2021-08-23 18:18:27 +00:00
|
|
|
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'notloggedin' );
|
2021-01-26 23:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$title = Title::newFromText( $params['page'] );
|
|
|
|
$result = null;
|
|
|
|
|
|
|
|
if ( !$title ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
|
|
|
}
|
|
|
|
$commentName = $params['commentname'];
|
|
|
|
$subscribe = $params['subscribe'];
|
|
|
|
|
|
|
|
if ( $subscribe ) {
|
2021-04-27 20:21:50 +00:00
|
|
|
$success = $this->subscriptionStore->addSubscriptionForUser(
|
2021-01-26 23:18:22 +00:00
|
|
|
$user,
|
|
|
|
$title,
|
|
|
|
$commentName
|
|
|
|
);
|
2021-04-27 20:21:50 +00:00
|
|
|
if ( !$success ) {
|
|
|
|
$this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' );
|
|
|
|
}
|
2021-01-26 23:18:22 +00:00
|
|
|
} else {
|
2021-04-27 20:21:50 +00:00
|
|
|
$success = $this->subscriptionStore->removeSubscriptionForUser(
|
2021-01-26 23:18:22 +00:00
|
|
|
$user,
|
|
|
|
$commentName
|
|
|
|
);
|
2021-04-27 20:21:50 +00:00
|
|
|
if ( !$success ) {
|
|
|
|
$this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' );
|
|
|
|
}
|
2021-01-26 23:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
'page' => $title,
|
|
|
|
'commentname' => $commentName,
|
2021-04-27 20:21:50 +00:00
|
|
|
'subscribe' => $subscribe,
|
2021-01-26 23:18:22 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'page' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
],
|
|
|
|
'token' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
],
|
|
|
|
'commentname' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
],
|
|
|
|
'subscribe' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'boolean',
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function needsToken() {
|
|
|
|
return 'csrf';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|