2023-04-30 12:57:30 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-05-04 20:25:00 +00:00
|
|
|
const assert = require("assert").strict
|
|
|
|
const DiscordTypes = require("discord-api-types/v10")
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const { discord, sync, db } = passthrough
|
|
|
|
/** @type {import("../../matrix/file")} */
|
|
|
|
const file = sync.require("../../matrix/file")
|
2023-05-08 11:37:51 +00:00
|
|
|
/** @type {import("../../matrix/api")} */
|
|
|
|
const api = sync.require("../../matrix/api")
|
2023-05-04 20:25:00 +00:00
|
|
|
|
2023-05-05 13:25:15 +00:00
|
|
|
function kstateStripConditionals(kstate) {
|
|
|
|
for (const [k, content] of Object.entries(kstate)) {
|
|
|
|
if ("$if" in content) {
|
|
|
|
if (content.$if) delete content.$if
|
|
|
|
else delete kstate[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kstate
|
|
|
|
}
|
|
|
|
|
2023-05-05 05:29:08 +00:00
|
|
|
function kstateToState(kstate) {
|
2023-05-05 13:25:15 +00:00
|
|
|
const events = []
|
|
|
|
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 && !content.$if) continue
|
|
|
|
delete content.$if
|
|
|
|
|
2023-05-05 05:29:08 +00:00
|
|
|
const [type, state_key] = k.split("/")
|
|
|
|
assert.ok(typeof type === "string")
|
|
|
|
assert.ok(typeof state_key === "string")
|
2023-05-05 13:25:15 +00:00
|
|
|
events.push({type, state_key, content})
|
|
|
|
}
|
|
|
|
return events
|
2023-05-05 05:29:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 13:25:15 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} roomID
|
|
|
|
*/
|
|
|
|
async function roomToKState(roomID) {
|
2023-05-08 11:37:51 +00:00
|
|
|
const root = await api.getAllState(roomID)
|
2023-05-05 13:25:15 +00:00
|
|
|
return stateToKState(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @params {string} roomID
|
|
|
|
* @params {any} kstate
|
|
|
|
*/
|
|
|
|
function applyKStateDiffToRoom(roomID, kstate) {
|
|
|
|
const events = kstateToState(kstate)
|
|
|
|
return Promise.all(events.map(({type, state_key, content}) =>
|
2023-05-08 11:37:51 +00:00
|
|
|
api.sendState(roomID, type, state_key, content)
|
2023-05-05 13:25:15 +00:00
|
|
|
))
|
|
|
|
}
|
2023-05-05 05:29:08 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
2023-05-05 13:25:15 +00:00
|
|
|
// keys that are missing in "actual" will not be deleted on "target" (no action)
|
2023-05-05 05:29:08 +00:00
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
2023-05-04 20:25:00 +00:00
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
2023-05-05 05:29:08 +00:00
|
|
|
* @param {import("discord-api-types/v10").APIGuild} guild
|
2023-05-04 20:25:00 +00:00
|
|
|
*/
|
2023-05-05 05:29:08 +00:00
|
|
|
async function channelToKState(channel, guild) {
|
|
|
|
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
|
2023-05-04 20:25:00 +00:00
|
|
|
assert.ok(typeof spaceID === "string")
|
|
|
|
|
|
|
|
const avatarEventContent = {}
|
|
|
|
if (guild.icon) {
|
2023-05-05 05:29:08 +00:00
|
|
|
avatarEventContent.discord_path = file.guildIcon(guild)
|
|
|
|
avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path)
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:25:15 +00:00
|
|
|
const channelKState = {
|
2023-05-05 05:29:08 +00:00
|
|
|
"m.room.name/": {name: channel.name},
|
2023-05-05 13:25:15 +00:00
|
|
|
"m.room.topic/": {$if: channel.topic, topic: channel.topic},
|
2023-05-05 05:29:08 +00:00
|
|
|
"m.room.avatar/": avatarEventContent,
|
|
|
|
"m.room.guest_access/": {guest_access: "can_join"},
|
|
|
|
"m.room.history_visibility/": {history_visibility: "invited"},
|
2023-05-05 13:25:15 +00:00
|
|
|
[`m.space.parent/${spaceID}`]: {
|
|
|
|
via: ["cadence.moe"], // TODO: put the proper server here
|
2023-05-05 05:29:08 +00:00
|
|
|
canonical: true
|
|
|
|
},
|
|
|
|
"m.room.join_rules/": {
|
|
|
|
join_rule: "restricted",
|
|
|
|
allow: [{
|
|
|
|
type: "m.room.membership",
|
|
|
|
room_id: spaceID
|
|
|
|
}]
|
|
|
|
}
|
2023-04-30 12:57:30 +00:00
|
|
|
}
|
2023-05-04 20:25:00 +00:00
|
|
|
|
2023-05-05 13:25:15 +00:00
|
|
|
return {spaceID, channelKState}
|
2023-05-05 05:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
|
|
|
* @param guild
|
|
|
|
* @param {string} spaceID
|
|
|
|
* @param {any} kstate
|
|
|
|
*/
|
|
|
|
async function createRoom(channel, guild, spaceID, kstate) {
|
2023-05-08 11:37:51 +00:00
|
|
|
const root = await api.createRoom({
|
2023-05-04 20:25:00 +00:00
|
|
|
name: channel.name,
|
|
|
|
topic: channel.topic || undefined,
|
|
|
|
preset: "private_chat",
|
|
|
|
visibility: "private",
|
|
|
|
invite: ["@cadence:cadence.moe"], // TODO
|
2023-05-05 05:29:08 +00:00
|
|
|
initial_state: kstateToState(kstate)
|
2023-05-04 20:25:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id)
|
|
|
|
|
|
|
|
// Put the newly created child into the space
|
2023-05-08 11:37:51 +00:00
|
|
|
await api.sendState(spaceID, "m.space.child", root.room_id, {
|
2023-05-04 20:25:00 +00:00
|
|
|
via: ["cadence.moe"] // TODO: use the proper server
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-05 05:29:08 +00:00
|
|
|
/**
|
2023-05-05 13:25:15 +00:00
|
|
|
* @param {import("discord-api-types/v10").APIGuildChannel} channel
|
2023-05-05 05:29:08 +00:00
|
|
|
*/
|
2023-05-05 13:25:15 +00:00
|
|
|
function channelToGuild(channel) {
|
2023-05-05 05:29:08 +00:00
|
|
|
const guildID = channel.guild_id
|
|
|
|
assert(guildID)
|
|
|
|
const guild = discord.guilds.get(guildID)
|
|
|
|
assert(guild)
|
2023-05-05 13:25:15 +00:00
|
|
|
return guild
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} channelID
|
|
|
|
*/
|
|
|
|
async function syncRoom(channelID) {
|
|
|
|
/** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */
|
|
|
|
const channel = discord.channels.get(channelID)
|
|
|
|
assert.ok(channel)
|
|
|
|
const guild = channelToGuild(channel)
|
2023-05-05 05:29:08 +00:00
|
|
|
|
2023-05-05 13:25:15 +00:00
|
|
|
const {spaceID, channelKState} = await channelToKState(channel, guild)
|
2023-05-05 05:29:08 +00:00
|
|
|
|
|
|
|
/** @type {string?} */
|
|
|
|
const existing = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channel.id)
|
|
|
|
if (!existing) {
|
2023-05-05 13:25:15 +00:00
|
|
|
return createRoom(channel, guild, spaceID, channelKState)
|
|
|
|
} else {
|
|
|
|
// sync channel state to room
|
|
|
|
const roomKState = await roomToKState(existing)
|
|
|
|
const roomDiff = diffKState(roomKState, channelKState)
|
|
|
|
const roomApply = applyKStateDiffToRoom(existing, roomDiff)
|
|
|
|
|
|
|
|
// sync room as space member
|
|
|
|
const spaceKState = await roomToKState(spaceID)
|
|
|
|
const spaceDiff = diffKState(spaceKState, {
|
|
|
|
[`m.space.child/${existing}`]: {
|
|
|
|
via: ["cadence.moe"] // TODO: use the proper server
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const spaceApply = applyKStateDiffToRoom(spaceID, spaceDiff)
|
|
|
|
return Promise.all([roomApply, spaceApply])
|
2023-05-05 05:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 20:25:00 +00:00
|
|
|
async function createAllForGuild(guildID) {
|
|
|
|
const channelIDs = discord.guildChannelMap.get(guildID)
|
|
|
|
assert.ok(channelIDs)
|
|
|
|
for (const channelID of channelIDs) {
|
2023-05-05 13:25:15 +00:00
|
|
|
await syncRoom(channelID).then(r => console.log(`synced ${channelID}:`, r))
|
2023-05-04 20:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.createRoom = createRoom
|
2023-05-05 13:25:15 +00:00
|
|
|
module.exports.syncRoom = syncRoom
|
2023-05-04 20:25:00 +00:00
|
|
|
module.exports.createAllForGuild = createAllForGuild
|
2023-05-05 13:25:15 +00:00
|
|
|
module.exports.kstateToState = kstateToState
|
|
|
|
module.exports.stateToKState = stateToKState
|
|
|
|
module.exports.diffKState = diffKState
|
|
|
|
module.exports.channelToKState = channelToKState
|
|
|
|
module.exports.kstateStripConditionals = kstateStripConditionals
|