diff --git a/src/bot.ts b/src/bot.ts index 0a69e4a5f87c9d8e929369786d0226f289e14355..496026fe6d9e9802c3809df1a3ecdd2e227558d0 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -18,9 +18,7 @@ import { Provisioner } from "./provisioner"; // messages get delayed from discord. const MSG_PROCESS_DELAY = 750; const MIN_PRESENCE_UPDATE_DELAY = 250; -const AVATAR_SIZE = 512; // matrix -> discord -const MAX_DISCORD_NAME_LENGTH = 32; -const DISCORD_NAME_START = 0; + // TODO: This is bad. We should be serving the icon from the own homeserver. const MATRIX_ICON_URL = "https://matrix.org/_matrix/media/r0/download/matrix.org/mlxoESwIsTbJrfXyAAogrNxA"; class ChannelLookupResult { diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index f0ef001a7c1639789cd769e2580e2ee992e22299..55061171406ef273b1b3273900aa359d7ccb8e18 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -9,7 +9,8 @@ import * as mime from "mime"; import * as log from "npmlog"; const MaxFileSize = 8000000; - +const MIN_NAME_LENGTH = 2; +const MAX_NAME_LENGTH = 32; export class MatrixEventProcessorOpts { constructor( readonly config: DiscordBridgeConfig, @@ -44,12 +45,18 @@ export class MatrixEventProcessor { if (this.config.bridge.disableHereMention) { body = body.replace(new RegExp(`@here`, "g"), "@ here"); } - + let displayName = event.sender; + let avatarUrl = undefined; if (profile) { - profile.displayname = profile.displayname || event.sender; + if (profile.displayname && + profile.displayname.length >= MIN_NAME_LENGTH && + profile.displayname.length <= MAX_NAME_LENGTH) { + displayName = profile.displayname; + } + if (profile.avatar_url) { const mxClient = this.bridge.getClientFactory().getClientAs(); - profile.avatar_url = mxClient.mxcUrlToHttp(profile.avatar_url); + avatarUrl = mxClient.mxcUrlToHttp(profile.avatar_url); } /* See issue #82 const isMarkdown = (event.content.format === "org.matrix.custom.html"); @@ -60,18 +67,11 @@ export class MatrixEventProcessor { body = `*${body}*`; } */ - return new Discord.RichEmbed({ - author: { - name: profile.displayname, - icon_url: profile.avatar_url, - url: `https://matrix.to/#/${event.sender}`, - }, - description: body, - }); } return new Discord.RichEmbed({ author: { - name: event.sender, + name: displayName.substr(0, MAX_NAME_LENGTH), + icon_url: avatarUrl, url: `https://matrix.to/#/${event.sender}`, }, description: body, diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 611a49dc79f40a175366ac2e77e060cbbacb8a2a..07c4efe7e547e2182702ed939ed9236bfe992a98 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -85,7 +85,7 @@ describe("MatrixEventProcessor", () => { Chai.assert.equal(evt.author.url, "https://matrix.to/#/@test:localhost"); }); - it("Should should contain the users displayname if it exists.", () => { + it("Should contain the users displayname if it exists.", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost", @@ -99,7 +99,7 @@ describe("MatrixEventProcessor", () => { Chai.assert.equal(evt.author.url, "https://matrix.to/#/@test:localhost"); }); - it("Should should contain the users userid if the displayname is not set.", () => { + it("Should contain the users userid if the displayname is not set", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost", @@ -112,7 +112,43 @@ describe("MatrixEventProcessor", () => { Chai.assert.equal(evt.author.url, "https://matrix.to/#/@test:localhost"); }); - it("Should should contain the users avatar if it exists.", () => { + it("Should use the userid when the displayname is too short", () => { + const processor = createMatrixEventProcessor(); + const evt = processor.EventToEmbed({ + sender: "@test:localhost", + content: { + body: "testcontent", + }, + }, { + displayname: "t"}, mockChannel as any); + Chai.assert.equal(evt.author.name, "@test:localhost"); + }); + + it("Should use the userid when displayname is too long", () => { + const processor = createMatrixEventProcessor(); + const evt = processor.EventToEmbed({ + sender: "@test:localhost", + content: { + body: "testcontent", + }, + }, { + displayname: "this is a very very long displayname that should be capped", + }, mockChannel as any); + Chai.assert.equal(evt.author.name, "@test:localhost"); + }); + + it("Should cap the sender name if it is too long", () => { + const processor = createMatrixEventProcessor(); + const evt = processor.EventToEmbed({ + sender: "@testwithalottosayaboutitselfthatwillgoonandonandonandon:localhost", + content: { + body: "testcontent", + }, + }, null, mockChannel as any); + Chai.assert.equal(evt.author.name, "@testwithalottosayaboutitselftha"); + }); + + it("Should contain the users avatar if it exists.", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost", @@ -276,14 +312,6 @@ describe("MatrixEventProcessor", () => { }, }, mxClient)).to.eventually.eq(""); }); - it("message without an info", () => { - const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ - content: { - msgtype: "m.video", - }, - }, mxClient)).to.eventually.eq(""); - }); it("message with a large info.size", () => { const LARGE_FILE = 8000000; const processor = createMatrixEventProcessor();