Skip to content

Commit

Permalink
feat: add external_url to normal messages bridged from discord
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Falco <[email protected]>
  • Loading branch information
SethFalco committed Nov 17, 2022
1 parent ece29f4 commit 9774b1f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/860.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add external_url property to normal messages that are bridged from Discord. Thanks to @SethFalco!
2 changes: 2 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ export class DiscordBot {
body: result.body,
format: "org.matrix.custom.html",
formatted_body: result.formattedBody,
external_url: msg.url,
msgtype: result.msgtype,
};
if (editEventId) {
Expand All @@ -1114,6 +1115,7 @@ export class DiscordBot {
body: result.body,
format: "org.matrix.custom.html",
formatted_body: result.formattedBody,
external_url: msg.url,
msgtype: result.msgtype,
};
sendContent["m.relates_to"] = {
Expand Down
7 changes: 7 additions & 0 deletions src/matrixtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export interface IMatrixMessage {
format?: string;
"m.new_content"?: any; // tslint:disable-line no-any
"m.relates_to"?: any; // tslint:disable-line no-any

/**
* Indicates where the message came from.
*
* @see {@link https://spec.matrix.org/v1.3/application-service-api/#referencing-messages-from-a-third-party-network Referencing messages from a third party network}
*/
external_url?: string;
}

export interface IMatrixMediaInfo {
Expand Down
1 change: 1 addition & 0 deletions test/mocks/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class MockMessage {
public channel: Discord.TextChannel | undefined;
public guild: Discord.Guild | undefined;
public author: MockUser;
public url: string;
public mentions: any = {};
constructor(channel?: Discord.TextChannel) {
this.mentions.everyone = false;
Expand Down
20 changes: 20 additions & 0 deletions test/test_discordbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const modDiscordBot = Proxyquire("../src/bot", {
},
},
});

describe("DiscordBot", () => {
let discordBot;
const config = {
Expand All @@ -60,6 +61,7 @@ describe("DiscordBot", () => {
discordSendDelay: 50,
},
};

describe("run()", () => {
it("should resolve when ready.", async () => {
discordBot = new modDiscordBot.DiscordBot(
Expand Down Expand Up @@ -102,6 +104,7 @@ describe("DiscordBot", () => {
await discordBot.LookupRoom("123", "321");
});
});

describe("OnMessage()", () => {
const channel = new MockTextChannel();
const msg = new MockMessage(channel);
Expand Down Expand Up @@ -130,6 +133,7 @@ describe("DiscordBot", () => {
};
return discord;
}

it("ignores own messages", async () => {
discordBot = getDiscordBot();
const guild: any = new MockGuild("123", []);
Expand All @@ -140,46 +144,54 @@ describe("DiscordBot", () => {
await discordBot.OnMessage(msg);
expect(mockBridge.getIntent(author.id).wasCalled("sendEvent", false)).to.equal(0);
});

it("Passes on !matrix commands", async () => {
discordBot = getDiscordBot();
msg.author = author;
msg.content = "!matrix test";
await discordBot.OnMessage(msg);
expect(HANDLE_COMMAND).to.be.true;
});

it("skips empty messages", async () => {
discordBot = getDiscordBot();
msg.content = "";
msg.author = author;
await discordBot.OnMessage(msg as any);
expect(mockBridge.getIntent(author.id).wasCalled("sendEvent", false)).to.equal(0);
});

it("sends normal messages", async () => {
discordBot = getDiscordBot();
msg.author = author;
msg.content = "Foxies are amazing!";
await discordBot.OnMessage(msg as any);
mockBridge.getIntent(author.id).wasCalled("sendEvent");
});

it("sends edit messages", async () => {
discordBot = getDiscordBot();
msg.author = author;
msg.content = "Foxies are super amazing!";
msg.url = "https://discord.com/channels/123/321/1028397843632902214";
await discordBot.OnMessage(msg, "editevent");
mockBridge.getIntent(author.id).wasCalled("sendEvent", true, "!asdf:localhost", {
"body": "* Foxies are super amazing!",
"format": "org.matrix.custom.html",
"formatted_body": "* Foxies are super amazing!",
"external_url": "https://discord.com/channels/123/321/1028397843632902214",
"m.new_content": {
body: "Foxies are super amazing!",
format: "org.matrix.custom.html",
formatted_body: "Foxies are super amazing!",
external_url: "https://discord.com/channels/123/321/1028397843632902214",
msgtype: "m.text",
},
"m.relates_to": { event_id: "editevent", rel_type: "m.replace" },
"msgtype": "m.text",
});
});

it("uploads images", async () => {
discordBot = getDiscordBot();
msg.author = author;
Expand All @@ -206,6 +218,7 @@ describe("DiscordBot", () => {
url: "mxc://someimage.png",
});
});

it("uploads videos", async () => {
discordBot = getDiscordBot();
msg.author = author;
Expand All @@ -232,6 +245,7 @@ describe("DiscordBot", () => {
url: "mxc://foxes.mov",
});
});

it("uploads audio", async () => {
discordBot = getDiscordBot();
msg.author = author;
Expand All @@ -256,6 +270,7 @@ describe("DiscordBot", () => {
url: "mxc://meow.mp3",
});
});

it("uploads other files", async () => {
discordBot = getDiscordBot();
msg.author = author;
Expand All @@ -281,6 +296,7 @@ describe("DiscordBot", () => {
});
});
});

describe("OnMessageUpdate()", () => {
it("should return on an unchanged message", async () => {
discordBot = new modDiscordBot.DiscordBot(
Expand Down Expand Up @@ -308,6 +324,7 @@ describe("DiscordBot", () => {
await discordBot.OnMessageUpdate(oldMsg, newMsg);
expect(checkMsgSent).to.be.false;
});

it("should send a matrix edit on an edited discord message", async () => {
discordBot = new modDiscordBot.DiscordBot(
config,
Expand Down Expand Up @@ -347,6 +364,7 @@ describe("DiscordBot", () => {
await discordBot.OnMessageUpdate(oldMsg, newMsg);
expect(checkEditEventSent).to.equal("editedid");
});

it("should send a new message if no store event found", async () => {
discordBot = new modDiscordBot.DiscordBot(
config,
Expand Down Expand Up @@ -392,6 +410,7 @@ describe("DiscordBot", () => {
expect(checkEditEventSent).to.be.undefined;
});
});

describe("event:message", () => {
it("should delay messages so they arrive in order", async () => {
discordBot = new modDiscordBot.DiscordBot(
Expand All @@ -414,6 +433,7 @@ describe("DiscordBot", () => {
}
await discordBot.discordMessageQueue[CHANID];
});

it("should handle messages that reject in the queue", async () => {
discordBot = new modDiscordBot.DiscordBot(
config,
Expand Down

0 comments on commit 9774b1f

Please sign in to comment.