Skip to content

Commit 9774b1f

Browse files
committed
feat: add external_url to normal messages bridged from discord
Signed-off-by: Seth Falco <[email protected]>
1 parent ece29f4 commit 9774b1f

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

changelog.d/860.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add external_url property to normal messages that are bridged from Discord. Thanks to @SethFalco!

src/bot.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ export class DiscordBot {
11051105
body: result.body,
11061106
format: "org.matrix.custom.html",
11071107
formatted_body: result.formattedBody,
1108+
external_url: msg.url,
11081109
msgtype: result.msgtype,
11091110
};
11101111
if (editEventId) {
@@ -1114,6 +1115,7 @@ export class DiscordBot {
11141115
body: result.body,
11151116
format: "org.matrix.custom.html",
11161117
formatted_body: result.formattedBody,
1118+
external_url: msg.url,
11171119
msgtype: result.msgtype,
11181120
};
11191121
sendContent["m.relates_to"] = {

src/matrixtypes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export interface IMatrixMessage {
5656
format?: string;
5757
"m.new_content"?: any; // tslint:disable-line no-any
5858
"m.relates_to"?: any; // tslint:disable-line no-any
59+
60+
/**
61+
* Indicates where the message came from.
62+
*
63+
* @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}
64+
*/
65+
external_url?: string;
5966
}
6067

6168
export interface IMatrixMediaInfo {

test/mocks/message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class MockMessage {
2828
public channel: Discord.TextChannel | undefined;
2929
public guild: Discord.Guild | undefined;
3030
public author: MockUser;
31+
public url: string;
3132
public mentions: any = {};
3233
constructor(channel?: Discord.TextChannel) {
3334
this.mentions.everyone = false;

test/test_discordbot.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const modDiscordBot = Proxyquire("../src/bot", {
4646
},
4747
},
4848
});
49+
4950
describe("DiscordBot", () => {
5051
let discordBot;
5152
const config = {
@@ -60,6 +61,7 @@ describe("DiscordBot", () => {
6061
discordSendDelay: 50,
6162
},
6263
};
64+
6365
describe("run()", () => {
6466
it("should resolve when ready.", async () => {
6567
discordBot = new modDiscordBot.DiscordBot(
@@ -102,6 +104,7 @@ describe("DiscordBot", () => {
102104
await discordBot.LookupRoom("123", "321");
103105
});
104106
});
107+
105108
describe("OnMessage()", () => {
106109
const channel = new MockTextChannel();
107110
const msg = new MockMessage(channel);
@@ -130,6 +133,7 @@ describe("DiscordBot", () => {
130133
};
131134
return discord;
132135
}
136+
133137
it("ignores own messages", async () => {
134138
discordBot = getDiscordBot();
135139
const guild: any = new MockGuild("123", []);
@@ -140,46 +144,54 @@ describe("DiscordBot", () => {
140144
await discordBot.OnMessage(msg);
141145
expect(mockBridge.getIntent(author.id).wasCalled("sendEvent", false)).to.equal(0);
142146
});
147+
143148
it("Passes on !matrix commands", async () => {
144149
discordBot = getDiscordBot();
145150
msg.author = author;
146151
msg.content = "!matrix test";
147152
await discordBot.OnMessage(msg);
148153
expect(HANDLE_COMMAND).to.be.true;
149154
});
155+
150156
it("skips empty messages", async () => {
151157
discordBot = getDiscordBot();
152158
msg.content = "";
153159
msg.author = author;
154160
await discordBot.OnMessage(msg as any);
155161
expect(mockBridge.getIntent(author.id).wasCalled("sendEvent", false)).to.equal(0);
156162
});
163+
157164
it("sends normal messages", async () => {
158165
discordBot = getDiscordBot();
159166
msg.author = author;
160167
msg.content = "Foxies are amazing!";
161168
await discordBot.OnMessage(msg as any);
162169
mockBridge.getIntent(author.id).wasCalled("sendEvent");
163170
});
171+
164172
it("sends edit messages", async () => {
165173
discordBot = getDiscordBot();
166174
msg.author = author;
167175
msg.content = "Foxies are super amazing!";
176+
msg.url = "https://discord.com/channels/123/321/1028397843632902214";
168177
await discordBot.OnMessage(msg, "editevent");
169178
mockBridge.getIntent(author.id).wasCalled("sendEvent", true, "!asdf:localhost", {
170179
"body": "* Foxies are super amazing!",
171180
"format": "org.matrix.custom.html",
172181
"formatted_body": "* Foxies are super amazing!",
182+
"external_url": "https://discord.com/channels/123/321/1028397843632902214",
173183
"m.new_content": {
174184
body: "Foxies are super amazing!",
175185
format: "org.matrix.custom.html",
176186
formatted_body: "Foxies are super amazing!",
187+
external_url: "https://discord.com/channels/123/321/1028397843632902214",
177188
msgtype: "m.text",
178189
},
179190
"m.relates_to": { event_id: "editevent", rel_type: "m.replace" },
180191
"msgtype": "m.text",
181192
});
182193
});
194+
183195
it("uploads images", async () => {
184196
discordBot = getDiscordBot();
185197
msg.author = author;
@@ -206,6 +218,7 @@ describe("DiscordBot", () => {
206218
url: "mxc://someimage.png",
207219
});
208220
});
221+
209222
it("uploads videos", async () => {
210223
discordBot = getDiscordBot();
211224
msg.author = author;
@@ -232,6 +245,7 @@ describe("DiscordBot", () => {
232245
url: "mxc://foxes.mov",
233246
});
234247
});
248+
235249
it("uploads audio", async () => {
236250
discordBot = getDiscordBot();
237251
msg.author = author;
@@ -256,6 +270,7 @@ describe("DiscordBot", () => {
256270
url: "mxc://meow.mp3",
257271
});
258272
});
273+
259274
it("uploads other files", async () => {
260275
discordBot = getDiscordBot();
261276
msg.author = author;
@@ -281,6 +296,7 @@ describe("DiscordBot", () => {
281296
});
282297
});
283298
});
299+
284300
describe("OnMessageUpdate()", () => {
285301
it("should return on an unchanged message", async () => {
286302
discordBot = new modDiscordBot.DiscordBot(
@@ -308,6 +324,7 @@ describe("DiscordBot", () => {
308324
await discordBot.OnMessageUpdate(oldMsg, newMsg);
309325
expect(checkMsgSent).to.be.false;
310326
});
327+
311328
it("should send a matrix edit on an edited discord message", async () => {
312329
discordBot = new modDiscordBot.DiscordBot(
313330
config,
@@ -347,6 +364,7 @@ describe("DiscordBot", () => {
347364
await discordBot.OnMessageUpdate(oldMsg, newMsg);
348365
expect(checkEditEventSent).to.equal("editedid");
349366
});
367+
350368
it("should send a new message if no store event found", async () => {
351369
discordBot = new modDiscordBot.DiscordBot(
352370
config,
@@ -392,6 +410,7 @@ describe("DiscordBot", () => {
392410
expect(checkEditEventSent).to.be.undefined;
393411
});
394412
});
413+
395414
describe("event:message", () => {
396415
it("should delay messages so they arrive in order", async () => {
397416
discordBot = new modDiscordBot.DiscordBot(
@@ -414,6 +433,7 @@ describe("DiscordBot", () => {
414433
}
415434
await discordBot.discordMessageQueue[CHANID];
416435
});
436+
417437
it("should handle messages that reject in the queue", async () => {
418438
discordBot = new modDiscordBot.DiscordBot(
419439
config,

0 commit comments

Comments
 (0)