d->m: Make role mentions really pretty

This commit is contained in:
Cadence Ember 2023-10-14 02:15:21 +13:00
parent 44f90cbb5f
commit d9d672bffd
3 changed files with 109 additions and 8 deletions

View file

@ -17,7 +17,12 @@ const reg = require("../../matrix/read-registration")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
function getDiscordParseCallbacks(message, useHTML) {
/**
* @param {DiscordTypes.APIMessage} message
* @param {DiscordTypes.APIGuild} guild
* @param {boolean} useHTML
*/
function getDiscordParseCallbacks(message, guild, useHTML) {
return {
/** @param {{id: string, type: "discordUser"}} node */
user: node => {
@ -53,8 +58,18 @@ function getDiscordParseCallbacks(message, useHTML) {
return `:${node.name}:`
}
},
role: node =>
"@&" + node.id,
role: node => {
const role = guild.roles.find(r => r.id === node.id)
if (!role) {
return "@&" + node.id // fallback for if the cache breaks. if this happens, fix discord-packets.js to store the role info.
} else if (useHTML && role.color) {
return `<font color="#${role.color.toString(16)}">@${role.name}</font>`
} else if (useHTML) {
return `<span data-mx-color="#ffffff" data-mx-bg-color="#414eef">@${role.name}</span>`
} else {
return `@${role.name}:`
}
},
everyone: node =>
"@room",
here: node =>
@ -160,11 +175,11 @@ async function messageToEvent(message, guild, options = {}, di) {
}))
let html = markdown.toHTML(content, {
discordCallback: getDiscordParseCallbacks(message, true)
discordCallback: getDiscordParseCallbacks(message, guild, true)
}, null, null)
let body = markdown.toHTML(content, {
discordCallback: getDiscordParseCallbacks(message, false),
discordCallback: getDiscordParseCallbacks(message, guild, false),
discordOnly: true,
escapeHTML: false,
}, null, null)
@ -223,10 +238,10 @@ async function messageToEvent(message, guild, options = {}, di) {
if (repliedToContent == "") repliedToContent = "[Media]"
else if (!repliedToContent) repliedToContent = "[Replied-to message content wasn't provided by Discord]"
const repliedToHtml = markdown.toHTML(repliedToContent, {
discordCallback: getDiscordParseCallbacks(message, true)
discordCallback: getDiscordParseCallbacks(message, guild, true)
}, null, null)
const repliedToBody = markdown.toHTML(repliedToContent, {
discordCallback: getDiscordParseCallbacks(message, false),
discordCallback: getDiscordParseCallbacks(message, guild, false),
discordOnly: true,
escapeHTML: false,
}, null, null)

View file

@ -73,6 +73,18 @@ test("message2event: simple room mention", async t => {
}])
})
test("message2event: simple role mentions", async t => {
const events = await messageToEvent(data.message.simple_role_mentions, data.guild.general, {})
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "I'm just @!!DLCS!!: testing a few role pings @Master Wonder Mage: don't mind me",
format: "org.matrix.custom.html",
formatted_body: `I'm just <font color="#a901ff">@!!DLCS!!</font> testing a few role pings <span data-mx-color="#ffffff" data-mx-bg-color="#414eef">@Master Wonder Mage</span> don't mind me`
}])
})
test("message2event: simple message link", async t => {
const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {})
t.deepEqual(events, [{

View file

@ -119,7 +119,51 @@ module.exports = {
}
],
premium_subscription_count: 14,
roles: [],
roles: [
{
version: 1696964862461,
unicode_emoji: null,
tags: {},
position: 22,
permissions: '0',
name: 'Master Wonder Mage',
mentionable: true,
managed: false,
id: '503685967463448616',
icon: null,
hoist: false,
flags: 0,
color: 0
}, {
version: 1696964862776,
unicode_emoji: null,
tags: {},
position: 131,
permissions: '0',
name: '!!DLCS!!',
mentionable: true,
managed: false,
id: '212762309364285440',
icon: null,
hoist: true,
flags: 0,
color: 11076095
}, {
version: 1696964862698,
unicode_emoji: '🍂',
tags: {},
position: 102,
permissions: '0',
name: 'corporate overlord',
mentionable: false,
managed: false,
id: '217013981053845504',
icon: null,
hoist: true,
flags: 0,
color: 16745267
}
],
discovery_splash: null,
default_message_notifications: 1,
region: "deprecated",
@ -434,6 +478,36 @@ module.exports = {
attachments: [],
guild_id: "112760669178241024"
},
simple_role_mentions: {
id: "1162374402785153106",
type: 0,
content: "I'm just <@&212762309364285440> testing a few role pings <@&503685967463448616> don't mind me",
channel_id: "160197704226439168",
author: {
id: "772659086046658620",
username: "cadence.worm",
avatar: "4b5c4b28051144e4c111f0113a0f1cf1",
discriminator: "0",
public_flags: 0,
flags: 0,
banner: null,
accent_color: null,
global_name: "cadence",
avatar_decoration_data: null,
banner_color: null
},
attachments: [],
embeds: [],
mentions: [],
mention_roles: [ "212762309364285440", "503685967463448616" ],
pinned: false,
mention_everyone: false,
tts: false,
timestamp: "2023-10-13T13:00:53.496000+00:00",
edited_timestamp: null,
flags: 0,
components: []
},
simple_message_link: {
id: "1126788210308161626",
type: 0,