From 5cd89d5c4e6f5695d852da8b014b818384333fdd Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 18 Oct 2019 16:38:19 +0100 Subject: [PATCH] Fix handling of state events --- src/Mjolnir.ts | 65 +++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index affb07c..83df629 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -295,54 +295,43 @@ export class Mjolnir { } private async handleEvent(roomId: string, event: any) { - if (!Object.keys(this.protectedRooms).includes(roomId)) return; - if (event['sender'] === await this.client.getUserId()) return; // Ignore ourselves - - if (event['type'] === 'm.room.power_levels' && event['state_key'] === '') { - // power levels were updated - recheck permissions - const url = this.protectedRooms[roomId]; - let html = `Power levels changed in ${roomId} - checking permissions...`; - let text = `Power levels changed in ${url} - checking permissions...`; - await this.client.sendMessage(this.managementRoomId, { - msgtype: "m.notice", - body: text, - format: "org.matrix.custom.html", - formatted_body: html, - }); - const errors = await this.verifyPermissionsIn(roomId); - const hadErrors = await this.printActionResult(errors); - if (!hadErrors) { - html = `All permissions look OK.`; - text = "All permissions look OK."; + if (Object.keys(this.protectedRooms).includes(roomId)) { + if (event['sender'] === await this.client.getUserId()) return; // Ignore ourselves + if (event['type'] === 'm.room.power_levels' && event['state_key'] === '') { + // power levels were updated - recheck permissions + const url = this.protectedRooms[roomId]; + let html = `Power levels changed in ${roomId} - checking permissions...`; + let text = `Power levels changed in ${url} - checking permissions...`; await this.client.sendMessage(this.managementRoomId, { msgtype: "m.notice", body: text, format: "org.matrix.custom.html", formatted_body: html, }); + const errors = await this.verifyPermissionsIn(roomId); + const hadErrors = await this.printActionResult(errors); + if (!hadErrors) { + html = `All permissions look OK.`; + text = "All permissions look OK."; + await this.client.sendMessage(this.managementRoomId, { + msgtype: "m.notice", + body: text, + format: "org.matrix.custom.html", + formatted_body: html, + }); + } + return; + } else if (event['type'] === "m.room.member") { + const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this); + await this.printActionResult(errors); } - return; } - if (!event['state_key']) return; // from here we don't do anything with events missing/empty state key - - if (ALL_RULE_TYPES.includes(event['type'])) { - await this.syncListForRoom(roomId); - } else if (event['type'] === "m.room.member") { - const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this); - const hadErrors = await this.printActionResult(errors); - - if (!hadErrors) { - const html = `Done updating rooms - no errors`; - const text = "Done updating rooms - no errors"; - await this.client.sendMessage(this.managementRoomId, { - msgtype: "m.notice", - body: text, - format: "org.matrix.custom.html", - formatted_body: html, - }); + if (this.banLists.map(b => b.roomId).includes(roomId)) { + if (ALL_RULE_TYPES.includes(event['type'])) { + await this.syncListForRoom(roomId); } - } else return; // Not processed + } } private async printActionResult(errors: RoomUpdateError[], title: string = null) {