Add an option to protect all joined rooms

This commit is contained in:
Travis Ralston committed 2020-01-21 15:19:03 -07:00
1 parent 97d02b3816
commit c0365416fa
6 files changed
+45 -9

No files matched your search

+29 -2
View File
@@ -44,6 +44,7 @@ export class Mjolnir {
private protections: IProtection[] = [];
private redactionQueue = new AutomaticRedactionQueue();
private automaticRedactionReasons: MatrixGlob[] = [];
private protectedJoinedRoomIds: string[] = [];
constructor(
public readonly client: MatrixClient,
@@ -75,6 +76,15 @@ export class Mjolnir {
}
});
client.on("room.join", (roomId: string, event: any) => {
LogService.info("Mjolnir", `Joined ${roomId}`);
return this.resyncJoinedRooms();
});
client.on("room.leave", (roomId: string, event: any) => {
LogService.info("Mjolnir", `Left ${roomId}`);
return this.resyncJoinedRooms();
});
client.getUserId().then(userId => {
this.localpart = userId.split(':')[0].substring(1);
return client.getUserProfile(userId);
@@ -115,6 +125,7 @@ export class Mjolnir {
}).then(async () => {
this.currentState = STATE_SYNCING;
await logMessage(LogLevel.DEBUG, "Mjolnir@startup", "Loading protected rooms...");
await this.resyncJoinedRooms(false);
try {
const data = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
if (data && data['rooms']) {
@@ -138,8 +149,7 @@ export class Mjolnir {
}
public async addProtectedRoom(roomId: string) {
const permalink = Permalinks.forRoom(roomId);
this.protectedRooms[roomId] = permalink;
this.protectedRooms[roomId] = Permalinks.forRoom(roomId);
let additionalProtectedRooms;
try {
@@ -167,6 +177,23 @@ export class Mjolnir {
await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms);
}
private async resyncJoinedRooms(withSync = true) {
if (!config.protectAllJoinedRooms) return;
const joinedRoomIds = (await this.client.getJoinedRooms()).filter(r => r !== config.managementRoom);
for (const roomId of this.protectedJoinedRoomIds) {
delete this.protectedRooms[roomId];
}
this.protectedJoinedRoomIds = joinedRoomIds;
for (const roomId of joinedRoomIds) {
this.protectedRooms[roomId] = Permalinks.forRoom(roomId);
}
if (withSync) {
await this.syncLists(config.verboseLogging);
}
}
private async getEnabledProtections() {
let enabled: string[] = [];
try {
+2 -2
View File
@@ -19,8 +19,8 @@ import { RichReply } from "matrix-bot-sdk";
// !mjolnir rooms
export async function execListProtectedRooms(roomId: string, event: any, mjolnir: Mjolnir) {
let html = "<b>Protected rooms:</b><br/><ul>";
let text = "Protected rooms:\n";
let html = `<b>Protected rooms (${Object.keys(mjolnir.protectedRooms).length}):</b><br/><ul>`;
let text = `Protected rooms (${Object.keys(mjolnir.protectedRooms).length}):\n`;
let hasRooms = false;
for (const roomId in mjolnir.protectedRooms) {
+1
View File
@@ -37,6 +37,7 @@ interface IConfig {
protectedRooms: string[]; // matrix.to urls
fasterMembershipChecks: boolean;
automaticallyRedactForReasons: string[]; // case-insensitive globs
protectAllJoinedRooms: boolean;
/**
* Config options only set at runtime. Try to avoid using the objects