Skip to content

Commit

Permalink
Show notification on new changes to pre-empt edit conflicts
Browse files Browse the repository at this point in the history
Polls the API every 5 seconds to check if there are new revisions or if the page has been deleted.

Solves #1418.
  • Loading branch information
siddharthvp committed May 3, 2024
1 parent 100cfbf commit 8653758
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/friendlytag.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ Twinkle.tag.callback = function friendlytagCallback() {
// Redirects and files: Add a link to each template's description page
Morebits.quickForm.getElements(result, 'tags').forEach(generateLinks);
}

Twinkle.notifyOnChanges();
};


Expand Down
1 change: 1 addition & 0 deletions modules/twinkleprod.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Twinkle.prod.callback = function twinkleprodCallback() {
evt.initEvent('change', true, true);
result.prodtype[0].dispatchEvent(evt);

Twinkle.notifyOnChanges();
};


Expand Down
2 changes: 2 additions & 0 deletions modules/twinklespeedy.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ Twinkle.speedy.initDialog = function twinklespeedyInitDialog(callbackfunc) {

// Check for prior deletions. Just once, upon init
Twinkle.speedy.callback.priorDeletionCount();

Twinkle.notifyOnChanges();
};

Twinkle.speedy.callback.modeChanged = function twinklespeedyCallbackModeChanged(form) {
Expand Down
2 changes: 2 additions & 0 deletions modules/twinklexfd.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Twinkle.xfd.callback = function twinklexfdCallback() {
var evt = document.createEvent('Event');
evt.initEvent('change', true, true);
result.venue.dispatchEvent(evt);

Twinkle.notifyOnChanges();
};

Twinkle.xfd.callback.wrongVenueWarning = function twinklexfdWrongVenueWarning(venue) {
Expand Down
52 changes: 52 additions & 0 deletions twinkle.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,58 @@ Twinkle.makeFindSourcesDiv = function makeSourcesDiv(divID) {
}
};

Twinkle.notifyOnChanges = function notifyOnChanges() {
let checkIfChanges = function (response) {
if (response.query.pages[0].missing) {
mw.notify('This page has just been deleted!', {
title: 'Twinkle',
autoHide: false,
type: 'error'
});
return true;
}
if (response.query.pages[0].revisions) {
mw.notify('Page has been edited in the meantime. Reload to see latest changes.', {
title: 'Twinkle',
autoHide: false,
type: 'warn'
});
return true;
}
return false;
};
let api = new mw.Api();
api.get({
action: 'query',
format: 'json',
prop: 'revisions',
revids: mw.config.get('wgRevisionId'),
formatversion: '2',
rvprop: 'ids|timestamp',
rvslots: 'main'
}).then(function (response) {
if (!checkIfChanges(response)) {
let timestamp = response.query.pages[0].revisions[0].timestamp;
let interval = setInterval(function () {
api.get({
action: 'query',
prop: 'revisions',
titles: Morebits.pageNameNorm,
formatversion: '2',
rvprop: 'ids',
rvslots: 'main',
rvstart: new Date(new Date(timestamp).getTime() + 1000).toISOString(),
rvdir: 'newer'
}).then(function (response) {
if (checkIfChanges(response)) {
clearInterval(interval);
}
});
}, 5000);
}
});
};

/** Twinkle-specific utility functions shared by multiple modules */
// Used in batch, unlink, and deprod to sort pages by namespace, as
// json formatversion=2 sorts by pageid instead (#1251)
Expand Down

0 comments on commit 8653758

Please sign in to comment.