From 4b487f05e9de1290da3512c66bff0b6c64be6e83 Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 1 Nov 2022 11:34:01 +0100 Subject: [PATCH 1/3] Add more configs to disable various matrix state changes Signed-off-by: Jan Bidler --- config/config.sample.yaml | 6 +++++ config/config.schema.yaml | 6 +++++ src/config.ts | 3 +++ src/matrixeventprocessor.ts | 9 ++++--- test/test_matrixeventprocessor.ts | 44 +++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/config/config.sample.yaml b/config/config.sample.yaml index 3443e4af..46b37717 100644 --- a/config/config.sample.yaml +++ b/config/config.sample.yaml @@ -34,8 +34,14 @@ bridge: disableReadReceipts: false # Disable Join Leave echos from matrix disableJoinLeaveNotifications: false + # Disable Ban echos from matrix + disableBanNotifications: false + # Disable Kick echos from matrix + disableKickNotifications: false # Disable Invite echos from matrix disableInviteNotifications: false +# Disable Room Name change echos from matrix + disableRoomNameNotifications: false # Disable Room Topic echos from matrix disableRoomTopicNotifications: false # Auto-determine the language of code blocks (this can be CPU-intensive) diff --git a/config/config.schema.yaml b/config/config.schema.yaml index eb320ac0..e6c55b92 100644 --- a/config/config.schema.yaml +++ b/config/config.schema.yaml @@ -28,8 +28,14 @@ properties: type: "boolean" disableJoinLeaveNotifications: type: "boolean" + disableBanNotifications: + type: "boolean" + disableKickNotifications: + type: "boolean" disableInviteNotifications: type: "boolean" + disableRoomNameNotifications: + type: "boolean" disableRoomTopicNotifications: type: "boolean" userActivity: diff --git a/src/config.ts b/src/config.ts index b9362fa9..d3f74a8c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -96,7 +96,10 @@ export class DiscordBridgeConfigBridge { public disableEveryoneMention: boolean = false; public disableHereMention: boolean = false; public disableJoinLeaveNotifications: boolean = false; + public disableBanNotifications: boolean = false; + public disableKickNotifications: boolean = false; public disableInviteNotifications: boolean = false; + public disableRoomNameNotifications: boolean = false; public disableRoomTopicNotifications: boolean = false; public determineCodeLanguage: boolean = false; public activityTracker: UserActivityTrackerConfig = UserActivityTrackerConfig.DEFAULT; diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index f1f46115..b294bba5 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -236,9 +236,12 @@ export class MatrixEventProcessor { const allowJoinLeave = !this.config.bridge.disableJoinLeaveNotifications; const allowInvite = !this.config.bridge.disableInviteNotifications; + const allowRoomName = !this.config.bridge.disableRoomNameNotifications; const allowRoomTopic = !this.config.bridge.disableRoomTopicNotifications; + const allowBan = !this.config.bridge.disableBanNotifications; + const allowKick = !this.config.bridge.disableKickNotifications; - if (event.type === "m.room.name") { + if (event.type === "m.room.name" && allowRoomName) { msg += `set the name to \`${event.content!.name}\``; } else if (event.type === "m.room.topic" && allowRoomTopic) { msg += `set the topic to \`${event.content!.topic}\``; @@ -263,11 +266,11 @@ export class MatrixEventProcessor { msg += "joined the room"; } else if (membership === "invite" && allowInvite) { msg += `invited \`${event.state_key}\` to the room`; - } else if (membership === "leave" && event.state_key !== event.sender) { + } else if (membership === "leave" && event.state_key !== event.sender && allowKick) { msg += `kicked \`${event.state_key}\` from the room`; } else if (membership === "leave" && allowJoinLeave) { msg += "left the room"; - } else if (membership === "ban") { + } else if (membership === "ban" && allowBan) { msg += `banned \`${event.state_key}\` from the room`; } else { // Ignore anything else diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 6e4cedfc..e06fea32 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -343,6 +343,20 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` set the name to `Test Name` on Matrix."); }); + it("Should not echo name changes", async () => { + const config = new DiscordBridgeConfig(); + config.bridge.disableRoomNameNotifications = true; + const {processor} = createMatrixEventProcessor(); + const event = { + content: { + name: "Test Name", + }, + sender: "@user:localhost", + type: "m.room.name", + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo topic changes", async () => { const {processor} = createMatrixEventProcessor(); const event = { @@ -438,6 +452,21 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` kicked `@user2:localhost` from the room on Matrix."); }); + it("Should not echo kicks", async () => { + const config = new DiscordBridgeConfig(); + config.bridge.disableKickNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + membership: "leave", + }, + sender: "@user:localhost", + state_key: "@user2:localhost", + type: "m.room.member", + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo leaves", async () => { const {processor} = createMatrixEventProcessor(); const event = { @@ -480,6 +509,21 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` banned `@user2:localhost` from the room on Matrix."); }); + it("Should not echo bans", async () => { + const config = new DiscordBridgeConfig(); + config.bridge.disableBanNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + membership: "ban", + }, + sender: "@user:localhost", + state_key: "@user2:localhost", + type: "m.room.member", + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); }); describe("EventToEmbed", () => { it("Should contain a profile.", async () => { From 6a4335cdaa8bf147745c8a300eee2fb67586137a Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 1 Nov 2022 11:37:37 +0100 Subject: [PATCH 2/3] Add feature file --- changelog.d/867.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/867.feature diff --git a/changelog.d/867.feature b/changelog.d/867.feature new file mode 100644 index 00000000..5d6c7d8a --- /dev/null +++ b/changelog.d/867.feature @@ -0,0 +1 @@ +Implement config values, in order to disable forwarding room name changes, bans and kicks from Matrix to Discord. From f9600286392e137552ae60717487f1f440381690 Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 1 Nov 2022 11:39:45 +0100 Subject: [PATCH 3/3] Fix tests --- src/matrixeventprocessor.ts | 2 +- test/test_matrixeventprocessor.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index b294bba5..1080345b 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -268,7 +268,7 @@ export class MatrixEventProcessor { msg += `invited \`${event.state_key}\` to the room`; } else if (membership === "leave" && event.state_key !== event.sender && allowKick) { msg += `kicked \`${event.state_key}\` from the room`; - } else if (membership === "leave" && allowJoinLeave) { + } else if (membership === "leave" && event.state_key === event.sender && allowJoinLeave) { msg += "left the room"; } else if (membership === "ban" && allowBan) { msg += `banned \`${event.state_key}\` from the room`; diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index e06fea32..ff2f8e40 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -344,9 +344,9 @@ describe("MatrixEventProcessor", () => { expect(STATE_EVENT_MSG).to.equal("`@user:localhost` set the name to `Test Name` on Matrix."); }); it("Should not echo name changes", async () => { - const config = new DiscordBridgeConfig(); - config.bridge.disableRoomNameNotifications = true; - const {processor} = createMatrixEventProcessor(); + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableRoomNameNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); const event = { content: { name: "Test Name", @@ -453,8 +453,8 @@ describe("MatrixEventProcessor", () => { expect(STATE_EVENT_MSG).to.equal("`@user:localhost` kicked `@user2:localhost` from the room on Matrix."); }); it("Should not echo kicks", async () => { - const config = new DiscordBridgeConfig(); - config.bridge.disableKickNotifications = true; + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableKickNotifications = true; const {processor} = createMatrixEventProcessor(0, bridge); const event = { content: { @@ -510,8 +510,8 @@ describe("MatrixEventProcessor", () => { expect(STATE_EVENT_MSG).to.equal("`@user:localhost` banned `@user2:localhost` from the room on Matrix."); }); it("Should not echo bans", async () => { - const config = new DiscordBridgeConfig(); - config.bridge.disableBanNotifications = true; + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableBanNotifications = true; const {processor} = createMatrixEventProcessor(0, bridge); const event = { content: {