forked from cadence/out-of-your-element
add tests, implement kstate and state diffing
This commit is contained in:
parent
c7868e9dbb
commit
f09eeccef3
10 changed files with 2656 additions and 64 deletions
|
@ -1,6 +1,8 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const {test} = require("supertape")
|
||||
const testData = require("../../test/data")
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
|
||||
const passthrough = require("../../passthrough")
|
||||
|
@ -10,22 +12,134 @@ const mreq = sync.require("../../matrix/mreq")
|
|||
/** @type {import("../../matrix/file")} */
|
||||
const file = sync.require("../../matrix/file")
|
||||
|
||||
function kstateToState(kstate) {
|
||||
return Object.entries(kstate).map(([k, content]) => {
|
||||
console.log(k)
|
||||
const [type, state_key] = k.split("/")
|
||||
assert.ok(typeof type === "string")
|
||||
assert.ok(typeof state_key === "string")
|
||||
return {type, state_key, content}
|
||||
})
|
||||
}
|
||||
|
||||
test("kstate2state: general", t => {
|
||||
t.deepEqual(kstateToState({
|
||||
"m.room.name/": {name: "test name"},
|
||||
"m.room.member/@cadence:cadence.moe": {membership: "join"}
|
||||
}), [
|
||||
{
|
||||
type: "m.room.name",
|
||||
state_key: "",
|
||||
content: {
|
||||
name: "test name"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "m.room.member",
|
||||
state_key: "@cadence:cadence.moe",
|
||||
content: {
|
||||
membership: "join"
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
function diffKState(actual, target) {
|
||||
const diff = {}
|
||||
// go through each key that it should have
|
||||
for (const key of Object.keys(target)) {
|
||||
if (key in actual) {
|
||||
// diff
|
||||
try {
|
||||
assert.deepEqual(actual[key], target[key])
|
||||
} catch (e) {
|
||||
// they differ. reassign the target
|
||||
diff[key] = target[key]
|
||||
}
|
||||
} else {
|
||||
// not present, needs to be added
|
||||
diff[key] = target[key]
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
test("diffKState: detects edits", t => {
|
||||
t.deepEqual(
|
||||
diffKState({
|
||||
"m.room.name/": {name: "test name"},
|
||||
"same/": {a: 2}
|
||||
}, {
|
||||
"m.room.name/": {name: "edited name"},
|
||||
"same/": {a: 2}
|
||||
}),
|
||||
{
|
||||
"m.room.name/": {name: "edited name"}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
test("diffKState: detects new properties", t => {
|
||||
t.deepEqual(
|
||||
diffKState({
|
||||
"m.room.name/": {name: "test name"},
|
||||
}, {
|
||||
"m.room.name/": {name: "test name"},
|
||||
"new/": {a: 2}
|
||||
}),
|
||||
{
|
||||
"new/": {a: 2}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||
* @param {import("discord-api-types/v10").APIGuild} guild
|
||||
*/
|
||||
async function createRoom(channel) {
|
||||
const guildID = channel.guild_id
|
||||
assert.ok(guildID)
|
||||
const guild = discord.guilds.get(guildID)
|
||||
assert.ok(guild)
|
||||
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guildID)
|
||||
async function channelToKState(channel, guild) {
|
||||
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
|
||||
assert.ok(typeof spaceID === "string")
|
||||
|
||||
const avatarEventContent = {}
|
||||
if (guild.icon) {
|
||||
avatarEventContent.url = await file.uploadDiscordFileToMxc(file.guildIcon(guild))
|
||||
avatarEventContent.discord_path = file.guildIcon(guild)
|
||||
avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path)
|
||||
}
|
||||
|
||||
const kstate = {
|
||||
"m.room.name/": {name: channel.name},
|
||||
"m.room.topic/": {topic: channel.topic || undefined},
|
||||
"m.room.avatar/": avatarEventContent,
|
||||
"m.room.guest_access/": {guest_access: "can_join"},
|
||||
"m.room.history_visibility/": {history_visibility: "invited"},
|
||||
[`m.space.parent/${spaceID}`]: { // TODO: put the proper server here
|
||||
via: ["cadence.moe"],
|
||||
canonical: true
|
||||
},
|
||||
"m.room.join_rules/": {
|
||||
join_rule: "restricted",
|
||||
allow: [{
|
||||
type: "m.room.membership",
|
||||
room_id: spaceID
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
return {spaceID, kstate}
|
||||
}
|
||||
|
||||
test("channel2room: general", async t => {
|
||||
t.deepEqual(await channelToKState(testData.channel.general, testData.guild.general).then(x => x.kstate), {expected: true, ...testData.room.general})
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||
* @param guild
|
||||
* @param {string} spaceID
|
||||
* @param {any} kstate
|
||||
*/
|
||||
async function createRoom(channel, guild, spaceID, kstate) {
|
||||
/** @type {import("../../types").R_RoomCreated} */
|
||||
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
|
||||
name: channel.name,
|
||||
|
@ -33,45 +147,7 @@ async function createRoom(channel) {
|
|||
preset: "private_chat",
|
||||
visibility: "private",
|
||||
invite: ["@cadence:cadence.moe"], // TODO
|
||||
initial_state: [
|
||||
{
|
||||
type: "m.room.avatar",
|
||||
state_key: "",
|
||||
content: avatarEventContent
|
||||
},
|
||||
{
|
||||
type: "m.room.guest_access",
|
||||
state_key: "",
|
||||
content: {
|
||||
guest_access: "can_join"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "m.room.history_visibility",
|
||||
state_key: "",
|
||||
content: {
|
||||
history_visibility: "invited"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "m.space.parent",
|
||||
state_key: spaceID,
|
||||
content: {
|
||||
via: ["cadence.moe"], // TODO: put the proper server here
|
||||
canonical: true
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "m.room.join_rules",
|
||||
content: {
|
||||
join_rule: "restricted",
|
||||
allow: [{
|
||||
type: "m.room.membership",
|
||||
room_id: spaceID
|
||||
}]
|
||||
}
|
||||
}
|
||||
]
|
||||
initial_state: kstateToState(kstate)
|
||||
})
|
||||
|
||||
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id)
|
||||
|
@ -82,6 +158,24 @@ async function createRoom(channel) {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||
*/
|
||||
async function syncRoom(channel) {
|
||||
const guildID = channel.guild_id
|
||||
assert(guildID)
|
||||
const guild = discord.guilds.get(guildID)
|
||||
assert(guild)
|
||||
|
||||
const {spaceID, kstate} = await channelToKState(channel, guild)
|
||||
|
||||
/** @type {string?} */
|
||||
const existing = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channel.id)
|
||||
if (!existing) {
|
||||
createRoom(channel, guild, spaceID, kstate)
|
||||
}
|
||||
}
|
||||
|
||||
async function createAllForGuild(guildID) {
|
||||
const channelIDs = discord.guildChannelMap.get(guildID)
|
||||
assert.ok(channelIDs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue