mediawiki-extensions-Discus.../includes/ApiDiscussionToolsSubscribe.php
Bartosz Dziewoński 6a50d1203c Limit number of topic subscriptions per user
Per Manuel Arostegui in T263817#7033384. The limit is 5000.
(I picked it arbitrarily, there's no real rationale for it.)

Also log a warning when any user reaches half of the limit,
so that we might make a decision about changing this mechanism
before it starts affecting users. Maybe at that time we'll
have data to show that it's safe to remove the limit.

Bug: T263817
Change-Id: I18a8ee0ad7383759229c5721d5253fb591457d4d
2021-05-25 17:16:24 +02:00

136 lines
3.2 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools;
use ApiBase;
use ApiMain;
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
use MediaWiki\MediaWikiServices;
use Title;
use User;
use Wikimedia\ParamValidator\ParamValidator;
class ApiDiscussionToolsSubscribe extends ApiBase {
/** @var SubscriptionStore */
protected $subscriptionStore;
/**
* @param ApiMain $main
* @param string $name
* @param SubscriptionStore $subscriptionStore
*/
public function __construct( ApiMain $main, $name, SubscriptionStore $subscriptionStore ) {
parent::__construct( $main, $name );
$this->subscriptionStore = $subscriptionStore;
}
/**
* @inheritDoc
*/
public function execute() {
// This should probably use dependency injection, but the check is only temporary
$services = MediaWikiServices::getInstance();
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
if ( $dtConfig->get( 'DiscussionTools_' . HookUtils::TOPICSUBSCRIPTION ) === 'unavailable' ) {
$this->dieWithError( [ 'apierror-moduledisabled', $this->getModuleName() ] );
}
$user = $this->getUser();
if ( !$user || $user->isAnon() ) {
// TODO: More specific error message
$this->dieWithError(
'apierror-mustbeloggedin-generic', 'notloggedin'
);
}
'@phan-var User $user';
$params = $this->extractRequestParams();
$title = Title::newFromText( $params['page'] );
$result = null;
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
return;
}
$commentName = $params['commentname'];
$subscribe = $params['subscribe'];
if ( $subscribe ) {
$success = $this->subscriptionStore->addSubscriptionForUser(
$user,
$title,
$commentName
);
if ( !$success ) {
$this->dieWithError( 'apierror-discussiontools-subscription-failed-add', 'subscription-failed' );
}
} else {
$success = $this->subscriptionStore->removeSubscriptionForUser(
$user,
$commentName
);
if ( !$success ) {
$this->dieWithError( 'apierror-discussiontools-subscription-failed-remove', 'subscription-failed' );
}
}
// TODO: Subscribe should be tri-state:
// * subscribe (add row)
// * ubsubscribe (delete row)
// * mute (set state=0)
$result = [
'page' => $title,
'commentname' => $commentName,
'subscribe' => $subscribe,
];
$this->getResult()->addValue( null, $this->getModuleName(), $result );
}
/**
* @inheritDoc
*/
public function getAllowedParams() {
return [
'page' => [
ParamValidator::PARAM_REQUIRED => true,
ApiBase::PARAM_HELP_MSG => 'apihelp-visualeditoredit-param-page',
],
'token' => [
ParamValidator::PARAM_REQUIRED => true,
],
'commentname' => [
ParamValidator::PARAM_REQUIRED => true,
ApiBase::PARAM_HELP_MSG => 'apihelp-discussiontoolsedit-param-commentname',
],
'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;
}
}