Skip to content

Commit f726de2

Browse files
authored
fix(WhoIsActive): add logEvent query (qiuwenbaike#1676)
1 parent 547a775 commit f726de2

File tree

6 files changed

+164
-56
lines changed

6 files changed

+164
-56
lines changed

dist/WhoIsActive/WhoIsActive.js

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

src/WhoIsActive/modules/types.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ type Usercontribs = {
44
}>;
55
};
66

7-
export type {Usercontribs};
7+
type Logevents = {
8+
logevents: Array<{
9+
timestamp: string;
10+
}>;
11+
};
12+
13+
export type {Usercontribs, Logevents};

src/WhoIsActive/modules/util/appendLastActiveMarker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {SYSTEM_SCRIPT_LIST} from '../constant';
22
import {getLastActiveMarker} from './getLastActiveMarker';
3-
import {getUserContribsTimestamp} from './getUserContribsTimestamp';
3+
import {getTimestamp} from './getTimestamp';
44
import {uniqueArray} from 'ext.gadget.Util';
55

66
const appendLastActiveMarker = async ({usernames, $elements}: {usernames: string[]; $elements: JQuery[]}) => {
@@ -10,7 +10,7 @@ const appendLastActiveMarker = async ({usernames, $elements}: {usernames: string
1010
continue;
1111
}
1212

13-
const timestamp = await getUserContribsTimestamp(username);
13+
const timestamp = (await getTimestamp(username)) ?? '0';
1414

1515
if (!timestamp) {
1616
continue;

src/WhoIsActive/modules/util/appendLastActiveMarkerToUserPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as OPTIONS from '../../options.json';
22
import {SYSTEM_SCRIPT_LIST} from '../constant';
33
import {getLastActiveMarker} from './getLastActiveMarker';
4-
import {getUserContribsTimestamp} from './getUserContribsTimestamp';
4+
import {getTimestamp} from './getTimestamp';
55

66
const appendLastActiveMarkerToUserPage = async (username: string) => {
77
if (SYSTEM_SCRIPT_LIST.includes(username)) {
88
return;
99
}
1010

11-
const timestamp = await getUserContribsTimestamp(username);
11+
const timestamp = (await getTimestamp(username)) ?? '0';
1212

1313
if (!timestamp) {
1414
return;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as OPTIONS from '../../options.json';
2+
import {Logevents, Usercontribs} from '../types';
3+
import {api} from './api';
4+
5+
const getUserContribsTimestamp = async (ucuser: string) => {
6+
let timestamp: string;
7+
8+
try {
9+
const params: ApiQueryUserContribsParams = {
10+
action: 'query',
11+
format: 'json',
12+
list: 'usercontribs',
13+
uclimit: 1,
14+
smaxage: 600,
15+
maxage: 600,
16+
ucuser,
17+
};
18+
19+
const result = await api.get(params);
20+
21+
const {usercontribs} = result['query'] as Usercontribs;
22+
if (!usercontribs.length) {
23+
return;
24+
}
25+
26+
({timestamp} = usercontribs[0]!);
27+
} catch (error: unknown) {
28+
timestamp = '0';
29+
console.error('[WhoIsActive] Ajax error:', error);
30+
}
31+
32+
return timestamp;
33+
};
34+
35+
const getLogEventsTimestamp = async (leuser: string) => {
36+
let timestamp: string;
37+
38+
try {
39+
const params: ApiQueryLogEventsParams = {
40+
leuser,
41+
action: 'query',
42+
format: 'json',
43+
formatversion: '2',
44+
list: 'logevents',
45+
lelimit: 1,
46+
leprop: 'timestamp',
47+
smaxage: 600,
48+
maxage: 600,
49+
};
50+
51+
const result = await api.get(params);
52+
53+
const {logevents} = result['query'] as Logevents;
54+
if (!logevents.length) {
55+
return;
56+
}
57+
58+
({timestamp} = logevents[0]!);
59+
} catch (error: unknown) {
60+
timestamp = '0';
61+
console.error('[WhoIsActive] Ajax error:', error);
62+
}
63+
64+
return timestamp;
65+
};
66+
67+
const getTimestamp = async (username: string) => {
68+
let timestamp: string;
69+
70+
if (mw.storage.get(OPTIONS.storageKey + username)) {
71+
timestamp = mw.storage.get(OPTIONS.storageKey + username) as string;
72+
} else {
73+
const ucTimestamp = (await getUserContribsTimestamp(username)) ?? '0';
74+
const leTimestamp = (await getLogEventsTimestamp(username)) ?? '0';
75+
timestamp = Number.parseInt(ucTimestamp, 10) > Number.parseInt(leTimestamp, 10) ? ucTimestamp : leTimestamp;
76+
77+
// Cache for 10 minutes
78+
mw.storage.set(OPTIONS.storageKey + username, timestamp, 10 * 60);
79+
}
80+
81+
return timestamp;
82+
};
83+
84+
export {getTimestamp};

src/WhoIsActive/modules/util/getUserContribsTimestamp.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)