forked from cadence/out-of-your-element
Refactor kstate resource uploading
This commit is contained in:
parent
07a133eba9
commit
24a3b9b0f4
7 changed files with 196 additions and 18 deletions
|
@ -4,7 +4,12 @@ const assert = require("assert").strict
|
|||
const mixin = require("mixin-deep")
|
||||
const {isDeepStrictEqual} = require("util")
|
||||
|
||||
/** Mutates the input. */
|
||||
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. */
|
||||
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.)
|
||||
|
@ -16,9 +21,33 @@ function kstateStripConditionals(kstate) {
|
|||
return kstate
|
||||
}
|
||||
|
||||
function kstateToState(kstate) {
|
||||
/** 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) {
|
||||
const events = []
|
||||
kstateStripConditionals(kstate)
|
||||
await kstateUploadMxc(kstate)
|
||||
for (const [k, content] of Object.entries(kstate)) {
|
||||
const slashIndex = k.indexOf("/")
|
||||
assert(slashIndex > 0)
|
||||
|
@ -74,6 +103,7 @@ function diffKState(actual, target) {
|
|||
}
|
||||
|
||||
module.exports.kstateStripConditionals = kstateStripConditionals
|
||||
module.exports.kstateUploadMxc = kstateUploadMxc
|
||||
module.exports.kstateToState = kstateToState
|
||||
module.exports.stateToKState = stateToKState
|
||||
module.exports.diffKState = diffKState
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const assert = require("assert")
|
||||
const {kstateToState, stateToKState, diffKState, kstateStripConditionals} = require("./kstate")
|
||||
const {kstateToState, stateToKState, diffKState, kstateStripConditionals, kstateUploadMxc} = require("./kstate")
|
||||
const {test} = require("supertape")
|
||||
|
||||
test("kstate strip: strips false conditions", t => {
|
||||
|
@ -21,8 +21,53 @@ test("kstate strip: keeps true conditions while removing $if", t => {
|
|||
})
|
||||
})
|
||||
|
||||
test("kstate2state: general", t => {
|
||||
t.deepEqual(kstateToState({
|
||||
test("kstateUploadMxc: sets the mxc", async t => {
|
||||
const input = {
|
||||
"m.room.avatar/": {
|
||||
url: {$url: "https://cdn.discordapp.com/guilds/112760669178241024/users/134826546694193153/avatars/38dd359aa12bcd52dd3164126c587f8c.png?size=1024"},
|
||||
test1: {
|
||||
test2: {
|
||||
test3: {$url: "https://cdn.discordapp.com/attachments/176333891320283136/1157854643037163610/Screenshot_20231001_034036.jpg"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await kstateUploadMxc(input)
|
||||
t.deepEqual(input, {
|
||||
"m.room.avatar/": {
|
||||
url: "mxc://cadence.moe/rfemHmAtcprjLEiPiEuzPhpl",
|
||||
test1: {
|
||||
test2: {
|
||||
test3: "mxc://cadence.moe/zAXdQriaJuLZohDDmacwWWDR"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test("kstateUploadMxc and strip: work together", async t => {
|
||||
const input = {
|
||||
"m.room.avatar/yes": {
|
||||
$if: true,
|
||||
url: {$url: "https://cdn.discordapp.com/guilds/112760669178241024/users/134826546694193153/avatars/38dd359aa12bcd52dd3164126c587f8c.png?size=1024"}
|
||||
},
|
||||
"m.room.avatar/no": {
|
||||
$if: false,
|
||||
url: {$url: "https://cdn.discordapp.com/avatars/320067006521147393/5fc4ad85c1ea876709e9a7d3374a78a1.png?size=1024"}
|
||||
},
|
||||
}
|
||||
kstateStripConditionals(input)
|
||||
await kstateUploadMxc(input)
|
||||
t.deepEqual(input, {
|
||||
"m.room.avatar/yes": {
|
||||
url: "mxc://cadence.moe/rfemHmAtcprjLEiPiEuzPhpl"
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
test("kstate2state: general", async t => {
|
||||
t.deepEqual(await kstateToState({
|
||||
"m.room.name/": {name: "test name"},
|
||||
"m.room.member/@cadence:cadence.moe": {membership: "join"},
|
||||
"uk.half-shot.bridge/org.matrix.appservice-irc://irc/epicord.net/#general": {creator: "@cadence:cadence.moe"}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue