From 9f623bae13dd1980522f61fd50127a471d2d0d6b Mon Sep 17 00:00:00 2001 From: Will Hunt <will@half-shot.uk> Date: Tue, 15 May 2018 16:23:37 +0100 Subject: [PATCH] Apply maximum name limits to embeds...again (sorry Travis) --- src/matrixeventprocessor.ts | 7 +++++-- test/test_matrixeventprocessor.ts | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 673d234..fb49a2f 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -10,6 +10,7 @@ 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, @@ -47,7 +48,9 @@ export class MatrixEventProcessor { let displayName = event.sender; let avatarUrl = undefined; if (profile) { - if (profile.displayname && profile.displayname.length > MIN_NAME_LENGTH) { + if (profile.displayname && + profile.displayname.length >= MIN_NAME_LENGTH && + profile.displayname.length <= MAX_NAME_LENGTH) { displayName = profile.displayname; } @@ -67,7 +70,7 @@ export class MatrixEventProcessor { } return new Discord.RichEmbed({ author: { - name: displayName, + name: displayName.substr(0, MAX_NAME_LENGTH), icon_url: avatarUrl, url: `https://matrix.to/#/${event.sender}`, }, diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index f8e104d..7565571 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -112,7 +112,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 too short", () => { + it("Should use the userid when the displayname is too short", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ sender: "@test:localhost", @@ -124,6 +124,30 @@ describe("MatrixEventProcessor", () => { 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 should contain the users avatar if it exists", () => { const processor = createMatrixEventProcessor(); const evt = processor.EventToEmbed({ -- GitLab