diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 2ad0107368de6ebf72a54f592d7917a9a968eca2..f4a0ed490245ec08a66f93a67b7dae324a99dc14 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -120,6 +120,16 @@ export class MatrixEventProcessor { messageEmbed.setDescription(body); await this.SetEmbedAuthor(messageEmbed, event.sender, profile); const replyEmbed = getReply ? (await this.GetEmbedForReply(event, channel)) : undefined; + if (replyEmbed && replyEmbed.fields) { + for (let i = 0; i < replyEmbed.fields.length; i++) { + const f = replyEmbed.fields[i]; + if (f.name === "ping") { + messageEmbed.description += `\n(${f.value})`; + replyEmbed.fields.splice(i, 1); + break; + } + } + } return { messageEmbed, replyEmbed, @@ -192,7 +202,16 @@ export class MatrixEventProcessor { try { const sourceEvent = await intent.getEvent(event.room_id, eventId); sourceEvent.content.body = sourceEvent.content.body || "Reply with unknown content"; - return (await this.EventToEmbed(sourceEvent, channel, false)).messageEmbed; + const replyEmbed = (await this.EventToEmbed(sourceEvent, channel, false)).messageEmbed; + + // if we reply to a discord member, ping them! + if (this.bridge.getBot().isRemoteUser(sourceEvent.sender)) { + const uid = new MatrixUser(sourceEvent.sender.replace("@", "")).localpart.substring("_discord".length); + replyEmbed.addField("ping", `<@${uid}>`); + } + + replyEmbed.setTimestamp(new Date(sourceEvent.origin_server_ts)); + return replyEmbed; } catch (ex) { log.warn("Failed to handle reply, showing a unknown embed:", ex); } diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 3d61f0ee22444532ac03cb2aaa214b07d313a770..6280f8112fcc732d6fd50dd83feca7741d59aacd 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -16,6 +16,8 @@ import { IMatrixEvent } from "../src/matrixtypes"; // we are a test file and thus need those /* tslint:disable:no-unused-expression max-file-line-count no-any */ +const TEST_TIMESTAMP = 1337; + const expect = Chai.expect; // const assert = Chai.assert; const bot = { @@ -63,7 +65,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { const bridge = { getBot: () => { return { - isRemoteUser: () => false, + isRemoteUser: (s) => s.includes("@_discord_"), }; }, getClientFactory: () => { @@ -88,6 +90,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { content: { body: "Hello!", }, + origin_server_ts: TEST_TIMESTAMP, sender: "@doggo:localhost", }; } else if (eventId === "$reply:localhost") { @@ -114,6 +117,13 @@ function createMatrixEventProcessor(): MatrixEventProcessor { }, sender: "@doggo:localhost", }; + } else if (eventId === "$discord:localhost") { + return { + content: { + body: "Foxies", + }, + sender: "@_discord_1234:localhost", + }; } return null; }, @@ -137,6 +147,11 @@ function createMatrixEventProcessor(): MatrixEventProcessor { return Buffer.alloc(size); }, }); + const discordbot = { + GetDiscordUserOrMember: async (s) => { + return new Discord.User({ } as any, { username: "Someuser" }); + }, + }; return new (Proxyquire("../src/matrixeventprocessor", { "./util": { @@ -146,7 +161,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { new MatrixEventProcessorOpts( config, bridge, - {} as any, + discordbot as any, )); } const mockChannel = new MockChannel(); @@ -416,6 +431,23 @@ describe("MatrixEventProcessor", () => { } as IMatrixEvent, mockChannel as any); Chai.assert.equal(embeds.messageEmbed.description, ""); }); + it("Should ping the user on discord replies", async () => { + const processor = createMatrixEventProcessor(); + const embeds = await processor.EventToEmbed({ + content: { + "body": "Bunnies", + "m.relates_to": { + "m.in_reply_to": { + event_id: "$discord:localhost", + }, + }, + "url": "mxc://bunny", + }, + sender: "@test:localhost", + type: "m.room.member", + } as IMatrixEvent, mockChannel as any); + Chai.assert.equal(embeds.messageEmbed.description, "Bunnies\n(<@1234>)"); + }); }); describe("HandleAttachment", () => { const SMALL_FILE = 200; @@ -627,5 +659,45 @@ This is the reply`, expect(result!.author!.icon_url).to.be.equal("https://fakeurl.com"); expect(result!.author!.url).to.be.equal("https://matrix.to/#/@doggo:localhost"); }); + it("should add the reply time", async () => { + const processor = createMatrixEventProcessor(); + const result = await processor.GetEmbedForReply({ + content: { + "body": "Test", + "m.relates_to": { + "m.in_reply_to": { + event_id: "$goodEvent:localhost", + }, + }, + }, + sender: "@test:localhost", + type: "m.room.message", + } as IMatrixEvent, mockChannel as any); + expect(result!.timestamp!.getTime()).to.be.equal(TEST_TIMESTAMP); + }); + it("should add field for discord replies", async () => { + const processor = createMatrixEventProcessor(); + const result = await processor.GetEmbedForReply({ + content: { + "body": "foxfoxfox", + "m.relates_to": { + "m.in_reply_to": { + event_id: "$discord:localhost", + }, + }, + }, + sender: "@test:localhost", + type: "m.room.message", + } as IMatrixEvent, mockChannel as any); + let foundField = false; + for (const f of result!.fields!) { + if (f.name === "ping") { + foundField = true; + expect(f.value).to.be.equal("<@1234>"); + break; + } + } + expect(foundField).to.be.true; + }); }); });