diff --git a/src/discordcommandhandler.ts b/src/discordcommandhandler.ts index ec89e5349ff61e5abd0547cd61264ba3ae68443d..7606383448f242eb16a307d9ce58dcea533e5fc6 100644 --- a/src/discordcommandhandler.ts +++ b/src/discordcommandhandler.ts @@ -126,7 +126,7 @@ export class DiscordCommandHandler { let errorMsg = ""; await Promise.all(allChannelMxids.map(async (chanMxid) => { try { - await this.bridge.botIntent[funcKey](chanMxid, name); + await this.bridge.botIntent.underlyingClient[funcKey + "User"](chanMxid, name); } catch (e) { // maybe we don't have permission to kick/ban/unban...? errorMsg += `\nCouldn't ${funcKey} ${name} from ${chanMxid}`; diff --git a/src/discordmessageprocessor.ts b/src/discordmessageprocessor.ts index 3903202d3d63f1a8dba0fe89db216c9a1c1457a8..29849fcac17724e4cc19f21c649dadd5ddc9f2bb 100644 --- a/src/discordmessageprocessor.ts +++ b/src/discordmessageprocessor.ts @@ -79,6 +79,7 @@ export class DiscordMessageProcessor { discordCallback: this.getDiscordParseCallbacksHTML(msg), }); + // parse the plain text stuff content = markdown.toHTML(content, { discordCallback: this.getDiscordParseCallbacks(msg), diff --git a/src/matrixmessageprocessor.ts b/src/matrixmessageprocessor.ts index dcc3603be9ba72467453f03885091a7497e13191..cdaf95f7dcf9a3a79327e716657e62d1276d97ba 100644 --- a/src/matrixmessageprocessor.ts +++ b/src/matrixmessageprocessor.ts @@ -158,8 +158,7 @@ export class MatrixMessageProcessor { try { const resp = await this.params.mxClient.lookupRoomAlias(id); if (resp && resp.roomId) { - const roomId = resp.roomId; - const channel = await this.bot.GetChannelFromRoomId(roomId); + const channel = await this.bot.GetChannelFromRoomId(resp.roomId); return `<#${channel.id}>`; } } catch (err) { } // ignore, room ID wasn't found diff --git a/src/matrixroomhandler.ts b/src/matrixroomhandler.ts index 375a3e96bc997b70fdb5d0a8e31d7d462604751f..c33309f8cdca5d2c69faff15c88273862e3e9d71 100644 --- a/src/matrixroomhandler.ts +++ b/src/matrixroomhandler.ts @@ -182,7 +182,7 @@ export class MatrixRoomHandler { // fields: { // guild_id: guild.id, // }, - // icon: guild.iconURL || ICON_URL, // TODO: Use icons from our content repo. Potential security risk. + // icon: guild.iconURL || ICON_URL, // network_id: guild.id, // }; // }), diff --git a/test/mocks/appservicemock.ts b/test/mocks/appservicemock.ts index 7458d46e8f5781c5b9e8049157dbd78b1a44796f..d1a7b15fd63ef07bcc70b679e3b791d990885287 100644 --- a/test/mocks/appservicemock.ts +++ b/test/mocks/appservicemock.ts @@ -69,10 +69,6 @@ class IntentMock extends AppserviceMockBase { this.underlyingClient = new MatrixClientMock(); } - public ban() { - this.funcCalled("ban"); - } - public join() { this.funcCalled("join"); } @@ -81,20 +77,16 @@ class IntentMock extends AppserviceMockBase { this.funcCalled("joinRoom"); } - public kick() { - this.funcCalled("kick"); - } - public leave() { this.funcCalled("leave"); } + public sendText(roomId: string, body: string) { + this.funcCalled("sendText", roomId, body); + } + public sendEvent(roomId: string, body: string) { this.funcCalled("sendEvent", roomId, body); - } - - public unban() { - this.funcCalled("unban"); } } @@ -104,6 +96,10 @@ class MatrixClientMock extends AppserviceMockBase { super(); } + public banUser(roomId: string, userId: string) { + this.funcCalled("banUser", roomId, userId); + } + public sendMessage(roomId: string, eventContent: IMatrixEvent) { this.funcCalled("sendMessage", roomId, eventContent); } @@ -123,6 +119,10 @@ class MatrixClientMock extends AppserviceMockBase { this.funcCalled("leaveRoom", roomId); } + public kickUser(roomId: string, userId: string) { + this.funcCalled("kickUser", roomId, userId); + } + public sendStateEvent(roomId: string, type: string, stateKey: string, content: {}) { this.funcCalled("sendStateEvent", roomId, type, stateKey, content); } @@ -146,4 +146,8 @@ class MatrixClientMock extends AppserviceMockBase { return { alias: "#alias:localhost" }; } } + + public unbanUser(roomId: string, userId: string) { + this.funcCalled("unbanUser", roomId, userId); + } } diff --git a/test/test_clientfactory.ts b/test/test_clientfactory.ts index b3d6df755287c1cdfeebd53042c506c68b014a8f..72381e511e09a4c6bd67c9e3e23ae4f17dc6a4a4 100644 --- a/test/test_clientfactory.ts +++ b/test/test_clientfactory.ts @@ -17,7 +17,6 @@ limitations under the License. import * as Chai from "chai"; import * as Proxyquire from "proxyquire"; import {DiscordBridgeConfigAuth} from "../src/config"; -import {MockDiscordClient} from "./mocks/discordclient"; // we are a test file and thus need those /* tslint:disable:no-unused-expression max-file-line-count no-any */ diff --git a/test/test_discordcommandhandler.ts b/test/test_discordcommandhandler.ts index a66439530ac0a2a4cfc91791f9753858f3088125..f296ccc0f5eb6c13cf8dca405eac507624bb1fd1 100644 --- a/test/test_discordcommandhandler.ts +++ b/test/test_discordcommandhandler.ts @@ -21,40 +21,19 @@ import { MockChannel } from "./mocks/channel"; import { MockMember } from "./mocks/member"; import { MockGuild } from "./mocks/guild"; import { Util } from "../src/util"; +import { AppserviceMock } from "./mocks/appservicemock"; // we are a test file and thus need those /* tslint:disable:no-unused-expression max-file-line-count no-any */ const expect = Chai.expect; -let USERSJOINED = 0; -let USERSKICKED = 0; -let USERSBANNED = 0; -let USERSUNBANNED = 0; let ROOMSUNBRIDGED = 0; -let MESSAGESENT: any = {}; let MARKED = -1; function createCH(opts: any = {}) { - USERSJOINED = 0; - USERSKICKED = 0; - USERSBANNED = 0; - USERSUNBANNED = 0; ROOMSUNBRIDGED = 0; - MESSAGESENT = {}; MARKED = -1; - const bridge = { - getIntent: () => { - return { - ban: async () => { USERSBANNED++; }, - getEvent: () => ({ content: { } }), - join: () => { USERSJOINED++; }, - kick: async () => { USERSKICKED++; }, - leave: () => { }, - sendMessage: async (roomId, content) => { MESSAGESENT = content; return content; }, - unban: async () => { USERSUNBANNED++; }, - }; - }, - }; + const bridge = new AppserviceMock(); const cs = { GetRoomIdsFromChannel: async (chan) => { return [`#${chan.id}:localhost`]; @@ -83,12 +62,12 @@ function createCH(opts: any = {}) { }, }, })).DiscordCommandHandler; - return new discordCommandHndlr(bridge as any, discord as any); + return {handler: new discordCommandHndlr(bridge as any, discord as any), bridge}; } describe("DiscordCommandHandler", () => { it("will kick a member", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -102,10 +81,10 @@ describe("DiscordCommandHandler", () => { member, }; await handler.Process(message); - expect(USERSKICKED).equals(1); + bridge.botIntent.underlyingClient.wasCalled("kickUser", true, "#123:localhost", "@123456:localhost"); }); it("will kick a member in all guild rooms", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel, (new MockChannel("456"))]); channel.guild = guild; @@ -120,10 +99,10 @@ describe("DiscordCommandHandler", () => { }; await handler.Process(message); // tslint:disable-next-line:no-magic-numbers - expect(USERSKICKED).equals(2); + expect(bridge.botIntent.underlyingClient.wasCalled("kickUser"),).to.equal(2); }); it("will deny permission", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -137,10 +116,10 @@ describe("DiscordCommandHandler", () => { member, }; await handler.Process(message); - expect(USERSKICKED).equals(0); + expect(bridge.botIntent.underlyingClient.wasCalled("kickUser", false)).to.equal(0); }); it("will ban a member", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -154,10 +133,10 @@ describe("DiscordCommandHandler", () => { member, }; await handler.Process(message); - expect(USERSBANNED).equals(1); + expect(bridge.botIntent.underlyingClient.wasCalled("banUser")).to.equal(1); }); it("will unban a member", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -171,10 +150,10 @@ describe("DiscordCommandHandler", () => { member, }; await handler.Process(message); - expect(USERSUNBANNED).equals(1); + expect(bridge.botIntent.underlyingClient.wasCalled("unbanUser")).to.equal(1); }); it("handles !matrix approve", async () => { - const handler: any = createCH(); + const {handler, bridge} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -191,7 +170,7 @@ describe("DiscordCommandHandler", () => { expect(MARKED).equals(1); }); it("handles !matrix deny", async () => { - const handler: any = createCH(); + const {handler} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; @@ -208,7 +187,7 @@ describe("DiscordCommandHandler", () => { expect(MARKED).equals(0); }); it("handles !matrix unbridge", async () => { - const handler: any = createCH(); + const {handler} = createCH(); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); channel.guild = guild; diff --git a/test/test_matrixcommandhandler.ts b/test/test_matrixcommandhandler.ts index 996d3a6e1255c0faacc42f35c28b87cfd4a3d19a..987c3cfe823330a6688706094c2761df54d35af1 100644 --- a/test/test_matrixcommandhandler.ts +++ b/test/test_matrixcommandhandler.ts @@ -18,29 +18,16 @@ import * as Chai from "chai"; import { Util } from "../src/util"; import { DiscordBridgeConfig } from "../src/config"; import { MockChannel } from "./mocks/channel"; -<<<<<<< HEAD import { AppserviceMock } from "./mocks/appservicemock"; -======= import * as Proxyquire from "proxyquire"; ->>>>>>> develop // we are a test file and thus need those /* tslint:disable:no-unused-expression max-file-line-count no-any */ const expect = Chai.expect; -let USERSJOINED = 0; -let USERSKICKED = 0; -let USERSBANNED = 0; -let USERSUNBANNED = 0; -let MESSAGESENT: any = {}; function createCH(opts: any = {}) { - USERSJOINED = 0; - USERSKICKED = 0; - USERSBANNED = 0; - USERSUNBANNED = 0; - MESSAGESENT = {}; const bridge = new AppserviceMock(); @@ -51,14 +38,6 @@ function createCH(opts: any = {}) { } else { config.bridge.enableSelfServiceBridging = true; } - const mxClient = { - getUserId: () => "@user:localhost", - joinRoom: async () => { - USERSJOINED++; - }, - sendReadReceipt: async () => { }, - setRoomDirectoryVisibilityAppService: async () => { }, - }; const provisioner = { AskBridgePermission: async () => { if (opts.denyBridgePermission) { @@ -101,7 +80,7 @@ function createCH(opts: any = {}) { }, }, })).MatrixCommandHandler; - return new MatrixCommandHndl(bot as any, bridge, config); + return {handler: new MatrixCommandHndl(bot as any, bridge, config), bridge}; } function createEvent(msg: string, room?: string, userId?: string) { @@ -125,9 +104,9 @@ function createContext(remoteData?: any) { describe("MatrixCommandHandler", () => { describe("Process", () => { it("should not process command if not in room", async () => { - const handler: any = createCH({disableSS: true}); + const {handler, bridge} = createCH({disableSS: true}); await handler.Process(createEvent("", "!666:localhost"), createContext()); - expect(MESSAGESENT.body).to.equal(undefined); + expect(bridge.botIntent.underlyingClient.) }); it("should warn if self service is disabled", async () => { const handler: any = createCH({disableSS: true}); diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index dd1a5d2e543b6740a5079750921b3af2626d472d..4e9c8a7605303f8d651096e6689f398ebe3b1d78 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -17,13 +17,7 @@ limitations under the License. import * as Chai from "chai"; import * as Discord from "discord.js"; import * as Proxyquire from "proxyquire"; - -import { PresenceHandler } from "../src/presencehandler"; -import { DiscordBot } from "../src/bot"; -import { MockGuild } from "./mocks/guild"; -import { MockCollection } from "./mocks/collection"; import { MockMember } from "./mocks/member"; -import { MockEmoji } from "./mocks/emoji"; import { MatrixEventProcessor, MatrixEventProcessorOpts } from "../src/matrixeventprocessor"; import { DiscordBridgeConfig } from "../src/config"; import { MockChannel } from "./mocks/channel"; diff --git a/test/test_matrixmessageprocessor.ts b/test/test_matrixmessageprocessor.ts index d154bfa98c55de25c00b4510d49ff08b487d3bca..3319248d0c5df278b9e81dcde44346c365a2e23a 100644 --- a/test/test_matrixmessageprocessor.ts +++ b/test/test_matrixmessageprocessor.ts @@ -15,12 +15,10 @@ limitations under the License. */ import * as Chai from "chai"; -import * as Discord from "discord.js"; import { MockGuild } from "./mocks/guild"; import { MockMember } from "./mocks/member"; import { MockChannel } from "./mocks/channel"; import { MockEmoji } from "./mocks/emoji"; -import { DiscordBot } from "../src/bot"; import { DbEmoji } from "../src/db/dbdataemoji"; import { MatrixMessageProcessor } from "../src/matrixmessageprocessor";