From 22dde9faf7bf2f88eab06164d66fd34dd106f8ae Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 10 May 2023 22:15:20 +1200 Subject: [PATCH] completed user syncing. it occurs on message send --- d2m/actions/create-room.js | 2 + d2m/actions/register-user.js | 76 ++++++++++++++++++++++++++++++++++-- d2m/actions/send-message.js | 5 +++ matrix/file.js | 11 ++++++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/d2m/actions/create-room.js b/d2m/actions/create-room.js index 7f2c799..96a9671 100644 --- a/d2m/actions/create-room.js +++ b/d2m/actions/create-room.js @@ -144,6 +144,8 @@ async function _syncRoom(channelID, shouldActuallySync) { return existing // only need to ensure room exists, and it does. return the room ID } + console.log(`[room sync] to matrix: ${channel.name}`) + const {spaceID, channelKState} = await channelToKState(channel, guild) // sync channel state to room diff --git a/d2m/actions/register-user.js b/d2m/actions/register-user.js index 89bac2c..cc5f515 100644 --- a/d2m/actions/register-user.js +++ b/d2m/actions/register-user.js @@ -80,13 +80,81 @@ async function ensureSimJoined(user, roomID) { /** * @param {import("discord-api-types/v10").APIUser} user - * @param {Required>} member + * @param {Omit} member */ -async function memberToStateContent(user, member) { - return { - displayname: member.nick || user.username +async function memberToStateContent(user, member, guildID) { + let displayname = user.username + if (member.nick && member.nick !== displayname) displayname = member.nick + " | " + displayname // prepend nick if present + + const content = { + displayname, + membership: "join", + "moe.cadence.ooye.member": { + }, + "uk.half-shot.discord.member": { + bot: !!user.bot, + displayColor: user.accent_color, + id: user.id, + username: user.discriminator.length === 4 ? `${user.username}#${user.discriminator}` : `@${user.username}` + } + } + + if (member.avatar || user.avatar) { + // const avatarPath = file.userAvatar(user) // the user avatar only + const avatarPath = file.memberAvatar(guildID, user, member) // the member avatar or the user avatar + content["moe.cadence.ooye.member"].avatar = avatarPath + content.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) + } + + return content +} + +function calculateProfileEventContentHash(content) { + return `${content.displayname}\u0000${content.avatar_url}` +} + +/** + * @param {import("discord-api-types/v10").APIUser} user + * @param {Omit} member + */ +async function syncUser(user, member, guildID, roomID) { + const mxid = await ensureSimJoined(user, roomID) + const content = await memberToStateContent(user, member, guildID) + const profileEventContentHash = calculateProfileEventContentHash(content) + const existingHash = db.prepare("SELECT profile_event_content_hash FROM sim_member WHERE room_id = ? AND mxid = ?").pluck().get(roomID, mxid) + // only do the actual sync if the hash has changed since we last looked + if (existingHash !== profileEventContentHash) { + await api.sendState(roomID, "m.room.member", mxid, content, mxid) + db.prepare("UPDATE sim_member SET profile_event_content_hash = ? WHERE room_id = ? AND mxid = ?").run(profileEventContentHash, roomID, mxid) + } +} + +async function syncAllUsersInRoom(roomID) { + const mxids = db.prepare("SELECT mxid FROM sim_member WHERE room_id = ?").pluck().all(roomID) + + const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(roomID) + assert.ok(typeof channelID === "string") + /** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */ + const channel = discord.channels.get(channelID) + const guildID = channel.guild_id + assert.ok(typeof guildID === "string") + + for (const mxid of mxids) { + const userID = db.prepare("SELECT discord_id FROM sim WHERE mxid = ?").pluck().get(mxid) + assert.ok(typeof userID === "string") + + /** @ts-ignore @type {Required} */ + const member = await discord.snow.guild.getGuildMember(guildID, userID) + /** @ts-ignore @type {Required} user */ + const user = member.user + assert.ok(user) + + console.log(`[user sync] to matrix: ${user.username} in ${channel.name}`) + await syncUser(user, member, guildID, roomID) } } module.exports.ensureSim = ensureSim module.exports.ensureSimJoined = ensureSimJoined +module.exports.syncUser = syncUser +module.exports.syncAllUsersInRoom = syncAllUsersInRoom diff --git a/d2m/actions/send-message.js b/d2m/actions/send-message.js index fb181d2..2630430 100644 --- a/d2m/actions/send-message.js +++ b/d2m/actions/send-message.js @@ -1,5 +1,7 @@ // @ts-check +const assert = require("assert") + const passthrough = require("../../passthrough") const { discord, sync, db } = passthrough /** @type {import("../converters/message-to-event")} */ @@ -15,11 +17,14 @@ const createRoom = sync.require("../actions/create-room") * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message */ async function sendMessage(message) { + assert.ok(message.member) + const event = messageToEvent.messageToEvent(message) const roomID = await createRoom.ensureRoom(message.channel_id) let senderMxid = null if (!message.webhook_id) { senderMxid = await registerUser.ensureSimJoined(message.author, roomID) + await registerUser.syncUser(message.author, message.member, message.guild_id, roomID) } const eventID = await api.sendEvent(roomID, "m.room.message", event, senderMxid) db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(eventID, message.id, 0) // 0 is primary, 1 is supporting diff --git a/matrix/file.js b/matrix/file.js index 137a096..077d527 100644 --- a/matrix/file.js +++ b/matrix/file.js @@ -58,5 +58,16 @@ function guildIcon(guild) { return `/icons/${guild.id}/${guild.icon}.png?size=${IMAGE_SIZE}` } +function userAvatar(user) { + return `/avatars/${user.id}/${user.avatar}.png?size=${IMAGE_SIZE}` +} + +function memberAvatar(guildID, user, member) { + if (!member.avatar) return userAvatar(user) + return `/guilds/${guildID}/users/${user.id}/avatars/${member.avatar}.png?size=${IMAGE_SIZE}` +} + module.exports.guildIcon = guildIcon +module.exports.userAvatar = userAvatar +module.exports.memberAvatar = memberAvatar module.exports.uploadDiscordFileToMxc = uploadDiscordFileToMxc