Skip to content

Commit ee6364e

Browse files
authored
refactor(MarkBlocked): ; optimize logic (qiuwenbaike#1652)
* refactor(MarkBlocked): ; optimize logic * avoid querying global user info of IP users * querying range blocking affecting certain IP address
1 parent 760dca3 commit ee6364e

File tree

4 files changed

+115
-11
lines changed

4 files changed

+115
-11
lines changed

dist/MarkBlocked/MarkBlocked.js

Lines changed: 63 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MarkBlocked/modules/markBlockedUser.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {QueryLocalAndGlobalBlocksAndLocksResponse} from './types';
22
import {generateUserLinks} from './util/generateUserLinks';
33
import {markLinks} from './util/markLinks';
44
import {queryGlobalUserInfo} from './util/queryGlobalUserInfo';
5+
import {queryIPBlocks} from './util/queryIPBlocks';
56
import {queryUserBlocks} from './util/queryUserBlocks';
67

78
const markBlockedUser = ($content: JQuery): void => {
@@ -21,6 +22,10 @@ const markBlockedUser = ($content: JQuery): void => {
2122
// since they use Array#splice to create bulk queries,
2223
// and items will be removed from the array "users".
2324
for (const guiuser of users) {
25+
if (mw.util.isIPv4Address(guiuser) || mw.util.isIPv6Address(guiuser)) {
26+
continue;
27+
}
28+
2429
promises[promises.length] = async (): Promise<void> => {
2530
try {
2631
const response = (await queryGlobalUserInfo(guiuser)) as QueryLocalAndGlobalBlocksAndLocksResponse;
@@ -46,6 +51,25 @@ const markBlockedUser = ($content: JQuery): void => {
4651
console.error('[MarkBlocked] Ajax error:', error);
4752
}
4853
};
54+
55+
for (let bkip of bkusers) {
56+
if (!mw.util.isIPv4Address(bkip) && !mw.util.isIPv6Address(bkip)) {
57+
continue;
58+
}
59+
60+
if (mw.util.isIPv6Address(bkip)) {
61+
bkip = bkip.toUpperCase();
62+
}
63+
64+
promises[promises.length] = async (): Promise<void> => {
65+
try {
66+
const response = (await queryIPBlocks(bkip)) as QueryLocalAndGlobalBlocksAndLocksResponse;
67+
markLinks({response, userLinks, bkip});
68+
} catch (error: unknown) {
69+
console.error('[MarkBlocked] Ajax error:', error);
70+
}
71+
};
72+
}
4973
}
5074

5175
void (async () => {

src/MarkBlocked/modules/util/markLinks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {getMessage} from '../i18n';
77
const markLinks = ({
88
response,
99
userLinks,
10+
bkip,
1011
}: {
1112
response: QueryLocalAndGlobalBlocksAndLocksResponse;
1213
userLinks: Record<string, JQuery[]>;
14+
bkip?: string;
1315
}): void => {
1416
// Local blocks
1517
if (response['query']?.blocks) {
@@ -37,7 +39,7 @@ const markLinks = ({
3739
.replace('$4', inHours(Date.now() - parseTS(block.timestamp)));
3840
tip = isPartialBlcok ? tip.replace('$5', getMessage('partial')) : tip.replace('$5', '');
3941

40-
const $links: JQuery[] | undefined = userLinks[block.user];
42+
const $links: JQuery[] | undefined = userLinks[bkip ?? block.user];
4143
if (!$links) {
4244
continue;
4345
}
@@ -71,7 +73,7 @@ const markLinks = ({
7173
.replace('$4', inHours(Date.now() - parseTS(block.timestamp)));
7274
tip = tip.replace('$5', '');
7375

74-
const $links: JQuery[] | undefined = userLinks[block.target];
76+
const $links: JQuery[] | undefined = userLinks[bkip ?? block.target];
7577
if (!$links) {
7678
continue;
7779
}
@@ -96,7 +98,7 @@ const markLinks = ({
9698

9799
const tip: string = getMessage('Locked');
98100

99-
const $links: JQuery[] | undefined = userLinks[user];
101+
const $links: JQuery[] | undefined = userLinks[bkip ?? user];
100102
if (!$links) {
101103
return;
102104
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {ApiQueryGlobalBlocksParamsRedefined} from '../types';
2+
import {api} from './api';
3+
4+
const queryIPBlocks = async (bkip: string) => {
5+
const params: ApiQueryBlocksParams & ApiQueryGlobalBlocksParamsRedefined = {
6+
action: 'query',
7+
format: 'json',
8+
formatversion: '2',
9+
list: ['blocks', 'globalblocks'],
10+
bkip,
11+
bklimit: 100,
12+
bkprop: ['by', 'expiry', 'reason', 'restrictions', 'timestamp', 'user'],
13+
bgip: bkip,
14+
bglimit: 100,
15+
bgprop: ['by', 'expiry', 'reason', 'timestamp', 'target'],
16+
smaxage: 600,
17+
maxage: 600,
18+
};
19+
20+
return await api.get(params);
21+
};
22+
23+
export {queryIPBlocks};

0 commit comments

Comments
 (0)