1
0
Fork 0

m->d: Fix reply previews saying undefined

This commit is contained in:
Cadence Ember 2024-02-14 11:04:54 +13:00
parent ae4e299cf3
commit 369e0862e5
3 changed files with 130 additions and 12 deletions

View file

@ -522,17 +522,40 @@ async function eventToMessage(event, guild, di) {
} else if (repliedToEvent.unsigned?.redacted_because) { } else if (repliedToEvent.unsigned?.redacted_because) {
contentPreview = " (in reply to a deleted message)" contentPreview = " (in reply to a deleted message)"
} else { } else {
const repliedToContent = repliedToEvent.content.formatted_body || repliedToEvent.content.body // Generate a reply preview for a standard message
const contentPreviewChunks = chunk( /** @type {string} */
entities.decodeHTML5Strict( // Remove entities like & " let repliedToContent = repliedToEvent.content.formatted_body || repliedToEvent.content.body
repliedToContent.replace(/.*<\/mx-reply>/s, "") // Remove everything before replies, so just use the actual message body repliedToContent = repliedToContent.replace(/.*<\/mx-reply>/s, "") // Remove everything before replies, so just use the actual message body
.replace(/^\s*<blockquote>.*?<\/blockquote>(.....)/s, "$1") // If the message starts with a blockquote, don't count it and use the message body afterwards repliedToContent = repliedToContent.replace(/^\s*<blockquote>.*?<\/blockquote>(.....)/s, "$1") // If the message starts with a blockquote, don't count it and use the message body afterwards
.replace(/(?:\n|<br>)+/g, " ") // Should all be on one line repliedToContent = repliedToContent.replace(/(?:\n|<br>)+/g, " ") // Should all be on one line
.replace(/<span [^>]*data-mx-spoiler\b[^>]*>.*?<\/span>/g, "[spoiler]") // Good enough method of removing spoiler content. (I don't want to break out the HTML parser unless I have to.) repliedToContent = repliedToContent.replace(/<span [^>]*data-mx-spoiler\b[^>]*>.*?<\/span>/g, "[spoiler]") // Good enough method of removing spoiler content. (I don't want to break out the HTML parser unless I have to.)
.replace(/<[^>]+>/g, "") // Completely strip all HTML tags and formatting. repliedToContent = repliedToContent.replace(/<img([^>]*)>/g, (_, att) => { // Convert Matrix emoji images into Discord emoji markdown
), 50) if (!att.includes("data-mx-emoticon")) return ""
// Try to get the equivalent Discord emoji, if there is a src and if we know about it
const mxcUrlMatch = att.match(/\bsrc="(mxc:\/\/[^"]+)"/)
if (mxcUrlMatch) {
const row = select("emoji", ["emoji_id", "name", "animated"], {mxc_url: mxcUrlMatch[1]}).get()
if (row) {
const animatedChar = row.animated ? "a" : ""
return `<${animatedChar}:${row.name}:${row.emoji_id}>`
}
}
// Emoji is unknown or inaccessible, try substituting the title text instead
const titleTextMatch = att.match(/\btitle=":?([^:"]+)/)
if (titleTextMatch) return `:${titleTextMatch[1]}:`
// Otherwise we can't use the emoji.
return ""
})
repliedToContent = repliedToContent.replace(/<[^:>][^>]*>/g, "") // Completely strip all HTML tags and formatting.
repliedToContent = entities.decodeHTML5Strict(repliedToContent) // Remove entities like &amp; &quot;
const contentPreviewChunks = chunk(repliedToContent, 50)
if (contentPreviewChunks.length) {
contentPreview = ":\n> " + contentPreviewChunks[0] contentPreview = ":\n> " + contentPreviewChunks[0]
if (contentPreviewChunks.length > 1) contentPreview = contentPreview.replace(/[,.']$/, "") + "..." if (contentPreviewChunks.length > 1) contentPreview = contentPreview.replace(/[,.']$/, "") + "..."
} else {
console.log("Unable to generate reply preview for this replied-to event because we stripped all of it:", repliedToEvent)
contentPreview = ""
}
} }
replyLine = `> ${replyLine}${contentPreview}\n` replyLine = `> ${replyLine}${contentPreview}\n`
})() })()

View file

