-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into hs/notify-on-mention
- Loading branch information
Showing
17 changed files
with
425 additions
and
10 deletions.
There are no files selected for viewing
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,114 @@ | ||
/* | ||
Copyright 2024 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 { MatrixEmitter, MatrixSendClient } from "./MatrixEmitter"; | ||
|
||
export class ModCache { | ||
private modRoomMembers: string[] = []; | ||
private ignoreList: string[] = []; | ||
private client: MatrixSendClient; | ||
private emitter: MatrixEmitter; | ||
private managementRoomId: string; | ||
private ttl: number = 1000 * 60 * 60; // 60 minutes | ||
private lastInvalidation = 0; | ||
private interval: any; | ||
|
||
constructor(client: MatrixSendClient, emitter: MatrixEmitter, managementRoomId: string) { | ||
this.client = client; | ||
this.emitter = emitter; | ||
this.managementRoomId = managementRoomId; | ||
this.lastInvalidation = Date.now(); | ||
this.init(); | ||
} | ||
|
||
/** | ||
* Initially populate cache and set bot listening for membership events in moderation room | ||
*/ | ||
async init() { | ||
await this.populateCache(); | ||
this.interval = setInterval( | ||
() => { | ||
if (Date.now() - this.lastInvalidation > this.ttl) { | ||
this.populateCache(); | ||
} | ||
}, | ||
1000 * 60, // check invalidation status every minute | ||
); | ||
this.emitter.on("room.event", async (roomId: string, event: any) => { | ||
if (roomId === this.managementRoomId && event.type === "m.room.member") { | ||
await this.populateCache(); | ||
this.lastInvalidation = Date.now(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Populate the cache by fetching moderation room membership events | ||
*/ | ||
public async populateCache() { | ||
const memberEvents = await this.client.getRoomMembers( | ||
this.managementRoomId, | ||
undefined, | ||
["join", "invite"], | ||
["ban", "leave"], | ||
); | ||
this.modRoomMembers = []; | ||
memberEvents.forEach((event) => { | ||
if (!this.modRoomMembers.includes(event.stateKey)) { | ||
this.modRoomMembers.push(event.stateKey); | ||
} | ||
const server = event.stateKey.split(":")[1]; | ||
if (!this.modRoomMembers.includes(server)) { | ||
this.modRoomMembers.push(server); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Check if a given entity is in cache | ||
*/ | ||
public checkMembership(entity: string) { | ||
return this.modRoomMembers.includes(entity) || this.ignoreList.includes(entity); | ||
} | ||
|
||
/** | ||
* Add a given entity to the list of users/servers who will not be banned but are not necessarily in moderator room | ||
*/ | ||
public addToIgnore(entity: string) { | ||
this.ignoreList.push(entity); | ||
} | ||
|
||
/** | ||
* Return a list of entities to ignore bans/ACLs for | ||
*/ | ||
public listIgnored() { | ||
return this.ignoreList; | ||
} | ||
|
||
/** | ||
* Return a list of both ignored entities and moderator room members | ||
*/ | ||
public listAll() { | ||
return this.ignoreList.concat(this.modRoomMembers); | ||
} | ||
|
||
/** | ||
* Clear the interval which refreshes cache | ||
*/ | ||
public stop() { | ||
clearInterval(this.interval); | ||
} | ||
} |
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
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
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,49 @@ | ||
/* | ||
Copyright 2024 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 { Mjolnir } from "../Mjolnir"; | ||
import { LogLevel, RichReply } from "@vector-im/matrix-bot-sdk"; | ||
|
||
// !mjolnir ignore <user|server> | ||
export async function execIgnoreCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { | ||
const target = parts[2]; | ||
|
||
await mjolnir.managementRoomOutput.logMessage( | ||
LogLevel.INFO, | ||
"IgnoreCommand", | ||
`Adding ${target} to internal moderator list.`, | ||
); | ||
mjolnir.moderators.addToIgnore(target); | ||
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅"); | ||
} | ||
|
||
// !mjolnir ignored | ||
export async function execListIgnoredCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { | ||
let html = "Ignored users:<ul>"; | ||
let text = "Ignored users:\n"; | ||
|
||
for (const name of mjolnir.moderators.listIgnored()) { | ||
html += `<li>${name}</li>`; | ||
text += `* ${name}\n`; | ||
} | ||
|
||
html += "</ul>"; | ||
|
||
const reply = RichReply.createFor(roomId, event, text, html); | ||
reply["msgtype"] = "m.notice"; | ||
await mjolnir.client.sendMessage(roomId, reply); | ||
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅"); | ||
} |
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
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
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
Oops, something went wrong.