From 17cecbea490791e9164b172b1ce201ef9c313fc0 Mon Sep 17 00:00:00 2001 From: Qwerfjkl <125462265+Qwerfjkl@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:52:18 +0100 Subject: [PATCH 1/3] Add chunking for API requests --- .../Controllers/DiscussionViewController.js | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/xfdcloser-src/Controllers/DiscussionViewController.js b/xfdcloser-src/Controllers/DiscussionViewController.js index a9868fe..a8ba1cb 100644 --- a/xfdcloser-src/Controllers/DiscussionViewController.js +++ b/xfdcloser-src/Controllers/DiscussionViewController.js @@ -27,21 +27,38 @@ class DiscussionViewController { } } fetchInfoFromApi() { - const pagesExistencesPromise = API.get({ - action: "query", - format: "json", - formatversion: 2, - titles: this.model.pagesNames, - prop: "info", - inprop: "talkid" - }).then(response => response.query.pages.forEach(page => { - const pageTitle = mw.Title.newFromText(page.title); - const talkpageTitle = pageTitle.getTalkPage(); - mw.Title.exist.set(pageTitle.getPrefixedDb(), !page.missing); - if ( talkpageTitle ) { - mw.Title.exist.set(talkpageTitle.getPrefixedDb(), !!page.talkid); - } - })); + const pageNames = this.model.pagesNames; + const chunkSize = pageNames.length <= 100 ? 50 : (await mw.user.getRights()).indexOf( 'apihighlimits' ) >= 0 ? 500 : 50; + + const chunks = []; + for (let i = 0; i < pageNames.length; i += chunkSize) { + chunks.push(pageNames.slice(i, i + chunkSize)); + } + + const fetchChunk = (chunk) => { + return API.get({ + action: "query", + format: "json", + formatversion: 2, + titles: chunk.join('|'), + prop: "info", + inprop: "talkid" + }).then(response => { + if (response.query && response.query.pages) { + response.query.pages.forEach(page => { + const pageTitle = mw.Title.newFromText(page.title); + const talkpageTitle = pageTitle.getTalkPage(); + mw.Title.exist.set(pageTitle.getPrefixedDb(), !page.missing); + if (talkpageTitle) { + mw.Title.exist.set(talkpageTitle.getPrefixedDb(), !!page.talkid); + } + }); + } + }) + }; + + const fetchPromises = chunks.map(chunk => fetchChunk(chunk)); + const pagesExistencesPromise = Promise.all(fetchPromises); const nominationDatePromise = ( this.model.venue.type !== "afd" && this.model.venue.type !== "mfd" ) ? $.Deferred().resolve( dateFromSubpageName(this.model.discussionSubpageName) ) : API.get({ From 236206f19c3d33edf48fdc86614ff9e617ca4f5c Mon Sep 17 00:00:00 2001 From: Qwerfjkl <125462265+Qwerfjkl@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:41:14 +0100 Subject: [PATCH 2/3] Update DiscussionViewController.js Remove dependancy on await --- .../Controllers/DiscussionViewController.js | 106 +++++++++--------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/xfdcloser-src/Controllers/DiscussionViewController.js b/xfdcloser-src/Controllers/DiscussionViewController.js index a8ba1cb..d136c7b 100644 --- a/xfdcloser-src/Controllers/DiscussionViewController.js +++ b/xfdcloser-src/Controllers/DiscussionViewController.js @@ -28,59 +28,61 @@ class DiscussionViewController { } fetchInfoFromApi() { const pageNames = this.model.pagesNames; - const chunkSize = pageNames.length <= 100 ? 50 : (await mw.user.getRights()).indexOf( 'apihighlimits' ) >= 0 ? 500 : 50; - - const chunks = []; - for (let i = 0; i < pageNames.length; i += chunkSize) { - chunks.push(pageNames.slice(i, i + chunkSize)); - } - - const fetchChunk = (chunk) => { - return API.get({ - action: "query", - format: "json", - formatversion: 2, - titles: chunk.join('|'), - prop: "info", - inprop: "talkid" - }).then(response => { - if (response.query && response.query.pages) { - response.query.pages.forEach(page => { - const pageTitle = mw.Title.newFromText(page.title); - const talkpageTitle = pageTitle.getTalkPage(); - mw.Title.exist.set(pageTitle.getPrefixedDb(), !page.missing); - if (talkpageTitle) { - mw.Title.exist.set(talkpageTitle.getPrefixedDb(), !!page.talkid); - } - }); - } - }) - }; - - const fetchPromises = chunks.map(chunk => fetchChunk(chunk)); - const pagesExistencesPromise = Promise.all(fetchPromises); - const nominationDatePromise = ( this.model.venue.type !== "afd" && this.model.venue.type !== "mfd" ) - ? $.Deferred().resolve( dateFromSubpageName(this.model.discussionSubpageName) ) - : API.get({ - action: "query", - format: "json", - formatversion: 2, - titles: this.model.discussionPageName, - prop: "revisions", - rvprop: "timestamp", - rvdir: "newer", - rvlimit: "1" - }).then(response => { - const page = response.query.pages[0]; - const timestamp = page.revisions[0].timestamp; - return new Date(timestamp); + mw.user.getRights().then(rights => { + const chunkSize = rights.indexOf( 'apihighlimits' ) >= 0 ? 500 : 50; + + const chunks = []; + for (let i = 0; i < pageNames.length; i += chunkSize) { + chunks.push(pageNames.slice(i, i + chunkSize)); + } + + const fetchChunk = (chunk) => { + return API.get({ + action: "query", + format: "json", + formatversion: 2, + titles: chunk.join('|'), + prop: "info", + inprop: "talkid" + }).then(response => { + if (response.query && response.query.pages) { + response.query.pages.forEach(page => { + const pageTitle = mw.Title.newFromText(page.title); + const talkpageTitle = pageTitle.getTalkPage(); + mw.Title.exist.set(pageTitle.getPrefixedDb(), !page.missing); + if (talkpageTitle) { + mw.Title.exist.set(talkpageTitle.getPrefixedDb(), !!page.talkid); + } + }); + } + }) + }; + + const fetchPromises = chunks.map(chunk => fetchChunk(chunk)); + const pagesExistencesPromise = Promise.all(fetchPromises); + const nominationDatePromise = ( this.model.venue.type !== "afd" && this.model.venue.type !== "mfd" ) + ? $.Deferred().resolve( dateFromSubpageName(this.model.discussionSubpageName) ) + : API.get({ + action: "query", + format: "json", + formatversion: 2, + titles: this.model.discussionPageName, + prop: "revisions", + rvprop: "timestamp", + rvdir: "newer", + rvlimit: "1" + }).then(response => { + const page = response.query.pages[0]; + const timestamp = page.revisions[0].timestamp; + return new Date(timestamp); + }); + nominationDatePromise.then(nominationDate => { + this.model.setNominationDate(nominationDate); }); - nominationDatePromise.then(nominationDate => { - this.model.setNominationDate(nominationDate); + $.when(pagesExistencesPromise, nominationDatePromise) + .then(() => { this.model.setStatusReady(); }) + .catch((code, error) => { this.model.setStatusError(code, error); }); }); - $.when(pagesExistencesPromise, nominationDatePromise) - .then(() => { this.model.setStatusReady(); }) - .catch((code, error) => { this.model.setStatusError(code, error); }); } updateFromModel() { @@ -139,4 +141,4 @@ class DiscussionViewController { } } -export default DiscussionViewController; \ No newline at end of file +export default DiscussionViewController; From 2c9c70130a0c65f62e20ebcdb7571f0695e6311a Mon Sep 17 00:00:00 2001 From: Qwerfjkl <125462265+Qwerfjkl@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:17:34 +0100 Subject: [PATCH 3/3] Update DiscussionViewController.js Minor linting fixes --- xfdcloser-src/Controllers/DiscussionViewController.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xfdcloser-src/Controllers/DiscussionViewController.js b/xfdcloser-src/Controllers/DiscussionViewController.js index d136c7b..3b3d978 100644 --- a/xfdcloser-src/Controllers/DiscussionViewController.js +++ b/xfdcloser-src/Controllers/DiscussionViewController.js @@ -23,13 +23,13 @@ class DiscussionViewController { this.quickCloseMenu.connect(this, {choose: "onQuickCloseChoose"}); if ( this.model.pages.length ) { - this.fetchInfoFromApi(); + this.fetchInfoFromApi(); } } fetchInfoFromApi() { const pageNames = this.model.pagesNames; mw.user.getRights().then(rights => { - const chunkSize = rights.indexOf( 'apihighlimits' ) >= 0 ? 500 : 50; + const chunkSize = rights.indexOf( "apihighlimits" ) >= 0 ? 500 : 50; const chunks = []; for (let i = 0; i < pageNames.length; i += chunkSize) { @@ -41,7 +41,7 @@ class DiscussionViewController { action: "query", format: "json", formatversion: 2, - titles: chunk.join('|'), + titles: chunk.join("|"), prop: "info", inprop: "talkid" }).then(response => { @@ -55,7 +55,7 @@ class DiscussionViewController { } }); } - }) + }); }; const fetchPromises = chunks.map(chunk => fetchChunk(chunk));