Special:TopicSubscriptions: Perform unsubscribe asynchronously

Bug: T327662
Change-Id: Ib7f5cf8b075f44ac06c0a014aa1e200ccda8fe26
This commit is contained in:
Ed Sanders 2023-02-04 14:30:14 +00:00
parent e89840cbae
commit 15a8074002
4 changed files with 51 additions and 0 deletions

View file

@ -30,6 +30,8 @@ class SpecialTopicSubscriptions extends SpecialPage {
parent::execute( $subpage );
$this->getOutput()->addModules( [ 'ext.discussionTools.init' ] );
$this->getOutput()->addHtml( $this->msg( 'discussiontools-topicsubscription-special-intro' )->parseAsBlock() );
$this->getOutput()->enableOOUI();

View file

@ -92,12 +92,18 @@ class TopicSubscriptionsPager extends TablePager {
$title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title );
return (string)new OOUI\ButtonWidget( [
'label' => $this->msg( 'discussiontools-topicsubscription-pager-unsubscribe-button' )->text(),
'classes' => [ 'ext-discussiontools-special-unsubscribe-button' ],
'framed' => false,
'flags' => [ 'destructive' ],
'data' => [
'item' => $row->sub_item,
'title' => $title->getPrefixedText(),
],
'href' => $title->getLinkURL( [
'action' => 'dtunsubscribe',
'commentname' => $row->sub_item,
] ),
'infusable' => true,
] );
default:

View file

@ -75,6 +75,11 @@ if ( url.searchParams.get( 'dtdebug' ) ) {
mw.hook( 'wikipage.content' ).add( mw.dt.init );
}
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'TopicSubscriptions' ) {
var topicSubscriptions = require( './topicsubscriptions.js' );
topicSubscriptions.initSpecialTopicSubscriptions();
}
module.exports = {
controller: controller,
Parser: require( './Parser.js' ),

View file

@ -51,6 +51,14 @@ function updateSubscribeButton( button, state ) {
}
}
/**
* Change the subscription state of a topic subscription
*
* @param {string} title Page title
* @param {string} commentName Comment name
* @param {boolean} subscribe Subscription state
* @return {jQuery.Promise} Promise which resolves after change of state
*/
function changeSubscription( title, commentName, subscribe ) {
var promise = api.postWithToken( 'csrf', {
action: 'discussiontoolssubscribe',
@ -183,6 +191,35 @@ function initTopicSubscriptions( $container, threadItemSet ) {
} );
}
function initSpecialTopicSubscriptions() {
api = require( './controller.js' ).getApi();
// Unsubscribe links on special page
// eslint-disable-next-line no-jquery/no-global-selector
$( '.ext-discussiontools-special-unsubscribe-button' ).each( function () {
var button = OO.ui.infuse( this );
var data = button.getData();
var subscribedState = STATE_SUBSCRIBED;
button.on( 'click', function () {
button.setDisabled( true );
changeSubscription( data.title, data.item, !subscribedState )
.then( function ( result ) {
button.setLabel( mw.msg(
result.subscribe ?
'discussiontools-topicsubscription-button-unsubscribe-label' :
'discussiontools-topicsubscription-button-subscribe-label'
) );
button.clearFlags();
button.setFlags( [ result.subscribe ? 'destructive' : 'progressive' ] );
subscribedState = result.subscribe ? STATE_SUBSCRIBED : STATE_UNSUBSCRIBED;
} ).always( function () {
button.setDisabled( false );
} );
} );
} );
}
/**
* Show the first time popup for auto topic subscriptions, if required
*/
@ -400,5 +437,6 @@ function updateAutoSubscriptionStates( $container, threadItemSet, threadItemId )
module.exports = {
initTopicSubscriptions: initTopicSubscriptions,
initSpecialTopicSubscriptions: initSpecialTopicSubscriptions,
updateAutoSubscriptionStates: updateAutoSubscriptionStates
};