Skip to content

Check block status change when issuing warning, and vice versa #319

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 23 additions & 4 deletions modules/twinkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(function($) {

var conv = require('ext.gadget.HanAssist').conv;
var api = new mw.Api(), relevantUserName, blockedUserName;
var api = new mw.Api(), relevantUserName, blockedUserName, initialTalkPageRev;
var menuFormattedNamespaces = $.extend({}, mw.config.get('wgFormattedNamespaces'));
menuFormattedNamespaces[0] = conv({ hans: '(条目)', hant: '(條目)' });
var blockActionText = {
Expand Down Expand Up @@ -185,6 +185,17 @@ Twinkle.block.callback = function twinkleblockCallback() {
result.actiontype[0].dispatchEvent(evt);
}
});

api.get({
action: 'query',
prop: 'revisions',
titles: 'User_talk:' + Morebits.wiki.flow.relevantUserName(true),
rvprop: 'ids',
rvlimit: 1,
format: 'json'
}).then(function(data) {
initialTalkPageRev = Object.keys(data.query.pages)[0] === '-1' ? null : data.query.pages[Object.keys(data.query.pages)[0]].revisions[0].revid;
});
};

// Store fetched user data, only relevant if switching IPv6 to a /64
Expand Down Expand Up @@ -1905,7 +1916,6 @@ Twinkle.block.callback.evaluate = function twinkleblockCallbackEvaluate(e) {
// boolean-flipped options
blockoptions.anononly = blockoptions.hardblock ? undefined : true;
blockoptions.allowusertalk = blockoptions.disabletalk ? undefined : true;

/*
Check if block status changed while processing the form.

Expand Down Expand Up @@ -1938,7 +1948,11 @@ Twinkle.block.callback.evaluate = function twinkleblockCallbackEvaluate(e) {
list: 'blocks|logevents',
letype: 'block',
lelimit: 1,
letitle: 'User:' + blockoptions.user
letitle: 'User:' + blockoptions.user,
prop: 'revisions',
titles: 'User_talk:' + Morebits.wiki.flow.relevantUserName(true),
rvprop: 'ids',
rvlimit: 1
};
// bkusers doesn't catch single IPs blocked as part of a range block
if (mw.util.isIPAddress(blockoptions.user, true)) {
Expand All @@ -1952,6 +1966,12 @@ Twinkle.block.callback.evaluate = function twinkleblockCallbackEvaluate(e) {
query.type = 'userrights';
}
api.get(query).then(function(data) {
// using twinkle to reblock can cause a user talk page edit, thus check first
var currentTalkPageRev = Object.keys(data.query.pages)[0] === '-1' ? null : data.query.pages[Object.keys(data.query.pages)[0]].revisions[0].revid;
Copy link
Preview

Copilot AI May 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code assumes that the revisions property exists when the page key is not '-1'. Consider adding a safeguard to verify that the revisions array exists and is non-empty before accessing its first element to avoid potential runtime errors.

Suggested change
var currentTalkPageRev = Object.keys(data.query.pages)[0] === '-1' ? null : data.query.pages[Object.keys(data.query.pages)[0]].revisions[0].revid;
var pageKey = Object.keys(data.query.pages)[0];
var currentTalkPageRev = pageKey === '-1' || !data.query.pages[pageKey].revisions || !data.query.pages[pageKey].revisions.length
? null
: data.query.pages[pageKey].revisions[0].revid;

Copilot uses AI. Check for mistakes.

if (currentTalkPageRev !== initialTalkPageRev && !confirm(conv({ hans: '用户讨论页已被修改,其他管理员可能已处理,是否继续?', hant: '使用者討論頁已被修改,其他管理員可能已處理,是否繼續?' }))) {
Morebits.status.error(conv({ hans: '执行封禁', hant: '執行封鎖' }), conv({ hans: '用户取消操作', hant: '使用者取消操作' }));
return;
}
var block = data.query.blocks[0];
// As with the initial data fetch, if an IP is blocked
// *and* rangeblocked, this would only grab whichever
Expand Down Expand Up @@ -1994,7 +2014,6 @@ Twinkle.block.callback.evaluate = function twinkleblockCallbackEvaluate(e) {
}
blockoptions.reblock = 1; // Writing over a block will fail otherwise
}

var groupsCanBeRemoved = [
'autoreviewer',
'confirmed',
Expand Down
41 changes: 26 additions & 15 deletions modules/twinklewarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* etc.), as well as the rollback success page
*/

var conv = require('ext.gadget.HanAssist').conv;
var conv = require('ext.gadget.HanAssist').conv, initialBlockId, initialBlockInfo;

Twinkle.warn = function twinklewarn() {

Expand Down Expand Up @@ -117,7 +117,6 @@ Twinkle.warn.callback = function twinklewarnCallback() {
!confirm(conv({ hans: '您将要警告自己!您确定要继续吗?', hant: '您將要警告自己!您確定要繼續嗎?' }))) {
return;
}

var dialog;
Twinkle.warn.dialog = new Morebits.simpleWindow(600, 440);
dialog = Twinkle.warn.dialog;
Expand Down Expand Up @@ -285,6 +284,10 @@ Twinkle.warn.callback = function twinklewarnCallback() {
Twinkle.warn.isFlow = false;
init();
});
Twinkle.block.fetchUserInfo(function () {
initialBlockId = Twinkle.block.blockLogId;
initialBlockInfo = Twinkle.block.currentBlockInfo;
});
};

