2023-05-10 05:40:31 +00:00
// @ts-check
2023-08-23 04:16:36 +00:00
const assert = require ( "assert" ) . strict
2024-07-20 13:30:07 +00:00
const mixin = require ( "@cloudrac3r/mixin-deep" )
2024-03-06 23:19:07 +00:00
const { isDeepStrictEqual } = require ( "util" )
2023-05-10 05:40:31 +00:00
2024-06-06 00:12:48 +00:00
const passthrough = require ( "../passthrough" )
const { sync } = passthrough
/** @type {import("./file")} */
const file = sync . require ( "./file" )
/** Mutates the input. Not recursive - can only include or exclude entire state events. */
2023-05-10 05:40:31 +00:00
function kstateStripConditionals ( kstate ) {
for ( const [ k , content ] of Object . entries ( kstate ) ) {
// conditional for whether a key is even part of the kstate (doing this declaratively on json is hard, so represent it as a property instead.)
if ( "$if" in content ) {
if ( content . $if ) delete content . $if
else delete kstate [ k ]
}
}
return kstate
}
2024-06-06 00:12:48 +00:00
/** Mutates the input. Works recursively through object tree. */
async function kstateUploadMxc ( obj ) {
const promises = [ ]
function inner ( obj ) {
for ( const [ k , v ] of Object . entries ( obj ) ) {
if ( v == null || typeof v !== "object" ) continue
if ( v . $url ) {
promises . push (
file . uploadDiscordFileToMxc ( v . $url )
. then ( mxc => obj [ k ] = mxc )
)
}
inner ( v )
}
}
inner ( obj )
await Promise . all ( promises )
return obj
}
/** Automatically strips conditionals and uploads URLs to mxc. */
async function kstateToState ( kstate ) {
2023-05-10 05:40:31 +00:00
const events = [ ]
kstateStripConditionals ( kstate )
2024-06-06 00:12:48 +00:00
await kstateUploadMxc ( kstate )
2023-05-10 05:40:31 +00:00
for ( const [ k , content ] of Object . entries ( kstate ) ) {
2023-09-06 23:53:10 +00:00
const slashIndex = k . indexOf ( "/" )
assert ( slashIndex > 0 )
const type = k . slice ( 0 , slashIndex )
const state _key = k . slice ( slashIndex + 1 )
2023-05-10 05:40:31 +00:00
events . push ( { type , state _key , content } )
}
return events
}
/ * *
* @ param { import ( "../types" ) . Event . BaseStateEvent [ ] } events
* @ returns { any }
* /
function stateToKState ( events ) {
const kstate = { }
for ( const event of events ) {
kstate [ event . type + "/" + event . state _key ] = event . content
}
return kstate
}
function diffKState ( actual , target ) {
const diff = { }
// go through each key that it should have
for ( const key of Object . keys ( target ) ) {
2023-09-06 23:53:10 +00:00
if ( ! key . includes ( "/" ) ) throw new Error ( ` target kstate's key " ${ key } " does not contain a slash separator; if a blank state_key was intended, add a trailing slash to the kstate key. \n context: ${ JSON . stringify ( target ) } ` )
2023-08-23 04:16:36 +00:00
if ( key === "m.room.power_levels/" ) {
// Special handling for power levels, we want to deep merge the actual and target into the final state.
2023-08-23 05:08:20 +00:00
if ( ! ( key in actual ) ) throw new Error ( ` want to apply a power levels diff, but original power level data is missing \n started with: ${ JSON . stringify ( actual ) } \n want to apply: ${ JSON . stringify ( target ) } ` )
2023-08-23 04:16:36 +00:00
const temp = mixin ( { } , actual [ key ] , target [ key ] )
2024-03-06 23:19:07 +00:00
if ( ! isDeepStrictEqual ( actual [ key ] , temp ) ) {
2023-08-23 04:16:36 +00:00
// they differ. use the newly prepared object as the diff.
diff [ key ] = temp
}
} else if ( key in actual ) {
2023-05-10 05:40:31 +00:00
// diff
2024-03-06 23:19:07 +00:00
if ( ! isDeepStrictEqual ( actual [ key ] , target [ key ] ) ) {
2023-08-23 04:16:36 +00:00
// they differ. use the target as the diff.
2023-05-10 05:40:31 +00:00
diff [ key ] = target [ key ]
}
2023-08-23 04:16:36 +00:00
2023-05-10 05:40:31 +00:00
} else {
// not present, needs to be added
diff [ key ] = target [ key ]
}
2023-08-23 04:16:36 +00:00
2023-05-10 05:40:31 +00:00
// keys that are missing in "actual" will not be deleted on "target" (no action)
}
return diff
}
module . exports . kstateStripConditionals = kstateStripConditionals
2024-06-06 00:12:48 +00:00
module . exports . kstateUploadMxc = kstateUploadMxc
2023-05-10 05:40:31 +00:00
module . exports . kstateToState = kstateToState
module . exports . stateToKState = stateToKState
module . exports . diffKState = diffKState