Compare commits

...

2 commits

2 changed files with 38 additions and 6 deletions

View file

@ -158,6 +158,31 @@ async function getMemberFromCacheOrHomeserver(roomID, mxid, api) {
}) })
} }
/**
* Splits a display name into one chunk containing <=80 characters, and another chunk containing the rest of the characters. Splits on
* whitespace if possible.
* These chunks, respectively, go in the display name, and at the top of the message.
* If the second part isn't empty, it'll also contain boldening markdown and a line break at the end, so that regardless of its value it
* can be prepended to the message content as-is.
* @summary Splits too-long Matrix names into a display name chunk and a message content chunk.
* @param {string} displayName - The Matrix side display name to chop up.
* @returns {[string, string]} [shortened display name, display name runoff]
*/
function splitDisplayName(displayName) {
/** @type {string[]} */
let displayNameChunks = chunk(displayName, 80)
if (displayNameChunks.length === 1) {
return [displayName, ""]
} else {
const displayNamePreRunoff = displayNameChunks[0]
// displayNameRunoff is a slice of the original rather than a concatenation of the rest of the chunks in order to preserve whatever whitespace it was broken on.
const displayNameRunoff = `**${displayName.slice(displayNamePreRunoff.length + 1)}**\n`
return [displayNamePreRunoff, displayNameRunoff]
}
}
/** /**
* @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker | Ty.Event.Outer_M_Room_Message_Encrypted_File} event * @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker | Ty.Event.Outer_M_Room_Message_Encrypted_File} event
* @param {import("discord-api-types/v10").APIGuild} guild * @param {import("discord-api-types/v10").APIGuild} guild
@ -179,6 +204,13 @@ async function eventToMessage(event, guild, di) {
const member = await getMemberFromCacheOrHomeserver(event.room_id, event.sender, di?.api) const member = await getMemberFromCacheOrHomeserver(event.room_id, event.sender, di?.api)
if (member.displayname) displayName = member.displayname if (member.displayname) displayName = member.displayname
if (member.avatar_url) avatarURL = utils.getPublicUrlForMxc(member.avatar_url) || undefined if (member.avatar_url) avatarURL = utils.getPublicUrlForMxc(member.avatar_url) || undefined
// If the display name is too long to be put into the webhook (80 characters is the maximum),
// put the excess characters into displayNameRunoff, later to be put at the top of the message
let [displayNameShortened, displayNameRunoff] = splitDisplayName(displayName)
// If the message type is m.emote, the full name is already included at the start of the message, so remove any runoff
if (event.type === "m.room.message" && event.content.msgtype === "m.emote") {
displayNameRunoff = ""
}
let content = event.content.body // ultimate fallback let content = event.content.body // ultimate fallback
const attachments = [] const attachments = []
@ -364,13 +396,13 @@ async function eventToMessage(event, guild, di) {
pendingFiles.push({name: filename, url}) pendingFiles.push({name: filename, url})
} }
content = replyLine + content content = displayNameRunoff + replyLine + content
// Split into 2000 character chunks // Split into 2000 character chunks
const chunks = chunk(content, 2000) const chunks = chunk(content, 2000)
messages = messages.concat(chunks.map(content => ({ messages = messages.concat(chunks.map(content => ({
content, content,
username: displayName, username: displayNameShortened,
avatar_url: avatarURL avatar_url: avatarURL
}))) })))
@ -379,7 +411,7 @@ async function eventToMessage(event, guild, di) {
// There needs to be a message to add attachments to. // There needs to be a message to add attachments to.
if (!messages.length) messages.push({ if (!messages.length) messages.push({
content, content,
username: displayName, username: displayNameShortened,
avatar_url: avatarURL avatar_url: avatarURL
}) })
messages[0].attachments = attachments messages[0].attachments = attachments

View file

@ -1436,8 +1436,8 @@ test("event2message: overly long usernames are shifted into the message content"
messagesToDelete: [], messagesToDelete: [],
messagesToEdit: [], messagesToEdit: [],
messagesToSend: [{ messagesToSend: [{
username: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTAN", username: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS",
content: "**T and I DON'T MATTER**: testing the member state cache", content: "**IMPORTANT and I DON'T MATTER**\ntesting the member state cache",
avatar_url: undefined avatar_url: undefined
}] }]
} }
@ -1468,7 +1468,7 @@ test("event2message: overly long usernames are not treated specially when the ms
messagesToDelete: [], messagesToDelete: [],
messagesToEdit: [], messagesToEdit: [],
messagesToSend: [{ messagesToSend: [{
username: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTAN", username: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS",
content: "\\* I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTANT and I DON'T MATTER looks at the start of the message", content: "\\* I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTANT and I DON'T MATTER looks at the start of the message",
avatar_url: undefined avatar_url: undefined
}] }]