Support creating v12 rooms

This commit is contained in:
Cadence Ember
2025-12-16 02:15:17 +13:00
parent a6bb248c0a
commit e4d0838af5
10 changed files with 149 additions and 38 deletions

View File

@@ -138,6 +138,16 @@ function getStateEvent(roomID, type, key) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state/${type}/${key}`)
}
/**
* @param {string} roomID
* @param {string} type
* @param {string} key
* @returns {Promise<Ty.Event.BaseStateEvent>} the entire state event
*/
function getStateEventOuter(roomID, type, key) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state/${type}/${key}?format=event`)
}
/**
* @param {string} roomID
* @returns {Promise<Ty.Event.InviteStrippedState[]>}
@@ -554,6 +564,7 @@ module.exports.getEvent = getEvent
module.exports.getEventForTimestamp = getEventForTimestamp
module.exports.getAllState = getAllState
module.exports.getStateEvent = getStateEvent
module.exports.getStateEventOuter = getStateEventOuter
module.exports.getInviteState = getInviteState
module.exports.getJoinedMembers = getJoinedMembers
module.exports.getMembers = getMembers

View File

@@ -10,6 +10,8 @@ const {sync} = passthrough
const file = sync.require("./file")
/** @type {import("./api")} */
const api = sync.require("./api")
/** @type {import("../m2d/converters/utils")} */
const utils = sync.require("../m2d/converters/utils")
/** Mutates the input. Not recursive - can only include or exclude entire state events. */
function kstateStripConditionals(kstate) {
@@ -45,12 +47,13 @@ async function kstateUploadMxc(obj) {
return obj
}
/** Automatically strips conditionals and uploads URLs to mxc. */
/** Automatically strips conditionals and uploads URLs to mxc. m.room.create is removed. */
async function kstateToState(kstate) {
const events = []
kstateStripConditionals(kstate)
await kstateUploadMxc(kstate)
for (const [k, content] of Object.entries(kstate)) {
if (k === "m.room.create/") continue
const slashIndex = k.indexOf("/")
assert(slashIndex > 0)
const type = k.slice(0, slashIndex)
@@ -60,6 +63,11 @@ async function kstateToState(kstate) {
return events
}
/** Extracts m.room.create for use in room creation_content. */
function kstateToCreationContent(kstate) {
return kstate["m.room.create/"] || {}
}
/**
* @param {import("../types").Event.BaseStateEvent[]} events
* @returns {any}
@@ -68,6 +76,11 @@ function stateToKState(events) {
const kstate = {}
for (const event of events) {
kstate[event.type + "/" + event.state_key] = event.content
// need to remember m.room.create sender for later...
if (event.type === "m.room.create" && event.state_key === "") {
kstate["m.room.create/outer"] = event
}
}
return kstate
}
@@ -81,12 +94,21 @@ function diffKState(actual, target) {
if (key === "m.room.power_levels/") {
// Special handling for power levels, we want to deep merge the actual and target into the final state.
if (!(key in actual)) throw new Error(`want to apply a power levels diff, but original power level data is missing\nstarted with: ${JSON.stringify(actual)}\nwant to apply: ${JSON.stringify(target)}`)
const temp = mixin({}, actual[key], target[key])
if (!isDeepStrictEqual(actual[key], temp)) {
const mixedTarget = mixin({}, actual[key], target[key])
if (!isDeepStrictEqual(actual[key], mixedTarget)) {
// they differ. use the newly prepared object as the diff.
diff[key] = temp
// if the diff includes users, it needs to be cleaned wrt room version 12
if (target[key].users && Object.keys(target[key].users).length > 0) {
if (!("m.room.create/" in actual)) throw new Error(`want to apply a power levels diff, but original m.room.create/ is missing\nstarted with: ${JSON.stringify(actual)}\nwant to apply: ${JSON.stringify(target)}`)
if (!("m.room.create/outer" in actual)) throw new Error(`want to apply a power levels diff, but original m.room.create/outer is missing\nstarted with: ${JSON.stringify(actual)}\nwant to apply: ${JSON.stringify(target)}`)
utils.removeCreatorsFromPowerLevels(actual["m.room.create/outer"], mixedTarget)
}
diff[key] = mixedTarget
}
} else if (key === "m.room.create/") {
// can't be modified - only for kstateToCreationContent
} else if (key in actual) {
// diff
if (!isDeepStrictEqual(actual[key], target[key])) {
@@ -129,6 +151,7 @@ async function applyKStateDiffToRoom(roomID, kstate) {
module.exports.kstateStripConditionals = kstateStripConditionals
module.exports.kstateUploadMxc = kstateUploadMxc
module.exports.kstateToState = kstateToState
module.exports.kstateToCreationContent = kstateToCreationContent
module.exports.stateToKState = stateToKState
module.exports.diffKState = diffKState
module.exports.roomToKState = roomToKState