Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chunking for API requests #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 55 additions & 36 deletions xfdcloser-src/Controllers/DiscussionViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,66 @@ class DiscussionViewController {
this.quickCloseMenu.connect(this, {choose: "onQuickCloseChoose"});

if ( this.model.pages.length ) {
this.fetchInfoFromApi();
this.fetchInfoFromApi();
}
}
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;
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 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);

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() {
Expand Down Expand Up @@ -122,4 +141,4 @@ class DiscussionViewController {
}
}

export default DiscussionViewController;
export default DiscussionViewController;
Loading