Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider edf792c4 rédigé par Will Hunt's avatar Will Hunt Validation de GitHub
Parcourir les fichiers

Merge pull request #124 from Half-Shot/hs/fix-short-webhook-names

Better edge case filtering on discord embed names
parents 8efcd113 da6d14d0
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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 {
......
......@@ -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,
......
......@@ -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();
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter