jenkins-bot has submitted this change and it was merged.
Change subject: Prevent short-term duplicate thanking
......................................................................
Prevent short-term duplicate thanking
Thanked revision to be stored both server side in the session and client side
in a cookie. Thanked revisions found in either storage solution will replace
their 'thank' link with the string 'thanked'.
Bug: 46690
Change-Id: I976cd8fbf00856c67b77daeb0d0a952efb371661
---
M ApiThank.php
M Thanks.hooks.php
M modules/ext.thanks.thank.js
3 files changed, 80 insertions(+), 13 deletions(-)
Approvals:
Kaldari: Looks good to me, approved
jenkins-bot: Verified
diff --git a/ApiThank.php b/ApiThank.php
index b13d547..23f72b7 100644
--- a/ApiThank.php
+++ b/ApiThank.php
@@ -57,6 +57,8 @@
),
'agent' => $agent,
) );
+ // Mark the thank in session to prevent
duplicates (Bug 46690)
+ $agent->getRequest()->setSessionData(
"thanks-thanked-{$rev->getId()}", true );
// Set success message
$result['success'] = '1';
// Log it if we're supposed to log it
diff --git a/Thanks.hooks.php b/Thanks.hooks.php
index 1c280c8..619063e 100644
--- a/Thanks.hooks.php
+++ b/Thanks.hooks.php
@@ -34,25 +34,44 @@
$recipientAllowed = !in_array( 'bot',
$recipient->getGroups() );
}
if ( $recipientAllowed && !$recipient->isAnon() ) {
- // Add 'thank' link
- $tooltip = wfMessage( 'thanks-thank-tooltip'
)->text();
- $thankLink = Html::element(
- 'a',
- array(
- 'class' =>
'mw-thanks-thank-link',
- 'href' => '#',
- 'title' => $tooltip,
- 'data-revision-id' =>
$rev->getId(),
- ),
- wfMessage( 'thanks-thank' )->plain()
- );
- $links[] = $thankLink;
+ $links[] = self::generateThankElement( $rev );
}
}
return true;
}
/**
+ * Helper for self::insertThankLink
+ * Creates either a thank link or thanked span based on users session
+ * @param $rev Revision object to generate the thank element for
+ */
+ protected static function generateThankElement( $rev ) {
+ global $wgUser;
+ // User has already thanked for revision
+ if ( $wgUser->getRequest()->getSessionData(
"thanks-thanked-{$rev->getId()}" ) ) {
+ return Html::element(
+ 'span',
+ array( 'class' => 'mw-thanks-thanked' ),
+ wfMessage( 'thanks-thanked', $wgUser )->parse()
+ );
+ }
+
+ // Add 'thank' link
+ $tooltip = wfMessage( 'thanks-thank-tooltip' )->text();
+
+ return Html::element(
+ 'a',
+ array(
+ 'class' => 'mw-thanks-thank-link',
+ 'href' => '#',
+ 'title' => $tooltip,
+ 'data-revision-id' => $rev->getId(),
+ ),
+ wfMessage( 'thanks-thank' )->plain()
+ );
+ }
+
+ /**
* Handler for PageHistoryBeforeList hook.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/PageHistoryBeforeList
* @param &$page WikiPage|Article|ImagePage|CategoryPage|Page The page
that the history is loading for.
diff --git a/modules/ext.thanks.thank.js b/modules/ext.thanks.thank.js
index 1b5c7d4..29cbe48 100644
--- a/modules/ext.thanks.thank.js
+++ b/modules/ext.thanks.thank.js
@@ -1,6 +1,51 @@
( function ( $, mw ) {
'use strict';
+ var thanked = {
+ maxHistory: 100,
+ load: function() {
+ var cookie = $.cookie( 'thanks-thanked' );
+ if ( cookie === null ) {
+ return [];
+ }
+ return unescape( cookie ).split( ',' );
+ },
+ push: function( $thankLink ) {
+ var saved = this.load();
+ saved.push( $thankLink.attr( 'data-revision-id' ) );
+ if ( saved.length > this.maxHistory ) { // prevent
forever growing
+ saved = saved.slice( saved.length -
this.maxHistory );
+ }
+ $.cookie( 'thanks-thanked', escape( saved.join( ',' ) )
);
+ },
+ contains: function( $thankLink ) {
+ // $.inArray returns the index position or -1 if
non-existant
+ if ( $.inArray( $thankLink.attr( 'data-revision-id' ),
this.load() ) !== -1 ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ };
+
+ var reloadThankedState = function() {
+ $( 'a.mw-thanks-thank-link' ).each( function( idx, el ) {
+ var $thankLink = $( el );
+ if ( thanked.contains( $thankLink ) ) {
+ $thankLink.before( mw.msg( 'thanks-thanked' ) );
+ $thankLink.remove();
+ }
+ } );
+ };
+
+ if ( $.isReady ) {
+ // This condition is required for soft-reloads
+ // to also trigger the reloadThankedState
+ reloadThankedState();
+ } else {
+ $( document ).ready( reloadThankedState );
+ }
+
$( 'a.mw-thanks-thank-link' ).click( function( e ) {
var source, $thankLink = $( this );
e.preventDefault();
@@ -18,6 +63,7 @@
.done( function( data ) {
$thankLink.before( mw.msg( 'thanks-thanked' ) );
$thankLink.remove();
+ thanked.push( $thankLink );
} )
.fail( function( errorCode, details ) {
// TODO: use something besides alert for the error
messages
--
To view, visit https://gerrit.wikimedia.org/r/60914
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I976cd8fbf00856c67b77daeb0d0a952efb371661
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Thanks
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Swalling <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits