d2m username colors #107

Open
mschae wants to merge 8 commits from mschae/out-of-your-element:role-colors into main
3 changed files with 47 additions and 49 deletions
Showing only changes of commit 510fd5aa3d - Show all commits

make better use of generic profile, add support for MSC 4466

mschae23 2026-11-07 14:12:58 +01:00
Signed by untrusted user: mschae
GPG key ID: A87721D300DE88AB

View file

@ -364,8 +364,8 @@ function defineEchoHandler() {
// set profile data on homeserver... // 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.)") 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.profileSetKey(mxid, "displayname", "Out Of Your Element (bot)", "all")
await api.profileSetAvatarUrl(mxid, avatarUrl) await api.profileSetKey(mxid, "avatar_url", avatarUrl, "all")
console.log("✅ Matrix profile updated...") console.log("✅ Matrix profile updated...")
console.log("Good to go. I hope you enjoy Out Of Your Element.") console.log("Good to go. I hope you enjoy Out Of Your Element.")

View file

@ -24,8 +24,6 @@ let hasher = null
// @ts-ignore // @ts-ignore
require("xxhash-wasm")().then(h => hasher = h) 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. * A sim is an account that is being simulated by the bridge to copy events from the other side.
* @param {DiscordTypes.APIUser} user * @param {DiscordTypes.APIUser} user
@ -107,8 +105,12 @@ async function userToGlobalProfile(user) {
globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath) globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath)
} }
if (user.accent_color) { if (user.accent_color !== null && user.accent_color !== undefined) {
globalProfile.name_color = `#${user.accent_color.toString(16).padStart(6, "0")}` const encoded = `#${user.accent_color.toString(16).padStart(6, "0")}`
globalProfile["eu.she-a.color"] = {
on_light: encoded,
on_dark: encoded
}
} }
return globalProfile return globalProfile
@ -254,7 +256,7 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat
* @param {string} mxid * @param {string} mxid
* @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, 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) { async function _sendSyncUser(roomID, mxid, content, powerLevel, options) {
const currentHash = _hashProfileContent(content, powerLevel ?? 0) const currentHash = _hashProfileContent(content, powerLevel ?? 0)
@ -272,13 +274,22 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) {
if (powerLevel != null) { if (powerLevel != null) {
actions.push(mxUtils.setUserPower(roomID, mxid, powerLevel, api)) 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) // Update global profile (if supported by server)
if (await supportsMsc4069) { if (!!options?.globalProfile && (support.msc4069 || support.msc4466)) {
actions.push(api.profileSetDisplayname(mxid, options?.globalProfile?.displayname || content.displayname, true)) for (const key of Object.keys(options.globalProfile)) {
actions.push(api.profileSetAvatarUrl(mxid, options?.globalProfile?.avatar_url || content.avatar_url, true)) actions.push(api.profileSetKey(mxid, key, options.globalProfile[key], "unchanged", support))
if (options?.globalProfile?.name_color || content["eu.she-a.color"]?.on_light) { }
// @ts-ignore const oldProfile = await api.getProfile(mxid).catch(e => { return {}})
actions.push(api.profileSetNameColor(mxid, options?.globalProfile?.name_color ?? content["eu.she-a.color"]?.on_light, true)) for (const key of Object.keys(oldProfile)) {
if (!options.globalProfile[key]) {
actions.push(api.profileDeleteKey(mxid, key, "unchanged", support))
}
} }
} }
await Promise.all(actions) await Promise.all(actions)
@ -324,4 +335,3 @@ module.exports.syncUser = syncUser
module.exports._sendSyncUser = _sendSyncUser module.exports._sendSyncUser = _sendSyncUser
module.exports.syncAllUsersInRoom = syncAllUsersInRoom module.exports.syncAllUsersInRoom = syncAllUsersInRoom
module.exports._memberToPowerLevel = memberToPowerLevel module.exports._memberToPowerLevel = memberToPowerLevel
module.exports.supportsMsc4069 = supportsMsc4069

View file

@ -421,46 +421,35 @@ async function sendTyping(roomID, isTyping, mxid, duration) {
/** /**
* @param {string} mxid * @param {string} mxid
* @param {string} displayname * @param {string} key
* @param {boolean} [inhibitPropagate] * @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 = {} const params = {}
if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false if (key === "displayname" || key === "avatar_url") {
await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/displayname`, mxid, params), { if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" }
displayname 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} mxid
* @param {string | null | undefined} avatar_url * @param {string} key
* @param {boolean} [inhibitPropagate] * @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 = {} const params = {}
if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false if (key === "displayname" || key === "avatar_url") {
if (avatar_url) { if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" }
await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/avatar_url`, mxid, params), { if (!support || support["msc4069"]) { params["org.matrix.msc4069.propagate"] = propagateTo === "all" }
avatar_url }
}) await mreq.mreq("DELETE", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params))
} 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
})
} }
async function ping() { async function ping() {
@ -685,9 +674,8 @@ module.exports.sendState = sendState
module.exports.sendEvent = sendEvent module.exports.sendEvent = sendEvent
module.exports.redactEvent = redactEvent module.exports.redactEvent = redactEvent
module.exports.sendTyping = sendTyping module.exports.sendTyping = sendTyping
module.exports.profileSetDisplayname = profileSetDisplayname module.exports.profileSetKey = profileSetKey
module.exports.profileSetAvatarUrl = profileSetAvatarUrl module.exports.profileDeleteKey = profileDeleteKey
module.exports.profileSetNameColor = profileSetNameColor
module.exports.ping = ping module.exports.ping = ping
module.exports.previewUrlIfEnabled = previewUrlIfEnabled module.exports.previewUrlIfEnabled = previewUrlIfEnabled
module.exports.getMedia = getMedia module.exports.getMedia = getMedia