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 35 additions and 23 deletions
Showing only changes of commit 025b2e4dcc - Show all commits

memoize MSC support

mschae23 2026-11-08 16:51:52 +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.profileSetKey(mxid, "displayname", "Out Of Your Element (bot)", "all") await api.setProfileKey(mxid, "displayname", "Out Of Your Element (bot)", "all")
await api.profileSetKey(mxid, "avatar_url", avatarUrl, "all") await api.setProfileKey(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

@ -276,25 +276,23 @@ async function _sendSyncUser(roomID, mxid, user, content, powerLevel, options) {
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)
const support = await api.mscSupport
if (user && options?.updateGlobalProfile && (support.msc4069 || support.msc4466)) { 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 needFullProfile = !content["eu.she-a.color"]
const globalProfile = await (needFullProfile ? discord.snow.user.getUser(user.id) : Promise.resolve(user)) 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)) { 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) { if (needFullProfile) {
// don't delete keys if some profile fields from Discord might be missing // 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)) { for (const key of Object.keys(oldProfile)) {
if (!globalProfile[key]) { if (!globalProfile[key]) {
actions.push(api.profileDeleteKey(mxid, key, "none", support)) actions.push(api.deleteProfileKey(mxid, key, "none"))
} }
} }
} }

View file

@ -424,14 +424,14 @@ async function sendTyping(roomID, isTyping, mxid, duration) {
* @param {string} key * @param {string} key
* @param {object} content * @param {object} content
* @param {"none" | "unchanged" | "all"} [propagateTo] * @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 = {} const params = {}
if (key === "displayname" || key === "avatar_url") { if (key === "displayname" || key === "avatar_url") {
if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } const support = await mscSupport
if (propagateTo !== "all" && (!support || support["msc4069"])) { params["org.matrix.msc4069.propagate"] = false } if (support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" }
} if (propagateTo !== "all" && support["msc4069"]) { params["org.matrix.msc4069.propagate"] = false }
}
const body = {} const body = {}
body[key] = content body[key] = content
await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/${key}`, mxid, params), body) 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} mxid
* @param {string} key * @param {string} key
* @param {"none" | "unchanged" | "all"} [propagateTo] * @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 = {} const params = {}
if (key === "displayname" || key === "avatar_url") { if (key === "displayname" || key === "avatar_url") {
if (!support || support["msc4466"]) { params["computer.gingershaped.msc4466.propagate_to"] = propagateTo ?? "none" } const support = await mscSupport
if (!support || support["msc4069"]) { params["org.matrix.msc4069.propagate"] = propagateTo === "all" } 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)) 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)) 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<typeof this.cache>) => 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 * @param {string} url
@ -674,10 +687,11 @@ 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.profileSetKey = profileSetKey module.exports.setProfileKey = setProfileKey
module.exports.profileDeleteKey = profileDeleteKey module.exports.deleteProfileKey = deleteProfileKey
module.exports.ping = ping module.exports.ping = ping
module.exports.previewUrlIfEnabled = previewUrlIfEnabled module.exports.previewUrlIfEnabled = previewUrlIfEnabled
module.exports.mscSupport = mscSupport
module.exports.getMedia = getMedia module.exports.getMedia = getMedia
module.exports.sendReadReceipt = sendReadReceipt module.exports.sendReadReceipt = sendReadReceipt
module.exports.ackEvent = ackEvent module.exports.ackEvent = ackEvent