-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(OnlineAdmins): add query cache (#1689)
* refactor(OnlineAdmins): add query cache
- Loading branch information
1 parent
f4f1233
commit e3c2caf
Showing
7 changed files
with
296 additions
and
228 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/OnlineAdmins/modules/api.ts → src/OnlineAdmins/modules/util/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as OPTIONS from '../../options.json'; | ||
import {queryLogEvents, queryRecentChanges, queryUserProps} from './query'; | ||
import {BLACK_LIST} from '../constant'; | ||
import {uniqueArray} from 'ext.gadget.Util'; | ||
|
||
const getAdmins = async () => { | ||
let stewards: string[] = []; | ||
let sysops: string[] = []; | ||
let patrollers: string[] = []; | ||
|
||
if (mw.storage.getObject(OPTIONS.storageKey)) { | ||
({stewards, sysops, patrollers} = mw.storage.getObject(OPTIONS.storageKey) as { | ||
stewards: string[]; | ||
sysops: string[]; | ||
patrollers: string[]; | ||
}); | ||
} else { | ||
let users: string[] = []; | ||
const promises: (() => Promise<void>)[] = []; | ||
|
||
const time: Date = new Date(); | ||
const rcstart: string = time.toISOString(); | ||
|
||
// Query users has log/edit record in recent 30 minutes | ||
time.setMinutes(time.getMinutes() - 30); // 最近更改30分钟内的编辑用户 | ||
const rcend: string = time.toISOString(); | ||
|
||
try { | ||
const recentchanges = await queryRecentChanges(rcstart, rcend); | ||
|
||
for (const {user} of recentchanges['query'].recentchanges as {user: string}[]) { | ||
users[users.length] = user; // Replace Array#push to avoid core-js polyfilling | ||
} | ||
} catch {} | ||
|
||
try { | ||
const logevents = await queryLogEvents(rcstart, rcend); | ||
|
||
for (const {user} of logevents['query'].logevents as {user: string}[]) { | ||
users[users.length] = user; | ||
} | ||
} catch {} | ||
|
||
users = uniqueArray(users); // Replace Set with uniqueArray, avoiding core-js polyfilling | ||
|
||
// Generating query promises | ||
for (let i = 0; i < users.length; i++) { | ||
const ususers = users.splice(0, 25); | ||
if (!ususers.length) { | ||
continue; | ||
} | ||
|
||
promises[promises.length] = async (): Promise<void> => { | ||
const response = await queryUserProps(ususers); | ||
|
||
for (const {groups, name} of response['query'].users as {groups: string[]; name: string}[]) { | ||
// remove bots | ||
if (groups.includes('bot') || BLACK_LIST.includes(name)) { | ||
continue; | ||
} | ||
|
||
// remove logs with no user names | ||
if (!name) { | ||
continue; | ||
} | ||
|
||
if (groups.includes('steward')) { | ||
stewards[stewards.length] = name; | ||
} | ||
|
||
if (groups.includes('sysop')) { | ||
sysops[sysops.length] = name; | ||
} | ||
|
||
if (groups.includes('patroller')) { | ||
patrollers[patrollers.length] = name; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
for (const promise of promises) { | ||
try { | ||
await promise(); | ||
} catch {} | ||
} | ||
} | ||
|
||
return {stewards, sysops, patrollers}; | ||
}; | ||
|
||
export {getAdmins}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {api} from './api'; | ||
|
||
const queryRecentChanges = async (rcstart: string, rcend: string) => { | ||
const params: ApiQueryRecentChangesParams = { | ||
rcstart, | ||
rcend, | ||
action: 'query', | ||
format: 'json', | ||
formatversion: '2', | ||
list: 'recentchanges', | ||
rcprop: 'user', | ||
rcshow: ['!bot', '!anon'], | ||
rclimit: 500, | ||
smaxage: 600, | ||
maxage: 600, | ||
}; | ||
const response = await api.get(params); | ||
|
||
return response; | ||
}; | ||
|
||
const queryLogEvents = async (lestart: string, leend: string) => { | ||
const params: ApiQueryLogEventsParams = { | ||
lestart, | ||
leend, | ||
action: 'query', | ||
format: 'json', | ||
formatversion: '2', | ||
list: 'logevents', | ||
leprop: 'user', | ||
lelimit: 500, | ||
smaxage: 600, | ||
maxage: 600, | ||
}; | ||
const response = await api.get(params); | ||
|
||
return response; | ||
}; | ||
|
||
const queryUserProps = async (ususers: string | string[]) => { | ||
const params: ApiQueryUsersParams = { | ||
ususers, | ||
action: 'query', | ||
format: 'json', | ||
formatversion: '2', | ||
list: 'users', | ||
usprop: 'groups', | ||
smaxage: 600, | ||
maxage: 600, | ||
}; | ||
const response = await api.get(params); | ||
|
||
return response; | ||
}; | ||
|
||
export {queryRecentChanges, queryLogEvents, queryUserProps}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"version": "2.0" | ||
"version": "2.0", | ||
"storageKey": "ext.gadget.OnlineAdmins_getAdmins" | ||
} |