Skip to content

Commit

Permalink
arv: refactor SPI report generation (#2070)
Browse files Browse the repository at this point in the history
* arv: refactor SPI report generation

* fix linter error

---------

Co-authored-by: NovemLinguae <[email protected]>
Co-authored-by: NovemLinguae <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2024
1 parent 303e29e commit e38ebe3
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions modules/twinklearv.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Twinkle.arv.callback.changeCategory = function (e) {
work_area.append(
{
type: 'dyninput',
name: 'sockpuppet',
name: 'sockpuppets',
label: 'Sockpuppets',
sublabel: 'Sock:',
tooltip: 'The username of the sockpuppet without the "User:" prefix',
Expand Down Expand Up @@ -574,26 +574,32 @@ Twinkle.arv.callback.evaluate = function(e) {
case 'sock':
/* falls through */
case 'puppet':
var sockParameters = {
evidence: form.evidence.value.trim(),
checkuser: form.checkuser.checked
};
var reportData = Twinkle.arv.callback.getSpiReportData(input);

var puppetReport = form.category.value === 'puppet';
if (puppetReport && !form.sockmaster.value.trim()) {
alert('You have not entered a sockmaster account for this puppet. Consider reporting this account as a sockpuppeteer instead.');
return;
} else if (!puppetReport && !form.sockpuppet[0].value.trim()) {
alert('You have not entered any sockpuppet account(s) for this sockmaster. Consider reporting this account as a sockpuppet instead.');
if (reportData.error) {
alert(reportData.error);
return;
}

sockParameters.uid = puppetReport ? form.sockmaster.value.trim() : uid;
sockParameters.sockpuppets = puppetReport ? [uid] : Morebits.array.uniq($.map($('input:text[name=sockpuppet]', form), (o) => $(o).val() || null));

Morebits.SimpleWindow.setButtonsEnabled(false);
Morebits.Status.init(form);
Twinkle.arv.processSock(sockParameters);

Morebits.wiki.addCheckpoint(); // prevent notification events from causing an erronous "action completed"

var reportpage = 'Wikipedia:Sockpuppet investigations/' + reportData.sockmaster;

Morebits.wiki.actionCompleted.redirect = reportpage;
Morebits.wiki.actionCompleted.notice = 'Reporting complete';

var spiPage = new Morebits.wiki.page(reportpage, 'Retrieving discussion page');
spiPage.setFollowRedirect(true);
spiPage.setEditSummary('Adding new report for [[Special:Contributions/' + reportData.sockmaster + '|' + reportData.sockmaster + ']].');
spiPage.setChangeTags(Twinkle.changeTags);
spiPage.setAppendText(reportData.wikitext);
spiPage.setWatchlist(Twinkle.getPref('spiWatchReport'));
spiPage.append();

Morebits.wiki.removeCheckpoint(); // all page updates have been started
break;

case 'an3':
Expand Down Expand Up @@ -818,32 +824,34 @@ Twinkle.arv.callback.getUsernameReportWikitext = function(input) {
return text;
};

Twinkle.arv.processSock = function(params) {
Morebits.wiki.addCheckpoint(); // prevent notification events from causing an erronous "action completed"
Twinkle.arv.callback.getSpiReportData = function(input) {
const isPuppetReport = input.category === 'puppet';

// prepare the SPI report
let text = '\n{{subst:SPI report|' +
params.sockpuppets.map((sock, index) => (index + 1) + '=' + sock).join('|') + '\n|evidence=' + params.evidence + ' \n';
if (!isPuppetReport) {
input.sockpuppets = input.sockpuppets.filter((sock) => sock !== ''); // ignore empty sockpuppet inputs
}

if (params.checkuser) {
text += '|checkuser=yes';
if (isPuppetReport && !input.sockmaster) {
return { error: 'You have not entered a sockmaster account for this puppet. Consider reporting this account as a sockpuppeteer instead.' };
} else if (!isPuppetReport && input.sockpuppets.length === 0) {
return { error: 'You have not entered any sockpuppet account(s) for this sockmaster. Consider reporting this account as a sockpuppet instead.' };
}
text += '}}';

const reportpage = 'Wikipedia:Sockpuppet investigations/' + params.uid;
input.sockmaster = input.sockmaster || input.uid;
input.sockpuppets = isPuppetReport ? [input.uid] : Morebits.array.uniq(input.sockpuppets);

Morebits.wiki.actionCompleted.redirect = reportpage;
Morebits.wiki.actionCompleted.notice = 'Reporting complete';
let text = '\n{{subst:SPI report|' +
input.sockpuppets.map((sock, index) => (index + 1) + '=' + sock).join('|') + '\n|evidence=' + input.evidence + ' \n';

const spiPage = new Morebits.wiki.Page(reportpage, 'Retrieving discussion page');
spiPage.setFollowRedirect(true);
spiPage.setEditSummary('Adding new report for [[Special:Contributions/' + params.uid + '|' + params.uid + ']].');
spiPage.setChangeTags(Twinkle.changeTags);
spiPage.setAppendText(text);
spiPage.setWatchlist(Twinkle.getPref('spiWatchReport'));
spiPage.append();
if (input.checkuser) {
text += '|checkuser=yes';
}
text += '}}';

Morebits.wiki.removeCheckpoint(); // all page updates have been started
return {
sockmaster: input.sockmaster,
wikitext: text
};
};

Twinkle.arv.processAN3 = function(params) {
Expand Down

0 comments on commit e38ebe3

Please sign in to comment.