/
moderation.ts
53 lines (40 loc) · 1.26 KB
/
moderation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Store from './store/index.js'
export const BLOCKED = 'blocked'
export const ALLOWED = 'allowed'
export const QUEUE = 'queue'
export type ModerationState = typeof BLOCKED | typeof ALLOWED | typeof QUEUE
export class ModerationChecker {
private readonly store: Store
constructor (store: Store) {
this.store = store
}
async check (pattern: string, actor?: string): Promise<ModerationState> {
if (typeof actor === 'string') {
const actorStore = this.store.forActor(actor)
// Check if in the actor-specific allowlist
if (await actorStore.allowlist.matches(pattern)) {
return ALLOWED
}
// Check if in the actor-specific blocklist
if (await actorStore.blocklist.matches(pattern)) {
return BLOCKED
}
}
if (await this.store.admins.matches(pattern)) {
return ALLOWED
}
// Check if in the global blocklist
if (await this.store.blocklist.matches(pattern)) {
return BLOCKED
}
// Check if in the global allowlist
if (await this.store.allowlist.matches(pattern)) {
return ALLOWED
}
return QUEUE
}
async isAllowed (pattern: string, actor?: string): Promise<boolean> {
const state = await this.check(pattern, actor)
return state !== BLOCKED
}
}