From d7ad37e4079fe1f2f37cc1e430ddfe7094fc058e Mon Sep 17 00:00:00 2001 From: mschae23 Date: Fri, 6 Nov 2026 20:30:25 +0100 Subject: [PATCH 1/7] assign role colors to d2m users --- src/d2m/actions/register-user.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index 3427bbd..759c8ff 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -113,8 +113,9 @@ async function userToGlobalProfile(user) { /** * @param {DiscordTypes.APIUser} user * @param {Omit | undefined} member + * @param {DiscordTypes.APIGuild} guild */ -async function memberToStateContent(user, member, guildID) { +async function memberToStateContent(user, member, guild) { let displayname = user.username if (user.global_name) displayname = user.global_name if (member?.nick) displayname = member.nick @@ -134,11 +135,20 @@ async function memberToStateContent(user, member, guildID) { 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 + const avatarPath = file.memberAvatar(guild.id, user, member) // the member avatar or the user avatar content["moe.cadence.ooye.member"].avatar = avatarPath content.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) } + const colorRole = member ? guild.roles.find(role => role.colors.primary_color && member.roles.includes(role.id)) : undefined + if (colorRole) { + const encoded = `#${colorRole.colors.primary_color.toString(16).padStart(6, "0")}` + content["eu.she-a.color"] = { + "on_dark": encoded, + "on_light": encoded + } + } + return content } @@ -219,7 +229,7 @@ function _hashProfileContent(content, powerLevel) { */ async function syncUser(user, member, channel, guild, roomID, interactionMetadata) { const mxid = await ensureSimJoined(user, roomID) - const content = await memberToStateContent(user, member, guild.id) + const content = await memberToStateContent(user, member, guild) const powerLevel = memberToPowerLevel(user, member, guild, channel) await _sendSyncUser(roomID, mxid, content, powerLevel, { // do not overwrite pre-existing data if we already have data and `member` is not accessible, because this would replace good data with bad data -- 2.34.1 From 1e717a1e59332584cfbf3b04cf12f34b800598e1 Mon Sep 17 00:00:00 2001 From: mschae23 Date: Sat, 7 Nov 2026 00:01:17 +0100 Subject: [PATCH 2/7] also write accent color to profile --- src/d2m/actions/register-user.js | 28 ++++++++++++++++++---------- src/d2m/discord-packets.js | 2 ++ src/matrix/api.js | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index 759c8ff..cfa4ea1 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -107,6 +107,10 @@ async function userToGlobalProfile(user) { globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) } + if (user.accent_color) { + globalProfile.name_color = `#${user.accent_color.toString(16).padStart(6, "0")}` + } + return globalProfile } @@ -140,14 +144,14 @@ async function memberToStateContent(user, member, guild) { content.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) } - const colorRole = member ? guild.roles.find(role => role.colors.primary_color && member.roles.includes(role.id)) : undefined - if (colorRole) { - const encoded = `#${colorRole.colors.primary_color.toString(16).padStart(6, "0")}` - content["eu.she-a.color"] = { - "on_dark": encoded, - "on_light": encoded - } - } + const colorRole = member ? guild.roles.find(role => role.colors.primary_color && member.roles.includes(role.id)) : undefined + if (colorRole) { + const encoded = `#${colorRole.colors.primary_color.toString(16).padStart(6, "0")}` + content["eu.she-a.color"] = { + "on_dark": encoded, + "on_light": encoded + } + } return content } @@ -248,9 +252,9 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat /** * @param {string} roomID * @param {string} mxid - * @param {{displayname: string, avatar_url?: string}} content + * @param {{displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}} content * @param {number | null} powerLevel - * @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string}}} [options] + * @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string, name_color?: string}}} [options] */ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { const currentHash = _hashProfileContent(content, powerLevel ?? 0) @@ -272,6 +276,10 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { if (await supportsMsc4069) { actions.push(api.profileSetDisplayname(mxid, options?.globalProfile?.displayname || content.displayname, true)) actions.push(api.profileSetAvatarUrl(mxid, options?.globalProfile?.avatar_url || content.avatar_url, true)) + if (options?.globalProfile?.name_color || content["eu.she-a.color"]?.on_light) { + // @ts-ignore + actions.push(api.profileSetNameColor(mxid, options?.globalProfile?.name_color ?? content["eu.she-a.color"]?.on_light, true)) + } } await Promise.all(actions) // Update cached hash diff --git a/src/d2m/discord-packets.js b/src/d2m/discord-packets.js index 8317e0c..500dec5 100644 --- a/src/d2m/discord-packets.js +++ b/src/d2m/discord-packets.js @@ -156,6 +156,8 @@ async function onPacket(client, message, listen) { // Role doesn't already exist. guild.roles.push(...newRoles) } + // lower position values mean lower priority (`@everyone` has position 0) + guild.roles.sort((a, b) => b.position - a.position) } } else if (message.t === "GUILD_MEMBER_UPDATE") { diff --git a/src/matrix/api.js b/src/matrix/api.js index 9b667fc..d5403f9 100644 --- a/src/matrix/api.js +++ b/src/matrix/api.js @@ -449,6 +449,20 @@ async function profileSetAvatarUrl(mxid, avatar_url, inhibitPropagate) { } } +/** + * @param {string} mxid + * @param {string} color + * @param {boolean} [inhibitPropagate] + */ +async function profileSetNameColor(mxid, color, inhibitPropagate) { + const params = {} + if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false + await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/eu.she-a.color`, mxid, params), { + "on_light": color, + "on_dark": color + }) +} + async function ping() { // not using mreq so that we can read the status code const res = await fetch(mreq.baseUrl + route`/client/v1/appservice/${reg.id}/ping`, { @@ -673,6 +687,7 @@ module.exports.redactEvent = redactEvent module.exports.sendTyping = sendTyping module.exports.profileSetDisplayname = profileSetDisplayname module.exports.profileSetAvatarUrl = profileSetAvatarUrl +module.exports.profileSetNameColor = profileSetNameColor module.exports.ping = ping module.exports.previewUrlIfEnabled = previewUrlIfEnabled module.exports.getMedia = getMedia -- 2.34.1 From 510fd5aa3d2fed54dfdfd985a763f719395e69a7 Mon Sep 17 00:00:00 2001 From: mschae23 Date: Sat, 7 Nov 2026 14:12:58 +0100 Subject: [PATCH 3/7] make better use of generic profile, add support for MSC 4466 --- scripts/setup.js | 4 +-- src/d2m/actions/register-user.js | 34 ++++++++++++------- src/matrix/api.js | 58 +++++++++++++------------------- 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/scripts/setup.js b/scripts/setup.js index 191ca3e..e79f2bb 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -364,8 +364,8 @@ function defineEchoHandler() { // set profile data on homeserver... console.log("⏩ Updating Matrix profile... (If you've joined lots of rooms, this is slow. Please allow at least 30 seconds.)") - await api.profileSetDisplayname(mxid, "Out Of Your Element (bot)") - await api.profileSetAvatarUrl(mxid, avatarUrl) + await api.profileSetKey(mxid, "displayname", "Out Of Your Element (bot)", "all") + await api.profileSetKey(mxid, "avatar_url", avatarUrl, "all") console.log("✅ Matrix profile updated...") console.log("Good to go. I hope you enjoy Out Of Your Element.") diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index cfa4ea1..4286f43 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -24,8 +24,6 @@ let hasher = null // @ts-ignore require("xxhash-wasm")().then(h => hasher = h) -const supportsMsc4069 = api.versions().then(v => !!v?.unstable_features?.["org.matrix.msc4069"]).catch(() => false) - /** * A sim is an account that is being simulated by the bridge to copy events from the other side. * @param {DiscordTypes.APIUser} user @@ -107,8 +105,12 @@ async function userToGlobalProfile(user) { globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) } - if (user.accent_color) { - globalProfile.name_color = `#${user.accent_color.toString(16).padStart(6, "0")}` + if (user.accent_color !== null && user.accent_color !== undefined) { + const encoded = `#${user.accent_color.toString(16).padStart(6, "0")}` + globalProfile["eu.she-a.color"] = { + on_light: encoded, + on_dark: encoded + } } return globalProfile @@ -254,7 +256,7 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat * @param {string} mxid * @param {{displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}} content * @param {number | null} powerLevel - * @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string, name_color?: string}}} [options] + * @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}}} [options] */ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { const currentHash = _hashProfileContent(content, powerLevel ?? 0) @@ -272,13 +274,22 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { if (powerLevel != null) { actions.push(mxUtils.setUserPower(roomID, mxid, powerLevel, api)) } + + const support = await api.versions().then(v => { return { + msc4069: !!v?.unstable_features?.["org.matrix.msc4069"], + msc4466: !!v?.unstable_features?.["computer.gingershaped.msc4466"] + }}).catch(() => { return {msc4069: false, msc4466: false}}) + // Update global profile (if supported by server) - if (await supportsMsc4069) { - actions.push(api.profileSetDisplayname(mxid, options?.globalProfile?.displayname || content.displayname, true)) - actions.push(api.profileSetAvatarUrl(mxid, options?.globalProfile?.avatar_url || content.avatar_url, true)) - if (options?.globalProfile?.name_color || content["eu.she-a.color"]?.on_light) { - // @ts-ignore - actions.push(api.profileSetNameColor(mxid, options?.globalProfile?.name_color ?? content["eu.she-a.color"]?.on_light, true)) + if (!!options?.globalProfile && (support.msc4069 || support.msc4466)) { + for (const key of Object.keys(options.globalProfile)) { + actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "unchanged", support)) + } + const oldProfile = await api.getProfile(mxid).catch(e => { return {}}) + for (const key of Object.keys(oldProfile)) { + if (!options.globalProfile[key]) { + actions.push(api.profileDeleteKey(mxid, key, "unchanged", support)) + } } } await Promise.all(actions) @@ -324,4 +335,3 @@ module.exports.syncUser = syncUser module.exports._sendSyncUser = _sendSyncUser module.exports.syncAllUsersInRoom = syncAllUsersInRoom module.exports._memberToPowerLevel = memberToPowerLevel -module.exports.supportsMsc4069 = supportsMsc4069 diff --git a/src/matrix/api.js b/src/matrix/api.js index d5403f9..978afb0 100644 --- a/src/matrix/api.js +++ b/src/matrix/api.js @@ -421,46 +421,35 @@ async function sendTyping(roomID, isTyping, mxid, duration) { /** * @param {string} mxid - * @param {string} displayname - * @param {boolean} [inhibitPropagate] + * @param {string} key + * @param {object} content + * @param {"none" | "unchanged" | "all"} [propagateTo] + * @param {{"msc4466": boolean, "msc4069": boolean}} [support] */ -async function profileSetDisplayname(mxid, displayname, inhibitPropagate) { +async function profileSetKey(mxid, key, content, propagateTo, support) { const params = {} - if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false - await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/displayname`, mxid, params), { - displayname - }) + if (key === "displayname" || key === "avatar_url") { + if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } + if (propagateTo !== "all" && (!support || support["msc4069"])) { params["org.matrix.msc4069.propagate"] = false } + } + const body = {} + body[key] = content + await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params), body) } /** * @param {string} mxid - * @param {string | null | undefined} avatar_url - * @param {boolean} [inhibitPropagate] + * @param {string} key + * @param {"none" | "unchanged" | "all"} [propagateTo] + * @param {{"msc4466": boolean, "msc4069": boolean}} [support] */ -async function profileSetAvatarUrl(mxid, avatar_url, inhibitPropagate) { +async function profileDeleteKey(mxid, key, propagateTo, support) { const params = {} - if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false - if (avatar_url) { - await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/avatar_url`, mxid, params), { - avatar_url - }) - } else { - await mreq.mreq("DELETE", path(route`/client/v3/profile/${mxid}/avatar_url`, mxid, params)) - } -} - -/** - * @param {string} mxid - * @param {string} color - * @param {boolean} [inhibitPropagate] - */ -async function profileSetNameColor(mxid, color, inhibitPropagate) { - const params = {} - if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false - await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/eu.she-a.color`, mxid, params), { - "on_light": color, - "on_dark": color - }) + if (key === "displayname" || key === "avatar_url") { + if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } + if (!support || support["msc4069"]) { params["org.matrix.msc4069.propagate"] = propagateTo === "all" } + } + await mreq.mreq("DELETE", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params)) } async function ping() { @@ -685,9 +674,8 @@ module.exports.sendState = sendState module.exports.sendEvent = sendEvent module.exports.redactEvent = redactEvent module.exports.sendTyping = sendTyping -module.exports.profileSetDisplayname = profileSetDisplayname -module.exports.profileSetAvatarUrl = profileSetAvatarUrl -module.exports.profileSetNameColor = profileSetNameColor +module.exports.profileSetKey = profileSetKey +module.exports.profileDeleteKey = profileDeleteKey module.exports.ping = ping module.exports.previewUrlIfEnabled = previewUrlIfEnabled module.exports.getMedia = getMedia -- 2.34.1 From 98b7615f8b9b5a7ebad7a5c0532da4fcf35b71ad Mon Sep 17 00:00:00 2001 From: mschae23 Date: Sat, 7 Nov 2026 15:26:09 +0100 Subject: [PATCH 4/7] disable profile propagation entirely --- src/d2m/actions/register-user.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index 4286f43..0bc2236 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -283,12 +283,12 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { // Update global profile (if supported by server) if (!!options?.globalProfile && (support.msc4069 || support.msc4466)) { for (const key of Object.keys(options.globalProfile)) { - actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "unchanged", support)) + actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "none", support)) } const oldProfile = await api.getProfile(mxid).catch(e => { return {}}) for (const key of Object.keys(oldProfile)) { if (!options.globalProfile[key]) { - actions.push(api.profileDeleteKey(mxid, key, "unchanged", support)) + actions.push(api.profileDeleteKey(mxid, key, "none", support)) } } } -- 2.34.1 From f59e3f46ab042b21ce478265dc2a22b6c2e4d319 Mon Sep 17 00:00:00 2001 From: mschae23 Date: Sat, 7 Nov 2026 17:26:27 +0100 Subject: [PATCH 5/7] fetch user from Discord to get accent color --- src/d2m/actions/register-pk-user.js | 2 +- src/d2m/actions/register-plu-ral-user.js | 2 +- src/d2m/actions/register-user.js | 29 +++++++++++++++--------- src/d2m/actions/register-webhook-user.js | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/d2m/actions/register-pk-user.js b/src/d2m/actions/register-pk-user.js index 281bfcb..205642a 100644 --- a/src/d2m/actions/register-pk-user.js +++ b/src/d2m/actions/register-pk-user.js @@ -161,7 +161,7 @@ async function syncUser(messageID, author, roomID, shouldActuallySync) { if (shouldActuallySync) { // Build current profile data and sync if the hash has changed const content = await memberToStateContent(pkMessage, author) - await registerUser._sendSyncUser(roomID, mxid, content, null) + await registerUser._sendSyncUser(roomID, mxid, null, content, null) } return mxid diff --git a/src/d2m/actions/register-plu-ral-user.js b/src/d2m/actions/register-plu-ral-user.js index 4e1d347..81a1e27 100644 --- a/src/d2m/actions/register-plu-ral-user.js +++ b/src/d2m/actions/register-plu-ral-user.js @@ -161,7 +161,7 @@ async function syncUser(channelID, messageID, author, roomID, shouldActuallySync if (shouldActuallySync) { // Build current profile data and sync if the hash has changed const content = await memberToStateContent(pluRalMessage, author) - await registerUser._sendSyncUser(roomID, mxid, content, null) + await registerUser._sendSyncUser(roomID, mxid, null, content, null) } return mxid diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index 0bc2236..6cb5bd7 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -237,10 +237,10 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat const mxid = await ensureSimJoined(user, roomID) const content = await memberToStateContent(user, member, guild) const powerLevel = memberToPowerLevel(user, member, guild, channel) - await _sendSyncUser(roomID, mxid, content, powerLevel, { + await _sendSyncUser(roomID, mxid, user, content, powerLevel, { // do not overwrite pre-existing data if we already have data and `member` is not accessible, because this would replace good data with bad data allowOverwrite: !!member, - globalProfile: await userToGlobalProfile(user) + updateGlobalProfile: true, }) const appInstalledByUser = user.bot && interactionMetadata?.authorizing_integration_owners?.[DiscordTypes.ApplicationIntegrationType.UserInstall] @@ -254,11 +254,12 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat /** * @param {string} roomID * @param {string} mxid + * @param {DiscordTypes.APIUser | null} user * @param {{displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}} content * @param {number | null} powerLevel - * @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}}} [options] + * @param {{allowOverwrite?: boolean, updateGlobalProfile?: boolean}} [options] */ -async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { +async function _sendSyncUser(roomID, mxid, user, content, powerLevel, options) { const currentHash = _hashProfileContent(content, powerLevel ?? 0) const existingHash = select("sim_member", "hashed_profile_content", {room_id: roomID, mxid}).safeIntegers().pluck().get() // console.log(roomID, mxid, existingHash, currentHash) @@ -281,14 +282,20 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) { }}).catch(() => { return {msc4069: false, msc4466: false}}) // Update global profile (if supported by server) - if (!!options?.globalProfile && (support.msc4069 || support.msc4466)) { - for (const key of Object.keys(options.globalProfile)) { - actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "none", support)) + if (user && options?.updateGlobalProfile && (support.msc4069 || support.msc4466)) { + const needFullProfile = !content["eu.she-a.color"] + const globalProfile = await (needFullProfile ? discord.snow.user.getUser(user.id) : Promise.resolve(user)) + .then(userToGlobalProfile).catch(e => { return {}}) + for (const key of Object.keys(globalProfile)) { + actions.push(api.profileSetKey(mxid, key, globalProfile[key], "none", support)) } - const oldProfile = await api.getProfile(mxid).catch(e => { return {}}) - for (const key of Object.keys(oldProfile)) { - if (!options.globalProfile[key]) { - actions.push(api.profileDeleteKey(mxid, key, "none", support)) + if (needFullProfile) { + // don't delete keys if some profile fields from Discord might be missing + const oldProfile = await api.getProfile(mxid).catch(e => { return {}}) + for (const key of Object.keys(oldProfile)) { + if (!globalProfile[key]) { + actions.push(api.profileDeleteKey(mxid, key, "none", support)) + } } } } diff --git a/src/d2m/actions/register-webhook-user.js b/src/d2m/actions/register-webhook-user.js index 5114a8e..81ee3ca 100644 --- a/src/d2m/actions/register-webhook-user.js +++ b/src/d2m/actions/register-webhook-user.js @@ -120,7 +120,7 @@ async function syncUser(author, roomID, shouldActuallySync) { if (shouldActuallySync) { // Build current profile data and sync if the hash has changed const content = await authorToStateContent(author) - await registerUser._sendSyncUser(roomID, mxid, content, null) + await registerUser._sendSyncUser(roomID, mxid, null, content, null) } return mxid -- 2.34.1 From 025b2e4dcc3b9a29edcf191d314771ec7527ab19 Mon Sep 17 00:00:00 2001 From: mschae23 Date: Sun, 8 Nov 2026 16:51:52 +0100 Subject: [PATCH 6/7] memoize MSC support --- scripts/setup.js | 4 ++-- src/d2m/actions/register-user.js | 16 ++++++-------- src/matrix/api.js | 38 ++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/scripts/setup.js b/scripts/setup.js index e79f2bb..3969705 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -364,8 +364,8 @@ function defineEchoHandler() { // set profile data on homeserver... console.log("⏩ Updating Matrix profile... (If you've joined lots of rooms, this is slow. Please allow at least 30 seconds.)") - await api.profileSetKey(mxid, "displayname", "Out Of Your Element (bot)", "all") - await api.profileSetKey(mxid, "avatar_url", avatarUrl, "all") + await api.setProfileKey(mxid, "displayname", "Out Of Your Element (bot)", "all") + await api.setProfileKey(mxid, "avatar_url", avatarUrl, "all") console.log("✅ Matrix profile updated...") console.log("Good to go. I hope you enjoy Out Of Your Element.") diff --git a/src/d2m/actions/register-user.js b/src/d2m/actions/register-user.js index 6cb5bd7..a31bba8 100644 --- a/src/d2m/actions/register-user.js +++ b/src/d2m/actions/register-user.js @@ -276,25 +276,23 @@ async function _sendSyncUser(roomID, mxid, user, content, powerLevel, options) { actions.push(mxUtils.setUserPower(roomID, mxid, powerLevel, api)) } - const support = await api.versions().then(v => { return { - msc4069: !!v?.unstable_features?.["org.matrix.msc4069"], - msc4466: !!v?.unstable_features?.["computer.gingershaped.msc4466"] - }}).catch(() => { return {msc4069: false, msc4466: false}}) - // Update global profile (if supported by server) + const support = await api.mscSupport if (user && options?.updateGlobalProfile && (support.msc4069 || support.msc4466)) { + // When the user has a role color, don't update the profile accent color because it won't change the message's appearance. + // Since user.accent_color is (currently) the only thing the full profile is needed for, skip the API call in that case. const needFullProfile = !content["eu.she-a.color"] const globalProfile = await (needFullProfile ? discord.snow.user.getUser(user.id) : Promise.resolve(user)) - .then(userToGlobalProfile).catch(e => { return {}}) + .then(userToGlobalProfile).catch(e => ({})) for (const key of Object.keys(globalProfile)) { - actions.push(api.profileSetKey(mxid, key, globalProfile[key], "none", support)) + actions.push(api.setProfileKey(mxid, key, globalProfile[key], "none")) } if (needFullProfile) { // don't delete keys if some profile fields from Discord might be missing - const oldProfile = await api.getProfile(mxid).catch(e => { return {}}) + const oldProfile = await api.getProfile(mxid).catch(e => ({})) for (const key of Object.keys(oldProfile)) { if (!globalProfile[key]) { - actions.push(api.profileDeleteKey(mxid, key, "none", support)) + actions.push(api.deleteProfileKey(mxid, key, "none")) } } } diff --git a/src/matrix/api.js b/src/matrix/api.js index 978afb0..6813454 100644 --- a/src/matrix/api.js +++ b/src/matrix/api.js @@ -424,14 +424,14 @@ async function sendTyping(roomID, isTyping, mxid, duration) { * @param {string} key * @param {object} content * @param {"none" | "unchanged" | "all"} [propagateTo] - * @param {{"msc4466": boolean, "msc4069": boolean}} [support] */ -async function profileSetKey(mxid, key, content, propagateTo, support) { +async function setProfileKey(mxid, key, content, propagateTo) { const params = {} if (key === "displayname" || key === "avatar_url") { - if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } - if (propagateTo !== "all" && (!support || support["msc4069"])) { params["org.matrix.msc4069.propagate"] = false } - } + const support = await mscSupport + if (support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } + if (propagateTo !== "all" && support["msc4069"]) { params["org.matrix.msc4069.propagate"] = false } + } const body = {} body[key] = content await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params), body) @@ -441,14 +441,14 @@ async function profileSetKey(mxid, key, content, propagateTo, support) { * @param {string} mxid * @param {string} key * @param {"none" | "unchanged" | "all"} [propagateTo] - * @param {{"msc4466": boolean, "msc4069": boolean}} [support] */ -async function profileDeleteKey(mxid, key, propagateTo, support) { +async function deleteProfileKey(mxid, key, propagateTo) { const params = {} if (key === "displayname" || key === "avatar_url") { - if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } - if (!support || support["msc4069"]) { params["org.matrix.msc4069.propagate"] = propagateTo === "all" } - } + const support = await mscSupport + if (support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } + if (propagateTo !== "all" && support["msc4069"]) { params["org.matrix.msc4069.propagate"] = false } + } await mreq.mreq("DELETE", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params)) } @@ -478,6 +478,19 @@ const urlPreviewEnabled = sync.remember(() => ({ fulfilled(this.cache ??= capabilities().then(c => c.capabilities?.["io.element.msc4452.preview_url"]?.enabled)) } })) +const mscSupport = sync.remember(() => ({ + /** @type {Promise<{"msc4069": boolean, "msc4466": boolean}>?} */ + cache: null, + then(/** @type {(_: NonNullable) => any} */ fulfilled) { + fulfilled(this.cache ??= versions().then(v => ({ + msc4069: !!v?.unstable_features?.["org.matrix.msc4069"], + msc4466: !!v?.unstable_features?.["computer.gingershaped.msc4466"], + })).catch(() => ({ + msc4069: false, + msc4466: false, + }))) + } +})) /** * @param {string} url @@ -674,10 +687,11 @@ module.exports.sendState = sendState module.exports.sendEvent = sendEvent module.exports.redactEvent = redactEvent module.exports.sendTyping = sendTyping -module.exports.profileSetKey = profileSetKey -module.exports.profileDeleteKey = profileDeleteKey +module.exports.setProfileKey = setProfileKey +module.exports.deleteProfileKey = deleteProfileKey module.exports.ping = ping module.exports.previewUrlIfEnabled = previewUrlIfEnabled +module.exports.mscSupport = mscSupport module.exports.getMedia = getMedia module.exports.sendReadReceipt = sendReadReceipt module.exports.ackEvent = ackEvent -- 2.34.1 From 819a7d41698f3e107bc947c9cd0e433fea737997 Mon Sep 17 00:00:00 2001 From: mschae23 Date: Fri, 13 Nov 2026 00:36:02 +0100 Subject: [PATCH 7/7] fix unsorted roles after startup --- src/d2m/discord-packets.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/d2m/discord-packets.js b/src/d2m/discord-packets.js index 500dec5..006a91a 100644 --- a/src/d2m/discord-packets.js +++ b/src/d2m/discord-packets.js @@ -93,6 +93,10 @@ async function onPacket(client, message, listen) { client.channels.set(thread.id, thread) } + for (const guild of client.guilds.values()) { + guild.roles.sort((a, b) => b.position - a.position) + } + if (listen === "full") { try { // Wait for guilds to be connected and homeserver to be online. If this is the last guild, a different code path is used to trigger the homeserver check. -- 2.34.1