From 98477dc0f626a5e5407d9122cec64e0f4b790112 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 1 Feb 2024 22:22:48 +1300 Subject: [PATCH] PK mentions now include member name --- d2m/actions/register-pk-user.js | 2 +- db/migrations/0010-add-sim-proxy.sql | 1 + db/orm-defs.d.ts | 1 + m2d/converters/event-to-message.js | 18 +++++++++++++----- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/d2m/actions/register-pk-user.js b/d2m/actions/register-pk-user.js index af3f8c9..ca47b7c 100644 --- a/d2m/actions/register-pk-user.js +++ b/d2m/actions/register-pk-user.js @@ -128,7 +128,7 @@ async function memberToStateContent(pkMessage, author) { async function syncUser(author, pkMessage, roomID) { const mxid = await ensureSimJoined(pkMessage, roomID) // Update the sim_proxy table, so mentions can look up the original sender later - db.prepare("INSERT OR IGNORE INTO sim_proxy (user_id, proxy_owner_id) VALUES (?, ?)").run(pkMessage.member.uuid, pkMessage.sender) + db.prepare("INSERT OR IGNORE INTO sim_proxy (user_id, proxy_owner_id, displayname) VALUES (?, ?, ?)").run(pkMessage.member.uuid, pkMessage.sender, author.username) // Sync the member state const content = await memberToStateContent(pkMessage, author) const currentHash = registerUser._hashProfileContent(content) diff --git a/db/migrations/0010-add-sim-proxy.sql b/db/migrations/0010-add-sim-proxy.sql index d7dd0a1..159bdff 100644 --- a/db/migrations/0010-add-sim-proxy.sql +++ b/db/migrations/0010-add-sim-proxy.sql @@ -1,5 +1,6 @@ CREATE TABLE IF NOT EXISTS sim_proxy ( user_id TEXT NOT NULL, proxy_owner_id TEXT NOT NULL, + displayname TEXT NOT NULL, PRIMARY KEY(user_id) ) WITHOUT ROWID; diff --git a/db/orm-defs.d.ts b/db/orm-defs.d.ts index 35fea51..622e1a0 100644 --- a/db/orm-defs.d.ts +++ b/db/orm-defs.d.ts @@ -66,6 +66,7 @@ export type Models = { sim_proxy: { user_id: string proxy_owner_id: string + displayname: string } webhook: { diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index a48fa75..e48b5f3 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -32,7 +32,7 @@ const markdownEscapes = [ [/^>/g, '\\>'], [/_/g, '\\_'], [/^(\d+)\. /g, '$1\\. '] - ] +] const turndownService = new TurndownService({ hr: "----", @@ -103,7 +103,15 @@ turndownService.addRule("inlineLink", { }, replacement: function (content, node) { - if (node.getAttribute("data-user-id")) return `<@${node.getAttribute("data-user-id")}>` + if (node.getAttribute("data-user-id")) { + const user_id = node.getAttribute("data-user-id") + const row = select("sim_proxy", ["displayname", "proxy_owner_id"], {user_id}).get() + if (row) { + return `**@${row.displayname}** (<@${row.proxy_owner_id}>)` + } else { + return `<@${user_id}>` + } + } if (node.getAttribute("data-message-id")) return `https://discord.com/channels/${node.getAttribute("data-guild-id")}/${node.getAttribute("data-channel-id")}/${node.getAttribute("data-message-id")}` if (node.getAttribute("data-channel-id")) return `<#${node.getAttribute("data-channel-id")}>` const href = node.getAttribute("href") @@ -507,9 +515,9 @@ async function eventToMessage(event, guild, di) { mxid = decodeURIComponent(mxid) if (mxUtils.eventSenderIsFromDiscord(mxid)) { // Handle mention of an OOYE sim user by their mxid - const userID = getUserOrProxyOwnerID(mxid) - if (!userID) return whole - return `${attributeValue} data-user-id="${userID}">` + const id = select("sim", "user_id", {mxid}).pluck().get() + if (!id) return whole + return `${attributeValue} data-user-id="${id}">` } else { // Handle mention of a Matrix user by their mxid // Check if this Matrix user is actually the sim user from another old bridge in the room?