From 703642e40e0d5989ec27de4c40c1b1c54cddf5eb Mon Sep 17 00:00:00 2001 From: Glaisher Date: Tue, 24 Nov 2015 22:27:25 +0500 Subject: [PATCH] Hide thanks button for botedits if bots are not allowed to receive thanks in mobile as well Also only load 'ext.thanks.mobilediff' module if the recipient is not an IP. Bug: T118686 Change-Id: I096d3b05a46590db426ae038f17f6e180fa5bf37 --- Thanks.hooks.php | 60 +++++++++++++++-------- modules/ext.thanks.mobilediff.js | 9 ++-- tests/qunit/test_ext.thanks.mobilediff.js | 8 --- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Thanks.hooks.php b/Thanks.hooks.php index 6cb1d3dc..bce2849a 100644 --- a/Thanks.hooks.php +++ b/Thanks.hooks.php @@ -41,31 +41,48 @@ class ThanksHooks { * @return bool */ public static function insertThankLink( $rev, &$links, $oldRev = null ) { - global $wgUser, $wgThanksSendToBots; + global $wgUser; + + $recipientId = $rev->getUser(); + $recipient = User::newFromId( $recipientId ); // Make sure Echo is turned on. - // Exclude anonymous users. // Don't let users thank themselves. + // Exclude anonymous users. // Exclude users who are blocked. + // Check whether bots are allowed to receive thanks. if ( class_exists( 'EchoNotifier' ) && !$wgUser->isAnon() - && $rev->getUser() !== $wgUser->getId() + && $recipientId !== $wgUser->getId() && !$wgUser->isBlocked() + && self::canReceiveThanks( $recipient ) && !$rev->isDeleted( Revision::DELETED_TEXT ) && ( !$oldRev || $rev->getParentId() == $oldRev->getId() ) ) { - $recipient = User::newFromId( $rev->getUser() ); - $recipientAllowed = true; - // If bots are not allowed, exclude them as recipients - if ( !$wgThanksSendToBots ) { - $recipientAllowed = !in_array( 'bot', $recipient->getGroups() ); - } - if ( $recipientAllowed && !$recipient->isAnon() ) { - $links[] = self::generateThankElement( $rev, $recipient ); - } + $links[] = self::generateThankElement( $rev, $recipient ); } return true; } + /** + * Check whether a user is allowed to receive thanks or not + * + * @param User $user Recipient + * @return bool true if allowed, false if not + */ + protected static function canReceiveThanks( User $user ) { + global $wgThanksSendToBots; + + if ( $user->isAnon() ) { + return false; + } + + if ( !$wgThanksSendToBots && in_array( 'bot', $user->getGroups() ) ) { + return false; + } + + return true; + } + /** * Helper for self::insertThankLink * Creates either a thank link or thanked span based on users session @@ -239,20 +256,23 @@ class ThanksHooks { * @return bool true in all cases */ public static function onBeforeSpecialMobileDiffDisplay( &$output, $ctx, $revisions ) { + $rev = $revisions[1]; + // If the Echo and MobileFrontend extensions are installed and the user is - // logged in, show a 'Thank' link. - if ( class_exists( 'EchoNotifier' ) + // logged in or recipient is not a bot if bots cannot receive thanks, show a 'Thank' link. + if ( $rev + && class_exists( 'EchoNotifier' ) && class_exists( 'SpecialMobileDiff' ) + && self::canReceiveThanks( User::newFromId( $rev->getUser() ) ) && $output->getUser()->isLoggedIn() ) { $output->addModules( array( 'ext.thanks.mobilediff' ) ); - $rev = $revisions[1]; - if ( $rev ) { - if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) { - // User already sent thanks for this revision - $output->addJsConfigVars( 'wgThanksAlreadySent', true ); - } + + if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) { + // User already sent thanks for this revision + $output->addJsConfigVars( 'wgThanksAlreadySent', true ); } + } return true; } diff --git a/modules/ext.thanks.mobilediff.js b/modules/ext.thanks.mobilediff.js index ef34ff9f..a897ae3d 100644 --- a/modules/ext.thanks.mobilediff.js +++ b/modules/ext.thanks.mobilediff.js @@ -88,12 +88,11 @@ gender = $user.data( 'user-gender' ), $thankBtn; - if ( !$user.hasClass( 'mw-mf-anon' ) ) { - $thankBtn = createThankLink( username, rev, gender ); - if ( $thankBtn ) { - $thankBtn.prependTo( $container ); - } + $thankBtn = createThankLink( username, rev, gender ); + if ( $thankBtn ) { + $thankBtn.prependTo( $container ); } + } $( function () { diff --git a/tests/qunit/test_ext.thanks.mobilediff.js b/tests/qunit/test_ext.thanks.mobilediff.js index ecf6cacc..f588cecf 100644 --- a/tests/qunit/test_ext.thanks.mobilediff.js +++ b/tests/qunit/test_ext.thanks.mobilediff.js @@ -11,12 +11,4 @@ assert.strictEqual( $container.find( 'button' ).length, 1, 'Thanks button was created.' ); } ); - QUnit.test( 'Do not render button for anon users', 1, function ( assert ) { - var $container = $( '
' ), - $user = $( '
' ); - - mw.thanks._mobileDiffInit( $user, $container ); - assert.strictEqual( $container.find( 'button' ).length, 0, 'No thanks button was created.' ); - } ); - }( jQuery ) );