@ -1292,6 +1292,100 @@ test("event2message: entities are not escaped in main message or reply preview",
) )
}) })
test("event2message: reply preview converts emoji formatting when replying to a known custom emoji", async t => {
t.deepEqual(
await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "> <@cadence:cadence.moe> :hippo:\n\nreply",
format: "org.matrix.custom.html",
formatted_body: "<mx-reply><blockquote><a href=\"https://matrix.to/#/!TqlyQmifxGUggEmdBN:cadence.moe/$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs?via=cadence.moe&via=matrix.org&via=conduit.rory.gay\">In reply to</a> <a href=\"https://matrix.to/#/@cadence:cadence.moe\">@cadence:cadence.moe</a><br><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC\" title=\":hippo:\" alt=\":hippo:\" /></blockquote></mx-reply>reply",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs"
}
}
},
event_id: "$bCMLaLiMfoRajaGTgzaxAci-g8hJfkspVJIKwYktnvc",
room_id: "!TqlyQmifxGUggEmdBN:cadence.moe"
}, data.guild.general, {
api: {
getEvent: mockGetEvent(t, "!TqlyQmifxGUggEmdBN:cadence.moe", "$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs", {
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: ":hippo:",
format: "org.matrix.custom.html",
formatted_body: "<img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC\" title=\":hippo:\" alt=\":hippo:\">"
}
})
}
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "> <:L1:1144820033948762203><:L2:1144820084079087647>Ⓜ️**cadence [they]**:"
+ "\n> <:hippo:230201364309868544>"
+ "\nreply",
avatar_url: undefined
}]
}
)
})
test("event2message: reply preview uses emoji title text when replying to an unknown custom emoji", async t => {
t.deepEqual(
await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "> <@cadence:cadence.moe> :hippo:\n\nreply",
format: "org.matrix.custom.html",
formatted_body: "<mx-reply><blockquote><a href=\"https://matrix.to/#/!TqlyQmifxGUggEmdBN:cadence.moe/$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs?via=cadence.moe&via=matrix.org&via=conduit.rory.gay\">In reply to</a> <a href=\"https://matrix.to/#/@cadence:cadence.moe\">@cadence:cadence.moe</a><br><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AHKNeXoRlprdULRMalhqfCdj\" title=\":hippo:\" alt=\":hippo:\" /></blockquote></mx-reply>reply",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs"
}
}
},
event_id: "$bCMLaLiMfoRajaGTgzaxAci-g8hJfkspVJIKwYktnvc",
room_id: "!TqlyQmifxGUggEmdBN:cadence.moe"
}, data.guild.general, {
api: {
getEvent: mockGetEvent(t, "!TqlyQmifxGUggEmdBN:cadence.moe", "$zmO-dtPO6FubBkDxJZ5YmutPIsG1RgV5JJku-9LeGWs", {
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: ":hippo:",
format: "org.matrix.custom.html",
formatted_body: "<img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AHKNeXoRlprdULRMalhqfCdj\" title=\":hippo:\" alt=\":hippo:\">"
}
})
}
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "> <:L1:1144820033948762203><:L2:1144820084079087647>Ⓜ️**cadence [they]**:"
+ "\n> :hippo:"
+ "\nreply",
avatar_url: undefined
}]
}
)
})
test("event2message: editing a rich reply to a sim user", async t => { test("event2message: editing a rich reply to a sim user", async t => {
const eventsFetched = [] const eventsFetched = []
t.deepEqual( t.deepEqual(

View file

@ -117,7 +117,8 @@ INSERT INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES
('!CzvdIdUQXgUjDVKxeU:cadence.moe', '@cadence:cadence.moe', 'cadence [they]', 'mxc://cadence.moe/azCAhThKTojXSZJRoWwZmhvU'), ('!CzvdIdUQXgUjDVKxeU:cadence.moe', '@cadence:cadence.moe', 'cadence [they]', 'mxc://cadence.moe/azCAhThKTojXSZJRoWwZmhvU'),
('!cBxtVRxDlZvSVhJXVK:cadence.moe', '@Milan:tchncs.de', 'Milan', NULL), ('!cBxtVRxDlZvSVhJXVK:cadence.moe', '@Milan:tchncs.de', 'Milan', NULL),
('!TqlyQmifxGUggEmdBN:cadence.moe', '@ampflower:matrix.org', 'Ampflower 🌺', 'mxc://cadence.moe/PRfhXYBTOalvgQYtmCLeUXko'), ('!TqlyQmifxGUggEmdBN:cadence.moe', '@ampflower:matrix.org', 'Ampflower 🌺', 'mxc://cadence.moe/PRfhXYBTOalvgQYtmCLeUXko'),
('!TqlyQmifxGUggEmdBN:cadence.moe', '@aflower:syndicated.gay', 'Rose', 'mxc://syndicated.gay/ZkBUPXCiXTjdJvONpLJmcbKP'); ('!TqlyQmifxGUggEmdBN:cadence.moe', '@aflower:syndicated.gay', 'Rose', 'mxc://syndicated.gay/ZkBUPXCiXTjdJvONpLJmcbKP'),
('!TqlyQmifxGUggEmdBN:cadence.moe', '@cadence:cadence.moe', 'cadence [they]', NULL);
INSERT INTO lottie (sticker_id, mxc_url) VALUES INSERT INTO lottie (sticker_id, mxc_url) VALUES
('860171525772279849', 'mxc://cadence.moe/ZtvvVbwMIdUZeovWVyGVFCeR'); ('860171525772279849', 'mxc://cadence.moe/ZtvvVbwMIdUZeovWVyGVFCeR');