From ccdca0d0d7e024d8d5b9cbe72f094e108386a929 Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Mon, 14 Jul 2014 12:08:43 -0700 Subject: [PATCH] Support updated flow frontend * Flow no-longer allows arbitrary html insertion, so remove * Update ext.thanks.flowthank.js to work with thank links generated directly by flow. Change-Id: I8ae14352f1b177446f3696ffadd6921a5125238e --- Thanks.hooks.php | 84 ++++----------------------------- Thanks.php | 4 +- modules/ext.thanks.flowthank.js | 12 ++--- 3 files changed, 15 insertions(+), 85 deletions(-) diff --git a/Thanks.hooks.php b/Thanks.hooks.php index 263ec7ff..4443875e 100644 --- a/Thanks.hooks.php +++ b/Thanks.hooks.php @@ -272,86 +272,18 @@ class ThanksHooks { } /** - * Handler for FlowAddModules + * Handler for FlowAddModules. Inserts javascript to enhance thank + * links from static urls to in-page dialogs along with reloading + * the previously thanked state. + * * @param OutputPage $out OutputPage object + * @param mixed $skin * @return bool */ - public static function onFlowAddModules( OutputPage $out ) { - $out->addModules( 'ext.thanks.flowthank' ); - return true; - } - - /** - * Handler for FlowAddInteractionLinks - * Appends a 'thank' button or a 'thanked' label to interaction links for Flow comments - * @param Flow\Model\PostRevision $post Flow PostRevision object for the comment - * @param User $user User viewing the page - * @param array $links Array of interaction links to be displayed - * @return bool - */ - public static function onFlowAddPostInteractionLinks( $post, $user, &$links ) { - global $wgThanksSendToBots; - // Make sure Echo is turned on. - // Exclude anonymous users. - // Don't let users thank themselves. - // Exclude users who are blocked. - if ( class_exists( 'EchoNotifier' ) - && !$user->isAnon() - && $post->getCreatorId() != $user->getId() - && !$user->isBlocked() - ) { - $recipient = User::newFromId( $post->getCreatorId() ); - $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::generateFlowThankElement( $post, $user, $recipient ); - } + public static function onBeforePageDisplay( OutputPage $out, $skin ) { + if ( $out->getTitle()->getContentModel() === 'flow-board' ) { + $out->addModules( 'ext.thanks.flowthank' ); } return true; } - - /** - * Creates either a 'thank' button or a 'thanked' label based on session data - * @param Flow\Model\PostRevision $post Flow PostRevision object for the comment - * @param User $user User viewing the page - * @param User $recipient User who receives thanks notification - * @return string HTML segment for the links - */ - protected static function generateFlowThankElement( $post, $user, $recipient ) { - $cssActiveClass = 'mw-thanks-flow-thank-link mw-ui-button mw-ui-quiet mw-ui-constructive'; - $cssInactiveClass = 'mw-thanks-flow-thanked mw-ui-button mw-ui-quiet mw-ui-disabled'; - - $uuid = $post->getPostId()->getAlphadecimal(); - - // User has already thanked for revision - if ( $user->getRequest()->getSessionData( "flow-thanked-{$uuid}" ) ) { - return Html::rawElement( - 'span', - array( 'class' => $cssInactiveClass ), - wfMessage( 'thanks-button-thanked', $user )->escaped() - ); - } - - // Add 'thank' link - $tooltip = wfMessage( 'thanks-thank-tooltip' ) - ->params( $user->getName(), $recipient->getName() ) - ->text(); - - return Html::rawElement( - 'a', - array( - 'class' => $cssActiveClass, - 'href' => SpecialPage::getTitleFor( - 'Thanks', - 'Flow/' . $uuid - )->getFullURL(), - 'title' => $tooltip, - 'data-post-id' => $uuid - ), - wfMessage( 'thanks-button-thank', $user )->escaped() - ); - } } diff --git a/Thanks.php b/Thanks.php index 6edbaa4e..62204d89 100644 --- a/Thanks.php +++ b/Thanks.php @@ -73,9 +73,7 @@ $wgHooks['AddNewAccount'][] = 'ThanksHooks::onAccountCreated'; $wgHooks['BeforeSpecialMobileDiffDisplay'][] = 'ThanksHooks::onBeforeSpecialMobileDiffDisplay'; $wgHooks['UnitTestsList'][] = 'ThanksHooks::registerUnitTests'; $wgHooks['GetLogTypesOnUser'][] = 'ThanksHooks::onGetLogTypesOnUser'; - -$wgHooks['FlowAddPostInteractionLinks'][] = 'ThanksHooks::onFlowAddPostInteractionLinks'; -$wgHooks['FlowAddModules'][] = 'ThanksHooks::onFlowAddModules'; +$wgHooks['BeforePageDisplay'][] = 'ThanksHooks::onBeforePageDisplay'; // Register modules $wgResourceModules['ext.thanks'] = array( diff --git a/modules/ext.thanks.flowthank.js b/modules/ext.thanks.flowthank.js index 0864a37f..dbbab225 100644 --- a/modules/ext.thanks.flowthank.js +++ b/modules/ext.thanks.flowthank.js @@ -2,16 +2,16 @@ 'use strict'; mw.thanks.thanked.cookieName = 'flow-thanked'; - mw.thanks.thanked.attrName = 'data-post-id'; + mw.thanks.thanked.attrName = 'data-flow-id'; var $thankedLabel = $( '' ) .append( mw.msg( 'thanks-button-thanked', mw.user ) ) - .addClass( 'mw-thanks-flow-thanked mw-ui-button mw-ui-quiet mw-ui-disabled' ); + .addClass( 'mw-thanks-flow-thanked mw-ui-quiet' ); var reloadThankedState = function() { $( 'a.mw-thanks-flow-thank-link' ).each( function( idx, el ) { var $thankLink = $( el ); - if ( mw.thanks.thanked.contains( $thankLink ) ) { + if ( mw.thanks.thanked.contains( $thankLink.closest( '.flow-post' ) ) ) { $thankLink.before( $thankedLabel.clone() ); $thankLink.remove(); } @@ -21,13 +21,13 @@ var sendFlowThanks = function( $thankLink ) { ( new mw.Api ).get( { 'action' : 'flowthank', - 'postid' : $thankLink.attr( 'data-post-id' ), + 'postid' : $thankLink.closest( '.flow-post' ).attr( mw.thanks.thanked.attrName ), 'token' : mw.user.tokens.get( 'editToken' ) } ) .done( function( data ) { + mw.thanks.thanked.push( $thankLink.closest( '.flow-post' ) ); $thankLink.before( $thankedLabel.clone() ); $thankLink.remove(); - mw.thanks.thanked.push( $thankLink ); } ) .fail( function( errorCode, details ) { // TODO: use something besides alert for the error messages @@ -50,7 +50,7 @@ } // .on() is needed to make the button work for dynamically loaded posts - $( '.flow-container' ).on( 'click', 'a.mw-thanks-flow-thank-link', function( e ) { + $( '.flow-board' ).on( 'click', 'a.mw-thanks-flow-thank-link', function( e ) { var $thankLink = $( this ); e.preventDefault(); sendFlowThanks( $thankLink );