d2m username colors #107
4 changed files with 21 additions and 14 deletions
fetch user from Discord to get accent color
commit
f59e3f46ab
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,17 +282,23 @@ 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))
|
||||
}
|
||||
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 (!options.globalProfile[key]) {
|
||||
if (!globalProfile[key]) {
|
||||
actions.push(api.profileDeleteKey(mxid, key, "none", support))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await Promise.all(actions)
|
||||
// Update cached hash
|
||||
db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE room_id = ? AND mxid = ?").run(currentHash, roomID, mxid)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue