/
TrustedReporters.ts
92 lines (77 loc) · 3.32 KB
/
TrustedReporters.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Protection } from "./IProtection";
import { MXIDListProtectionSetting, NumberProtectionSetting } from "./ProtectionSettings";
import { Mjolnir } from "../Mjolnir";
const MAX_REPORTED_EVENT_BACKLOG = 20;
/*
* Hold a list of users trusted to make reports, and enact consequences on
* events that surpass configured report count thresholds
*/
export class TrustedReporters extends Protection {
private recentReported = new Map<string /* eventId */, Set<string /* reporterId */>>();
settings = {
mxids: new MXIDListProtectionSetting(),
alertThreshold: new NumberProtectionSetting(3),
// -1 means 'disabled'
redactThreshold: new NumberProtectionSetting(-1),
banThreshold: new NumberProtectionSetting(-1)
};
constructor() {
super();
}
public get name(): string {
return 'TrustedReporters';
}
public get description(): string {
return "Count reports from trusted reporters and take a configured action";
}
public async handleReport(mjolnir: Mjolnir, roomId: string, reporterId: string, event: any, reason?: string): Promise<any> {
if (!this.settings.mxids.value.includes(reporterId)) {
// not a trusted user, we're not interested
return
}
let reporters = this.recentReported.get(event.id);
if (reporters === undefined) {
// first report we've seen recently for this event
reporters = new Set<string>();
this.recentReported.set(event.id, reporters);
if (this.recentReported.size > MAX_REPORTED_EVENT_BACKLOG) {
// queue too big. push the oldest reported event off the queue
const oldest = Array.from(this.recentReported.keys())[0];
this.recentReported.delete(oldest);
}
}
reporters.add(reporterId);
let met: string[] = [];
if (reporters.size === this.settings.alertThreshold.value) {
met.push("alert");
// do nothing. let the `sendMessage` call further down be the alert
}
if (reporters.size === this.settings.redactThreshold.value) {
met.push("redact");
await mjolnir.client.redactEvent(roomId, event.id, "abuse detected");
}
if (reporters.size === this.settings.banThreshold.value) {
met.push("ban");
await mjolnir.client.banUser(event.userId, roomId, "abuse detected");
}
if (met.length > 0) {
await mjolnir.client.sendMessage(mjolnir.config.managementRoom, {
msgtype: "m.notice",
body: `message ${event.id} reported by ${[...reporters].join(', ')}. `
+ `actions: ${met.join(', ')}`
});
}
}
}