Jdlrobson has submitted this change and it was merged. Change subject: Bug 41519: Only request token when clicking the star ......................................................................
Bug 41519: Only request token when clicking the star Currently we are making unnecessary token requests. This changes things so we only make one when the user hits the star. Document functions in process Bug 41519 Change-Id: I9512c26a3f7ae020bf604a7b94283821a08606a3 --- M javascripts/modules/mf-watchstar.js 1 file changed, 37 insertions(+), 19 deletions(-) Approvals: Jdlrobson: Verified; Looks good to me, approved diff --git a/javascripts/modules/mf-watchstar.js b/javascripts/modules/mf-watchstar.js index baf88b4..dff5fac 100644 --- a/javascripts/modules/mf-watchstar.js +++ b/javascripts/modules/mf-watchstar.js @@ -1,14 +1,21 @@ (function( M, $ ) { var api = M.require( 'api' ), w = ( function() { - var lastToken, nav = M.require( 'navigation' ), popup = M.require( 'notifications' ), + var nav = M.require( 'navigation' ), popup = M.require( 'notifications' ), drawer = new nav.CtaDrawer( { content: mw.msg( 'mobile-frontend-watchlist-cta' ), returnToQuery: 'article_action=watch' } ); // FIXME: this should live in a separate module and make use of MobileFrontend events - function logWatchEvent( eventType ) { + /** + * Checks whether a list of article titles are being watched by the current user + * Checks a local cache before making a query to server + * + * @param {Integer} eventType: 0=watched article, 1=stopped watching article, 2=clicked star as anonymous user + * @param {String} token: Token returned from getToken, optional when eventType is 2 + */ + function logWatchEvent( eventType, token ) { var types = [ 'watchlist', 'unwatchlist', 'anonCTA' ], data = { // FIXME: this gives wrong results when page loaded dynamically @@ -16,7 +23,7 @@ anon: mw.config.get( 'wgUserName' ) === null, action: types[ eventType ], isStable: mw.config.get( 'wgMFMode' ), - token: lastToken || '+\\', // +\\ for anon + token: eventType === 2 ? '+\\' : token, // +\\ for anon username: mw.config.get( 'wgUserName' ) || '' }; @@ -55,6 +62,13 @@ return $( '<a class="watch-this-article">' ).appendTo( container )[ 0 ]; } + /** + * Creates a watchlist button + * + * @param {jQuery} container: Element in which to create a watch star + * @param {String} title: The title to be watched + * @param {Boolean} isWatchedArticle: Whether the article is currently watched by the user or not + */ function createWatchListButton( container, title, isWatchedArticle ) { var prevent, watchBtn = createButton( container ); @@ -68,12 +82,12 @@ $( watchBtn ).removeClass( 'disabled loading' ); } - function success( data ) { + function success( data, token ) { if ( data.watch.hasOwnProperty( 'watched' ) ) { - logWatchEvent( 0 ); + logWatchEvent( 0, token ); $( watchBtn ).addClass( 'watched' ); } else { - logWatchEvent( 1 ); + logWatchEvent( 1, token ); $( watchBtn ).removeClass( 'watched' ); } enable(); @@ -81,7 +95,10 @@ function toggleWatchStatus( unwatch ) { api.getToken( 'watch' ).done( function( token ) { - toggleWatch( title, token, unwatch, success, enable ); + toggleWatch( title, token, unwatch, + function( data ) { + success( data, token ); + }, enable ); } ); } @@ -143,13 +160,18 @@ } } + /** + * Creates a watch star OR a drawer to encourage user to register / login + * + * @param {jQuery} container: A jQuery container to create a watch list button + * @param {String} title: The name of the article to watch + */ function initWatchListIcon( container, title ) { - api.getToken( 'watch' ).done( function( token ) { - lastToken = token; + if ( M.isLoggedIn() ) { checkWatchStatus( [ title ], function( status ) { createWatchListButton( container, title, status[ title ] ); } ); - } ).fail( function() { + } else { $( createButton( container ) ).click( function( ev ) { if ( !drawer.isVisible() ) { // log if enabled @@ -160,7 +182,7 @@ } ev.stopPropagation(); } ); - } ); + } } /** @@ -184,14 +206,10 @@ createWatchListButton( this, title, true ); } ); } else { - api.getToken( 'watch' ).done( function( token ) { - lastToken = token; - - checkWatchStatus( titles, function( status ) { - $container.find( 'li' ).each( function() { - var title = $( this ).attr( 'title' ); - createWatchListButton( this, title, status[ title ] ); - } ); + checkWatchStatus( titles, function( status ) { + $container.find( 'li' ).each( function() { + var title = $( this ).attr( 'title' ); + createWatchListButton( this, title, status[ title ] ); } ); } ); } -- To view, visit https://gerrit.wikimedia.org/r/56411 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9512c26a3f7ae020bf604a7b94283821a08606a3 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> Gerrit-Reviewer: JGonera <jgon...@wikimedia.org> Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits