1
0
Fork 0

Coverage for role and emoji converter

This commit is contained in:
Cadence Ember 2023-11-23 15:52:41 +13:00
parent 8439512f1a
commit 7029247461
2 changed files with 16 additions and 5 deletions

View file

@ -52,11 +52,8 @@ function getDiscordParseCallbacks(message, guild, useHTML) {
emoji: node => { emoji: node => {
if (useHTML) { if (useHTML) {
const mxc = select("emoji", "mxc_url", {emoji_id: node.id}).pluck().get() const mxc = select("emoji", "mxc_url", {emoji_id: node.id}).pluck().get()
if (mxc) { assert(mxc) // All emojis should have been added ahead of time in the messageToEvent function.
return `<img data-mx-emoticon height="32" src="${mxc}" title=":${node.name}:" alt=":${node.name}:">` return `<img data-mx-emoticon height="32" src="${mxc}" title=":${node.name}:" alt=":${node.name}:">`
} else { // We shouldn't get here since all emojis should have been added ahead of time in the messageToEvent function.
return `<img src="mxc://cadence.moe/${node.id}" data-mx-emoticon alt=":${node.name}:" title=":${node.name}:" height="24">`
}
} else { } else {
return `:${node.name}:` return `:${node.name}:`
} }
@ -64,7 +61,9 @@ function getDiscordParseCallbacks(message, guild, useHTML) {
role: node => { role: node => {
const role = guild.roles.find(r => r.id === node.id) const role = guild.roles.find(r => r.id === node.id)
if (!role) { if (!role) {
return "@&" + node.id // fallback for if the cache breaks. if this happens, fix discord-packets.js to store the role info. // This fallback should only trigger if somebody manually writes a silly message, or if the cache breaks (hasn't happened yet).
// If the cache breaks, fix discord-packets.js to store role info properly.
return "@&" + node.id
} else if (useHTML && role.color) { } else if (useHTML && role.color) {
return `<font color="#${role.color.toString(16)}">@${role.name}</font>` return `<font color="#${role.color.toString(16)}">@${role.name}</font>`
} else if (useHTML) { } else if (useHTML) {

View file

@ -85,6 +85,18 @@ test("message2event: simple role mentions", async t => {
}]) }])
}) })
test("message2event: manually constructed unknown roles should use fallback", async t => {
const events = await messageToEvent(data.message.unknown_role, data.guild.general, {})
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "I'm just @&4 testing a few role pings <@&B> don't mind me",
format: "org.matrix.custom.html",
formatted_body: "I'm just @&4 testing a few role pings &lt;@&amp;B&gt; don't mind me"
}])
})
test("message2event: simple message link", async t => { test("message2event: simple message link", async t => {
const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {}) const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {})
t.deepEqual(events, [{ t.deepEqual(events, [{