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 92cdd4fb1ea205c78604d2a17b9a7200808b6a42..673d23436dff5081ec4a2eca6f6b8b24d400c7c6 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -9,7 +9,7 @@ import * as mime from "mime"; import * as log from "npmlog"; const MaxFileSize = 8000000; - +const MIN_NAME_LENGTH = 2; export class MatrixEventProcessorOpts { constructor( readonly config: DiscordBridgeConfig, @@ -44,12 +44,16 @@ 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) { + 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 +64,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, + icon_url: avatarUrl, url: `https://matrix.to/#/${event.sender}`, }, description: body, diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 343558cd114b3daf669d01e391cdf266d5ca874d..f8e104d5f63bc1ad362407163a69813ed7225c38 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -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 should contain the users userid if the displayname is not set", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost", @@ -112,7 +112,19 @@ 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 should contain the users userid if 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 should contain the users avatar if it exists", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost",