here's what I would have done differently

This commit is contained in:
Cadence Ember 2023-09-10 14:59:31 +12:00
parent 5daef9ae47
commit 5bf051c624

View file

@ -157,6 +157,7 @@ async function getMemberFromCacheOrHomeserver(roomID, mxid, api) {
return {displayname: null, avatar_url: null} return {displayname: null, avatar_url: null}
}) })
} }
/** /**
* Splits a display name into one chunk containing <=80 characters, and another chunk containing the rest of the characters. Splits on * Splits a display name into one chunk containing <=80 characters, and another chunk containing the rest of the characters. Splits on
* whitespace if possible. * whitespace if possible.
@ -165,7 +166,7 @@ async function getMemberFromCacheOrHomeserver(roomID, mxid, api) {
* can be prepended to the message content as-is. * 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. * @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. * @param {string} displayName - The Matrix side display name to chop up.
* @returns {[string, string]} The afformentioned chunks in their respective order. * @returns {[string, string]} [shortened display name, display name runoff]
*/ */
function splitDisplayName(displayName) { function splitDisplayName(displayName) {
/** @type {string[]} */ /** @type {string[]} */
@ -174,13 +175,10 @@ function splitDisplayName(displayName) {
if (displayNameChunks.length === 1) { if (displayNameChunks.length === 1) {
return [displayName, ""] return [displayName, ""]
} else { } else {
/** @type {string} */
const displayNamePreRunoff = displayNameChunks[0] const displayNamePreRunoff = displayNameChunks[0]
// displayNameRunoff is a substring rather than a concatenation of the rest of the chunks in order to preserve whatever whitespace // 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.
// it was broken on. const displayNameRunoff = `**${displayName.slice(displayNamePreRunoff.length + 1)}**\n`
/** @type {string} */
const displayNameRunoff = `**${displayName.substring(displayNamePreRunoff.length + 1)}**\n`
return [displayNamePreRunoff, displayNameRunoff] return [displayNamePreRunoff, displayNameRunoff]
} }
} }
@ -206,11 +204,9 @@ 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), put the excess characters into displayNameRunoff, later to // If the display name is too long to be put into the webhook (80 characters is the maximum),
// be put at the top of the message // put the excess characters into displayNameRunoff, later to be put at the top of the message
/** @type {string} */ let [displayNameShortened, displayNameRunoff] = splitDisplayName(displayName)
let displayNameShortened, displayNameRunoff
[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 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") { if (event.type === "m.room.message" && event.content.msgtype === "m.emote") {
displayNameRunoff = "" displayNameRunoff = ""
@ -294,7 +290,7 @@ async function eventToMessage(event, guild, di) {
if (fileReplyContentAlternative) { if (fileReplyContentAlternative) {
contentPreview = " " + fileReplyContentAlternative contentPreview = " " + fileReplyContentAlternative
} else { } else {
const repliedToContent = repliedToEvent.content.formatted_body || repliedToEvent.content.body const repliedToContent = repliedToEvent.content.formatted_body || repliedToEvent.content.body
const contentPreviewChunks = chunk( const contentPreviewChunks = chunk(
repliedToContent.replace(/.*<\/mx-reply>/, "") // Remove everything before replies, so just use the actual message body repliedToContent.replace(/.*<\/mx-reply>/, "") // Remove everything before replies, so just use the actual message body
.replace(/.*?<\/blockquote>/, "") // If the message starts with a blockquote, don't count it and use the message body afterwards .replace(/.*?<\/blockquote>/, "") // If the message starts with a blockquote, don't count it and use the message body afterwards