diff --git a/src/bot.ts b/src/bot.ts index f3b7ae47a27b109dd38844116ae0e8e99dcdc4bb..6ce62ff1a267298ae9d092fb04a2e9116a382ee5 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -5,7 +5,7 @@ import { DbEmoji } from "./db/dbdataemoji"; import { DbEvent } from "./db/dbdataevent"; import { MatrixUser, RemoteUser, Bridge, Entry } from "matrix-appservice-bridge"; import { Util } from "./util"; -import { MessageProcessor, MessageProcessorOpts } from "./messageprocessor"; +import { MessageProcessor, MessageProcessorOpts, MessageProcessorMatrixResult } from "./messageprocessor"; import { MatrixEventProcessor, MatrixEventProcessorOpts } from "./matrixeventprocessor"; import { PresenceHandler } from "./presencehandler"; import * as Discord from "discord.js"; @@ -435,25 +435,30 @@ export class DiscordBot { }); } - private async SendMessage(msg: Discord.Message) { - const rooms = await this.GetRoomIdsFromChannel(msg.channel) - const intent = this.GetIntentFromDiscordMember(msg.author); + private async SendMatrixMessage(matrixMsg: MessageProcessorMatrixResult, chan: Discord.Channel, + guild: Discord.Guild, author: Discord.User, + msgID: string): Promise<boolean> { + const rooms = await this.GetRoomIdsFromChannel(chan); + const intent = this.GetIntentFromDiscordMember(author); rooms.forEach((room) => { intent.sendMessage(room, { - body: msg.body, + body: matrixMsg.body, msgtype: "m.text", - formatted_body: msg.formattedBody, + formatted_body: matrixMsg.formattedBody, format: "org.matrix.custom.html", }).then((res) => { const evt = new DbEvent(); evt.MatrixId = res.event_id + ";" + room; - evt.DiscordId = msg.id; - evt.ChannelId = msg.channel.id; - evt.GuildId = msg.guild.id; + evt.DiscordId = msgID; + evt.ChannelId = chan.id; + evt.GuildId = guild.id; this.store.Insert(evt); }); }); + + // Sending was a success + return Promise.resolve(true); } private AddGuildMember(guildMember: Discord.GuildMember) { @@ -618,7 +623,7 @@ export class DiscordBot { const editedMsg = await this.msgProcessor.FormatEdit(oldMsg, newMsg); // Send the message to all bridged matrix rooms - this.SendMessage(editedMsg); + this.SendMatrixMessage(editedMsg, newMsg.channel, newMsg.guild, newMsg.author, newMsg.id); } private async DeleteDiscordMessage(msg: Discord.Message) { diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts index 9197421cb10fa7d89e5a6aab8f814ce5387a49fa..bf3303a79b554397e02d3d4522956a66f78b38d9 100644 --- a/src/messageprocessor.ts +++ b/src/messageprocessor.ts @@ -73,11 +73,14 @@ export class MessageProcessor { public async FormatEdit(oldMsg: Discord.Message, newMsg: Discord.Message): Promise<MessageProcessorMatrixResult> { // TODO: Produce a nice, colored diff between the old and new message content - let formattedMsg = await this.FormatDiscordMessage(newMsg); + const formattedOldMsg = await this.FormatDiscordMessage(oldMsg); + const formattedNewMsg = await this.FormatDiscordMessage(newMsg); - formattedMsg.body = "edited: " + formattedMsg.body; - formattedMsg.formattedBody = "<i>edited:</i> " + formattedMsg.formattedBody; - return formattedMsg; + const msg = new MessageProcessorMatrixResult(); + + msg.body = "edit: " + formattedOldMsg.body + " -> " + formattedNewMsg.body; + msg.formattedBody = "<i>edit:</i> <del>" + formattedOldMsg.body + "</del> -> " + formattedNewMsg.body; + return msg; } public InsertEmbeds(content: string, msg: Discord.Message): string { diff --git a/test/test_discordbot.ts b/test/test_discordbot.ts index 95855e72dd60790d02f1a5682af28348c05351e9..88866ca2301b6172a9c90776d248841c40240a32 100644 --- a/test/test_discordbot.ts +++ b/test/test_discordbot.ts @@ -1,8 +1,13 @@ import * as Chai from "chai"; import * as ChaiAsPromised from "chai-as-promised"; import * as Proxyquire from "proxyquire"; +import * as Discord from "discord.js"; import * as log from "npmlog"; +import { MessageProcessorMatrixResult } from "../src/messageprocessor"; +import { MockGuild } from "./mocks/guild"; +import { MockMember } from "./mocks/member"; + Chai.use(ChaiAsPromised); log.level = "silent"; @@ -76,6 +81,60 @@ describe("DiscordBot", () => { return assert.isFulfilled(discordBot.LookupRoom("123", "321")); }); }); + describe("OnMessageUpdate()", () => { + it("should return on an unchanged message", () => { + discordBot = new modDiscordBot.DiscordBot( + config, + mockBridge, + ); + + const guild: any = new MockGuild("123", []); + guild._mockAddMember(new MockMember("12345", "TestUsername")); + const channel = new Discord.TextChannel(guild, null); + const oldMsg = new Discord.Message(channel, null, null); + const newMsg = new Discord.Message(channel, null, null); + oldMsg.embeds = []; + newMsg.embeds = []; + + // Content updated but not changed + oldMsg.content = "a"; + newMsg.content = "a"; + + // Mock the SendMatrixMessage method to check if it is called + let checkMsgSent = false; + discordBot.SendMatrixMessage = (...args) => checkMsgSent = true; + + discordBot.OnMessageUpdate(oldMsg, newMsg); + Chai.assert.equal(checkMsgSent, false); + }); + + it("should send a matrix message on an edited discord message", () => { + discordBot = new modDiscordBot.DiscordBot( + config, + mockBridge, + ); + + const guild: any = new MockGuild("123", []); + guild._mockAddMember(new MockMember("12345", "TestUsername")); + const channel = new Discord.TextChannel(guild, null); + const oldMsg = new Discord.Message(channel, null, null); + const newMsg = new Discord.Message(channel, null, null); + oldMsg.embeds = []; + newMsg.embeds = []; + + // Content updated and edited + oldMsg.content = "a"; + newMsg.content = "b"; + + // Mock the SendMatrixMessage method to check if it is called + let checkMsgSent = false; + discordBot.SendMatrixMessage = (...args) => checkMsgSent = true; + + discordBot.OnMessageUpdate(oldMsg, newMsg); + Chai.assert.equal(checkMsgSent, true); + }); + }); + // describe("ProcessMatrixMsgEvent()", () => { // // }); diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts index 063a29d80d226e4ca9dde2557ebc5ced1c0c87c4..4d9f392ef29e4251910906dda630eb33c72b755b 100644 --- a/test/test_messageprocessor.ts +++ b/test/test_messageprocessor.ts @@ -51,6 +51,24 @@ describe("MessageProcessor", () => { Chai.assert.equal(result.formattedBody, "<p>Hello <em>World</em>!</p>\n"); }); }); + describe("FormatEdit", () => { + it("should format edits appropriately", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot); + const oldMsg = new Discord.Message(null, null, null); + const newMsg = new Discord.Message(null, null, null); + oldMsg.embeds = []; + newMsg.embeds = []; + + // Content updated but not changed + oldMsg.content = "a"; + newMsg.content = "b"; + + const result = await processor.FormatEdit(oldMsg, newMsg); + Chai.assert.equal(result.body, "edit: a -> b"); + Chai.assert.equal(result.formattedBody, "<i>edit:</i> <del>a</del> -> b"); + }); + }); + describe("ReplaceMembers", () => { it("processes members missing from the guild correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);