d2m username colors #107

Open
mschae wants to merge 8 commits from mschae/out-of-your-element:role-colors into main
4 changed files with 21 additions and 14 deletions
Showing only changes of commit f59e3f46ab - Show all commits

fetch user from Discord to get accent color

mschae23 2026-11-07 17:26:27 +01:00
Signed by untrusted user: mschae
GPG key ID: A87721D300DE88AB

View file

@ -161,7 +161,7 @@ async function syncUser(messageID, author, roomID, shouldActuallySync) {
if (shouldActuallySync) { if (shouldActuallySync) {
// Build current profile data and sync if the hash has changed // Build current profile data and sync if the hash has changed
const content = await memberToStateContent(pkMessage, author) const content = await memberToStateContent(pkMessage, author)
await registerUser._sendSyncUser(roomID, mxid, content, null) await registerUser._sendSyncUser(roomID, mxid, null, content, null)
} }
return mxid return mxid

View file

@ -161,7 +161,7 @@ async function syncUser(channelID, messageID, author, roomID, shouldActuallySync
if (shouldActuallySync) { if (shouldActuallySync) {
// Build current profile data and sync if the hash has changed // Build current profile data and sync if the hash has changed
const content = await memberToStateContent(pluRalMessage, author) const content = await memberToStateContent(pluRalMessage, author)
await registerUser._sendSyncUser(roomID, mxid, content, null) await registerUser._sendSyncUser(roomID, mxid, null, content, null)
} }
return mxid return mxid

View file

@ -237,10 +237,10 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat
const mxid = await ensureSimJoined(user, roomID) const mxid = await ensureSimJoined(user, roomID)
const content = await memberToStateContent(user, member, guild) const content = await memberToStateContent(user, member, guild)
const powerLevel = memberToPowerLevel(user, member, guild, channel) 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 // 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, allowOverwrite: !!member,
globalProfile: await userToGlobalProfile(user) updateGlobalProfile: true,
}) })
const appInstalledByUser = user.bot && interactionMetadata?.authorizing_integration_owners?.[DiscordTypes.ApplicationIntegrationType.UserInstall] 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} roomID
* @param {string} mxid * @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 {{displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}} content
* @param {number | null} powerLevel * @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 currentHash = _hashProfileContent(content, powerLevel ?? 0)
const existingHash = select("sim_member", "hashed_profile_content", {room_id: roomID, mxid}).safeIntegers().pluck().get() const existingHash = select("sim_member", "hashed_profile_content", {room_id: roomID, mxid}).safeIntegers().pluck().get()
// console.log(roomID, mxid, existingHash, currentHash) // console.log(roomID, mxid, existingHash, currentHash)
@ -281,17 +282,23 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) {
}}).catch(() => { return {msc4069: false, msc4466: false}}) }}).catch(() => { return {msc4069: false, msc4466: false}})
// Update global profile (if supported by server) // Update global profile (if supported by server)
if (!!options?.globalProfile && (support.msc4069 || support.msc4466)) { if (user && options?.updateGlobalProfile && (support.msc4069 || support.msc4466)) {
for (const key of Object.keys(options.globalProfile)) { const needFullProfile = !content["eu.she-a.color"]
actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "none", support)) 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))
} }
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 => { return {}})
for (const key of Object.keys(oldProfile)) { for (const key of Object.keys(oldProfile)) {
if (!options.globalProfile[key]) { if (!globalProfile[key]) {
actions.push(api.profileDeleteKey(mxid, key, "none", support)) actions.push(api.profileDeleteKey(mxid, key, "none", support))
} }
} }
} }
}
await Promise.all(actions) await Promise.all(actions)
// Update cached hash // Update cached hash
db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE room_id = ? AND mxid = ?").run(currentHash, roomID, mxid) db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE room_id = ? AND mxid = ?").run(currentHash, roomID, mxid)

View file

@ -120,7 +120,7 @@ async function syncUser(author, roomID, shouldActuallySync) {
if (shouldActuallySync) { if (shouldActuallySync) {
// Build current profile data and sync if the hash has changed // Build current profile data and sync if the hash has changed
const content = await authorToStateContent(author) const content = await authorToStateContent(author)
await registerUser._sendSyncUser(roomID, mxid, content, null) await registerUser._sendSyncUser(roomID, mxid, null, content, null)
} }
return mxid return mxid