From b60292f2734c787315a9504f0ab1fe7bc1957e3e Mon Sep 17 00:00:00 2001 From: Will Hunt <will@half-shot.uk> Date: Tue, 15 May 2018 16:10:25 +0100 Subject: [PATCH] Set webhook names to sender names when displayname is too short --- src/bot.ts | 4 +--- src/matrixeventprocessor.ts | 23 ++++++++++------------- test/test_matrixeventprocessor.ts | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 0a69e4a..496026f 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 92cdd4f..673d234 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 343558c..f8e104d 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", -- GitLab