// This is all the messages that might be dispatched by the code
Expand Down Expand Up @@ -1911,20 +1914,28 @@ Twinkle.warn.callback.evaluate = function twinklewarnCallbackEvaluate(e) {

Morebits.simpleWindow.setButtonsEnabled(false);
Morebits.status.init(e.target);
Twinkle.block.fetchUserInfo(function () {
if (initialBlockId !== Twinkle.block.blockLogId || !!initialBlockInfo !== !!Twinkle.block.currentBlockInfo) {
Copy link
Preview

Copilot AI May 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The asynchronous fetching of block info followed by a check using '!!initialBlockInfo' may be unclear. Consider adding a comment or refactoring the boolean check to explicitly compare for null/undefined to improve readability and maintainability.

Suggested change
if (initialBlockId !== Twinkle.block.blockLogId || !!initialBlockInfo !== !!Twinkle.block.currentBlockInfo) {
if (initialBlockId !== Twinkle.block.blockLogId || (initialBlockInfo !== null && initialBlockInfo !== undefined) !== (Twinkle.block.currentBlockInfo !== null && Twinkle.block.currentBlockInfo !== undefined)) {

Copilot uses AI. Check for mistakes.

var newBlockMessage = conv({ hans: '此用户的封禁状态发生变化,确定继续发出警告?', hant: '此使用者的封鎖狀態發生變化,確定繼續發出警告?' });
if (!confirm(newBlockMessage)) {
Morebits.status.error(conv({ hans: '警告、提醒用户', hant: '警告、提醒使用者' }), conv({ hans: '用户取消操作', hant: '使用者取消操作' }));
return;
}
}
Morebits.wiki.actionCompleted.redirect = userTalkPage;
Morebits.wiki.actionCompleted.notice = conv({ hans: '警告完成,将在几秒后刷新', hant: '警告完成,將在幾秒後重新整理' });

Morebits.wiki.actionCompleted.redirect = userTalkPage;
Morebits.wiki.actionCompleted.notice = conv({ hans: '警告完成,将在几秒后刷新', hant: '警告完成,將在幾秒後重新整理' });

if (Twinkle.warn.isFlow) {
var flow_page = new Morebits.wiki.flow(userTalkPage, conv({ hans: '用户Flow讨论页留言', hant: '使用者Flow討論頁留言' }));
flow_page.setCallbackParameters(params);
Twinkle.warn.callbacks.main_flow(flow_page);
} else {
var wikipedia_page = new Morebits.wiki.page(userTalkPage, conv({ hans: '用户讨论页修改', hant: '使用者討論頁修改' }));
wikipedia_page.setCallbackParameters(params);
wikipedia_page.setFollowRedirect(true, false);
wikipedia_page.load(Twinkle.warn.callbacks.main);
}
if (Twinkle.warn.isFlow) {
var flow_page = new Morebits.wiki.flow(userTalkPage, conv({ hans: '用户Flow讨论页留言', hant: '使用者Flow討論頁留言' }));
flow_page.setCallbackParameters(params);
Twinkle.warn.callbacks.main_flow(flow_page);
} else {
var wikipedia_page = new Morebits.wiki.page(userTalkPage, conv({ hans: '用户讨论页修改', hant: '使用者討論頁修改' }));
wikipedia_page.setCallbackParameters(params);
wikipedia_page.setFollowRedirect(true, false);
wikipedia_page.load(Twinkle.warn.callbacks.main);
}
});
};

Twinkle.addInitCallback(Twinkle.warn, 'warn');
Expand Down