Merge "Hide thanks button for botedits if bots are not allowed to receive thanks in mobile as well"

This commit is contained in:
jenkins-bot 2015-11-30 21:13:46 +00:00 committed by Gerrit Code Review
commit ff0e5e436b
3 changed files with 44 additions and 33 deletions

View file

@ -41,31 +41,48 @@ class ThanksHooks {
* @return bool * @return bool
*/ */
public static function insertThankLink( $rev, &$links, $oldRev = null ) { 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. // Make sure Echo is turned on.
// Exclude anonymous users.
// Don't let users thank themselves. // Don't let users thank themselves.
// Exclude anonymous users.
// Exclude users who are blocked. // Exclude users who are blocked.
// Check whether bots are allowed to receive thanks.
if ( class_exists( 'EchoNotifier' ) if ( class_exists( 'EchoNotifier' )
&& !$wgUser->isAnon() && !$wgUser->isAnon()
&& $rev->getUser() !== $wgUser->getId() && $recipientId !== $wgUser->getId()
&& !$wgUser->isBlocked() && !$wgUser->isBlocked()
&& self::canReceiveThanks( $recipient )
&& !$rev->isDeleted( Revision::DELETED_TEXT ) && !$rev->isDeleted( Revision::DELETED_TEXT )
&& ( !$oldRev || $rev->getParentId() == $oldRev->getId() ) && ( !$oldRev || $rev->getParentId() == $oldRev->getId() )
) { ) {
$recipient = User::newFromId( $rev->getUser() ); $links[] = self::generateThankElement( $rev, $recipient );
$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 );
}
} }
return true; 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 * Helper for self::insertThankLink
* Creates either a thank link or thanked span based on users session * Creates either a thank link or thanked span based on users session
@ -239,20 +256,23 @@ class ThanksHooks {
* @return bool true in all cases * @return bool true in all cases
*/ */
public static function onBeforeSpecialMobileDiffDisplay( &$output, $ctx, $revisions ) { public static function onBeforeSpecialMobileDiffDisplay( &$output, $ctx, $revisions ) {
$rev = $revisions[1];
// If the Echo and MobileFrontend extensions are installed and the user is // If the Echo and MobileFrontend extensions are installed and the user is
// logged in, show a 'Thank' link. // logged in or recipient is not a bot if bots cannot receive thanks, show a 'Thank' link.
if ( class_exists( 'EchoNotifier' ) if ( $rev
&& class_exists( 'EchoNotifier' )
&& class_exists( 'SpecialMobileDiff' ) && class_exists( 'SpecialMobileDiff' )
&& self::canReceiveThanks( User::newFromId( $rev->getUser() ) )
&& $output->getUser()->isLoggedIn() && $output->getUser()->isLoggedIn()
) { ) {
$output->addModules( array( 'ext.thanks.mobilediff' ) ); $output->addModules( array( 'ext.thanks.mobilediff' ) );
$rev = $revisions[1];
if ( $rev ) { if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) {
if ( $output->getRequest()->getSessionData( 'thanks-thanked-' . $rev->getId() ) ) { // User already sent thanks for this revision
// User already sent thanks for this revision $output->addJsConfigVars( 'wgThanksAlreadySent', true );
$output->addJsConfigVars( 'wgThanksAlreadySent', true );
}
} }
} }
return true; return true;
} }

View file

@ -88,12 +88,11 @@
gender = $user.data( 'user-gender' ), gender = $user.data( 'user-gender' ),
$thankBtn; $thankBtn;
if ( !$user.hasClass( 'mw-mf-anon' ) ) { $thankBtn = createThankLink( username, rev, gender );
$thankBtn = createThankLink( username, rev, gender ); if ( $thankBtn ) {
if ( $thankBtn ) { $thankBtn.prependTo( $container );
$thankBtn.prependTo( $container );
}
} }
} }
$( function () { $( function () {

View file

@ -11,12 +11,4 @@
assert.strictEqual( $container.find( 'button' ).length, 1, 'Thanks button was created.' ); 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 = $( '<div>' ),
$user = $( '<div class="mw-mf-anon">' );
mw.thanks._mobileDiffInit( $user, $container );
assert.strictEqual( $container.find( 'button' ).length, 0, 'No thanks button was created.' );
} );
}( jQuery ) ); }( jQuery ) );