Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/246463

Change subject: Stub the API request made by WatchstarPageList
......................................................................

Stub the API request made by WatchstarPageList

The WatchstarPageList makes a request to the API to get the watched
status of all of the pages in the list. By getting this information as
part of the query made by the boostrap script and injecting a stubbed
WatchstarApi we can eliminate this request entirely.

Depends-On: I2668c05290b9284bbfab89a55753cca5a9911dbc
Change-Id: I45b01a8c1965b96903229cc9d0b7fe707acea571
---
M extension.json
M resources/ext.relatedArticles.readMore.bootstrap/index.js
M resources/ext.relatedArticles.readMore.minerva/index.js
3 files changed, 63 insertions(+), 8 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/RelatedArticles 
refs/changes/63/246463/1

diff --git a/extension.json b/extension.json
index 653da66..d77d9ac 100644
--- a/extension.json
+++ b/extension.json
@@ -70,6 +70,10 @@
                        "messages": [
                                "relatedarticles-read-more-heading"
                        ],
+                       "dependencies": [
+                               "mobile.watchstar",
+                               "mobile.pagelist.scripts"
+                       ],
                        "targets": [
                                "mobile"
                        ]
diff --git a/resources/ext.relatedArticles.readMore.bootstrap/index.js 
b/resources/ext.relatedArticles.readMore.bootstrap/index.js
index cc59ea6..7403424 100644
--- a/resources/ext.relatedArticles.readMore.bootstrap/index.js
+++ b/resources/ext.relatedArticles.readMore.bootstrap/index.js
@@ -13,6 +13,7 @@
         * * The ID of the page corresponding to the title
         * * The thumbnail, if any
         * * The Wikidata description, if any
+        * * Whether the page is watched by the user
         *
         * @private
         *
@@ -24,12 +25,12 @@
 
                return api.get( {
                        action: 'query',
-                       prop: 'pageimages|pageterms',
-                       wbptterms: 'description',
+                       prop: 'pageimages|pageterms|info',
+                       titles: titles,
                        pilimit: titles.length,
-                       'continue': '',
-
-                       titles: titles
+                       wbptterms: 'description',
+                       inprop: 'watched',
+                       'continue': ''
                } ).then( function ( data ) {
                        if ( !data.query || !data.query.pages ) {
                                return [];
@@ -40,7 +41,13 @@
                                        id: page.pageid,
                                        title: page.title,
                                        thumbnail: page.thumbnail,
-                                       description: undefined
+                                       description: undefined,
+
+                                       // If the page is watched by the user,
+                                       // then `page.watched` will be present
+                                       // and an empty string. By default,
+                                       // it's undefined.
+                                       isWatched: page.watched !== undefined
                                };
 
                                if (
diff --git a/resources/ext.relatedArticles.readMore.minerva/index.js 
b/resources/ext.relatedArticles.readMore.minerva/index.js
index 888c5ab..646b786 100644
--- a/resources/ext.relatedArticles.readMore.minerva/index.js
+++ b/resources/ext.relatedArticles.readMore.minerva/index.js
@@ -1,6 +1,47 @@
 ( function ( $ ) {
 
-       var MOBILE_WEB_WATCHING_FUNNEL = 'read-more';
+       var MOBILE_WEB_WATCHING_FUNNEL = 'read-more',
+               WatchstarApi = mw.mobileFrontend.require( 
'mobile.watchstar/WatchstarApi' ),
+               StubWatchstarApi;
+
+       /**
+        * The `WatchstarPageList` makes a request to the API to get the
+        * watched status of all of the pages in the list. By getting this
+        * information as part of the query made by the boostrap script and
+        * injecting a stubbed `WatchstarApi` we can eliminate this request
+        * entirely.
+        *
+        * @class
+        */
+       StubWatchstarApi = WatchstarApi.extend( {
+               load: function () {
+                       return $.Deferred()
+                               .resolve()
+                               .promise();
+               }
+       } );
+
+       /**
+        * Initializes a new instance of `StubWatchstarApi`.
+        *
+        * Here initialization means setting the watched status of the set of
+        * pages manually so clients can invoke `#isWatched` despite `#load`
+        * never making a request to the API.
+        *
+        * @static
+        * @method
+        *
+        * @param {Object[]} pages
+        */
+       StubWatchstarApi.fromRawPages = function ( pages ) {
+               var watchstarApi = new StubWatchstarApi();
+
+               $.each( pages, function ( _, page ) {
+                       watchstarApi.setWatchedPage( page, page.isWatched );
+               } );
+
+               return watchstarApi;
+       };
 
        /**
         * Converts the set of pages generated in the init script into a set of
@@ -24,6 +65,7 @@
 
        mw.trackSubscribe( 'ext.relatedArticles.init', function ( _, data ) {
                var WatchstarPageList = mw.mobileFrontend.require( 
'mobile.pagelist.scripts/WatchstarPageList' ),
+                       pages = data.pages,
                        pageList,
                        $container = $( '#content' ),
                        $readMore;
@@ -36,7 +78,9 @@
                        // logs the event, has a sensible default value for
                        // MobileWebWatching.funnel, but it's overwritten
                        // by WatchstarPageList.
-                       funnel: MOBILE_WEB_WATCHING_FUNNEL
+                       funnel: MOBILE_WEB_WATCHING_FUNNEL,
+
+                       api: StubWatchstarApi.fromRawPages( pages )
                } );
 
                $readMore = $( '<aside class="ra-read-more 
post-content"></aside>' );

-- 
To view, visit https://gerrit.wikimedia.org/r/246463
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I45b01a8c1965b96903229cc9d0b7fe707acea571
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/RelatedArticles
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to