From 942f912132c803765f0bd91557e3d8a010b1984b Mon Sep 17 00:00:00 2001 From: Miepee <38186597+Miepee@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:28:19 +0200 Subject: [PATCH] Add option to disable room topic echos from Matrix (#836) * Add option to disable room topic echos from Matrix Signed-off-by: Jan Bidler <janbidler00@protonmail.com> * Add tests for "no echos from matrix on discord" Signed-off-by: Jan Bidler <janbidler00@protonmail.com> --- changelog.d/836.feature | 1 + config/config.sample.yaml | 2 + config/config.schema.yaml | 2 + src/config.ts | 3 +- src/matrixeventprocessor.ts | 6 ++- test/test_matrixeventprocessor.ts | 65 ++++++++++++++++++++++++++++++- 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 changelog.d/836.feature diff --git a/changelog.d/836.feature b/changelog.d/836.feature new file mode 100644 index 0000000..fa8cf9c --- /dev/null +++ b/changelog.d/836.feature @@ -0,0 +1 @@ +Adds a config value, in order to disable forwarding room topic changes from Matrix to Discord (`disableRoomTopicNotifications`, false by default). diff --git a/config/config.sample.yaml b/config/config.sample.yaml index 1baa13e..3443e4a 100644 --- a/config/config.sample.yaml +++ b/config/config.sample.yaml @@ -36,6 +36,8 @@ bridge: disableJoinLeaveNotifications: false # Disable Invite echos from matrix disableInviteNotifications: false + # Disable Room Topic echos from matrix + disableRoomTopicNotifications: false # Auto-determine the language of code blocks (this can be CPU-intensive) determineCodeLanguage: false # MXID of an admin user that will be PMd if the bridge experiences problems. Optional diff --git a/config/config.schema.yaml b/config/config.schema.yaml index 5fae41c..eb320ac 100644 --- a/config/config.schema.yaml +++ b/config/config.schema.yaml @@ -30,6 +30,8 @@ properties: type: "boolean" disableInviteNotifications: type: "boolean" + disableRoomTopicNotifications: + type: "boolean" userActivity: type: "object" required: ["minUserActiveDays", "inactiveAfterDays"] diff --git a/src/config.ts b/src/config.ts index 96ebf97..b9362fa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -80,7 +80,7 @@ export class DiscordBridgeConfig { } } -class DiscordBridgeConfigBridge { +export class DiscordBridgeConfigBridge { public domain: string; public homeserverUrl: string; public port: number; @@ -97,6 +97,7 @@ class DiscordBridgeConfigBridge { public disableHereMention: boolean = false; public disableJoinLeaveNotifications: boolean = false; public disableInviteNotifications: boolean = false; + public disableRoomTopicNotifications: boolean = false; public determineCodeLanguage: boolean = false; public activityTracker: UserActivityTrackerConfig = UserActivityTrackerConfig.DEFAULT; public userLimit: number|null = null; diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 5ef631c..6fa4fd9 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -238,10 +238,11 @@ export class MatrixEventProcessor { const allowJoinLeave = !this.config.bridge.disableJoinLeaveNotifications; const allowInvite = !this.config.bridge.disableInviteNotifications; + const allowRoomTopic = !this.config.bridge.disableRoomTopicNotifications; if (event.type === "m.room.name") { msg += `set the name to \`${event.content!.name}\``; - } else if (event.type === "m.room.topic") { + } else if (event.type === "m.room.topic" && allowRoomTopic) { msg += `set the topic to \`${event.content!.topic}\``; } else if (event.type === "m.room.member") { const membership = event.content!.membership; @@ -274,6 +275,9 @@ export class MatrixEventProcessor { // Ignore anything else return; } + } else { + // Ignore anything else + return; } msg += " on Matrix."; diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 2f33a1f..64da1ed 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -20,6 +20,7 @@ import * as Proxyquire from "proxyquire"; import { MockMember } from "./mocks/member"; import { MatrixEventProcessor, MatrixEventProcessorOpts } from "../src/matrixeventprocessor"; import { DiscordBridgeConfig } from "../src/config"; +import { DiscordBridgeConfigBridge } from "../src/config"; import { MockChannel } from "./mocks/channel"; import { IMatrixEvent } from "../src/matrixtypes"; import { AppserviceMock } from "./mocks/appservicemock"; @@ -154,7 +155,7 @@ let KICKBAN_HANDLED = false; let MESSAGE_SENT = false; let MESSAGE_EDITED = false; -function createMatrixEventProcessor(storeMockResults = 0) { +function createMatrixEventProcessor(storeMockResults = 0, configBridge = new DiscordBridgeConfigBridge()) { STATE_EVENT_MSG = ""; MESSAGE_PROCCESS = ""; KICKBAN_HANDLED = false; @@ -173,6 +174,7 @@ function createMatrixEventProcessor(storeMockResults = 0) { OnUpdateUser: async () => { }, }; const config = new DiscordBridgeConfig(); + config.bridge = configBridge; const store = { Get: (a, b) => { @@ -354,6 +356,20 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` set the topic to `Test Topic` on Matrix."); }); + it("Should not echo topic changes", async () => { + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableRoomTopicNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + topic: "Test Topic", + }, + sender: "@user:localhost", + type: "m.room.topic", + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo joins", async () => { const {processor} = createMatrixEventProcessor(); const event = { @@ -367,6 +383,21 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` joined the room on Matrix."); }); + it("Should not echo joins", async () => { + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableJoinLeaveNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + membership: "join", + }, + sender: "@user:localhost", + type: "m.room.member", + unsigned: {}, + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo invites", async () => { const {processor} = createMatrixEventProcessor(); const event = { @@ -381,6 +412,22 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` invited `@user2:localhost` to the room on Matrix."); }); + it("Should not echo invites", async () => { + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableInviteNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + membership: "invite", + }, + sender: "@user:localhost", + state_key: "@user2:localhost", + type: "m.room.member", + unsigned: {}, + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo kicks", async () => { const {processor} = createMatrixEventProcessor(); const event = { @@ -409,6 +456,22 @@ describe("MatrixEventProcessor", () => { await processor.ProcessStateEvent(event); expect(STATE_EVENT_MSG).to.equal("`@user:localhost` left the room on Matrix."); }); + it("Should not echo leaves", async () => { + const bridge = new DiscordBridgeConfigBridge(); + bridge.disableJoinLeaveNotifications = true; + const {processor} = createMatrixEventProcessor(0, bridge); + const event = { + content: { + membership: "leave", + }, + sender: "@user:localhost", + state_key: "@user:localhost", + type: "m.room.member", + unsigned: {}, + } as IMatrixEvent; + await processor.ProcessStateEvent(event); + expect(STATE_EVENT_MSG).to.equal(""); + }); it("Should echo bans", async () => { const {processor} = createMatrixEventProcessor(); const event = { -- GitLab