Compare commits

..

No commits in common. "5bf051c6245b28821f471933214a0d4892beca98" and "8b7c47e3698d4707c6ef06eaf050d3607750ae2c" have entirely different histories.

2 changed files with 6 additions and 38 deletions

View file

@ -158,31 +158,6 @@ 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 {import("discord-api-types/v10").APIGuild} guild
@ -204,13 +179,6 @@ async function eventToMessage(event, guild, di) {
const member = await getMemberFromCacheOrHomeserver(event.room_id, event.sender, di?.api)
if (member.displayname) displayName = member.displayname
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
const attachments = []
@ -396,13 +364,13 @@ async function eventToMessage(event, guild, di) {
pendingFiles.push({name: filename, url})
}
content = displayNameRunoff + replyLine + content
content = replyLine + content
// Split into 2000 character chunks
const chunks = chunk(content, 2000)
messages = messages.concat(chunks.map(content => ({
content,
username: displayNameShortened,
username: displayName,
avatar_url: avatarURL
})))
@ -411,7 +379,7 @@ async function eventToMessage(event, guild, di) {
// There needs to be a message to add attachments to.
if (!messages.length) messages.push({
content,
username: displayNameShortened,
username: displayName,
avatar_url: avatarURL
})
messages[0].attachments = attachments

View file

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