1
0
Fork 0
out-of-your-element-fork-th.../d2m/actions/create-room.js

212 lines
6.6 KiB
JavaScript
Raw Normal View History

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")
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/kstate")} */
const ks = sync.require("../../matrix/kstate")
/**
* @param {string} roomID
*/
async function roomToKState(roomID) {
const root = await api.getAllState(roomID)
return ks.stateToKState(root)
}
/**
* @params {string} roomID
* @params {any} kstate
*/
function applyKStateDiffToRoom(roomID, kstate) {
const events = ks.kstateToState(kstate)
return Promise.all(events.map(({type, state_key, content}) =>
api.sendState(roomID, type, state_key, content)
))
}
2023-05-04 20:25:00 +00:00
/**
2023-06-28 12:06:56 +00:00
* @param {DiscordTypes.APIGuildTextChannel} channel
* @param {DiscordTypes.APIGuild} guild
2023-05-04 20:25:00 +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")
2023-06-28 12:06:56 +00:00
const customName = db.prepare("SELECT nick FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
2023-05-04 20:25:00 +00:00
const avatarEventContent = {}
if (guild.icon) {
avatarEventContent.discord_path = file.guildIcon(guild)
avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API
}
2023-06-28 12:06:56 +00:00
// TODO: Improve nasty nested ifs
let convertedName, convertedTopic
if (customName) {
convertedName = customName
if (channel.topic) {
convertedTopic = `${channel.name} | ${channel.topic}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
} else {
convertedTopic = `${channel.name}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
}
} else {
convertedName = channel.name
if (channel.topic) {
convertedTopic = `${channel.topic}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
} else {
convertedTopic = `Channel ID: ${channel.id}\nGuild ID: ${guild.id}`
}
}
const channelKState = {
2023-06-28 12:06:56 +00:00
"m.room.name/": {name: convertedName},
"m.room.topic/": {topic: convertedTopic},
"m.room.avatar/": avatarEventContent,
"m.room.guest_access/": {guest_access: "can_join"},
"m.room.history_visibility/": {history_visibility: "invited"},
[`m.space.parent/${spaceID}`]: {
via: ["cadence.moe"], // TODO: put the proper server here
canonical: true
},
"m.room.join_rules/": {
join_rule: "restricted",
allow: [{
2023-06-28 11:38:58 +00:00
type: "m.room_membership",
room_id: spaceID
}]
}
2023-04-30 12:57:30 +00:00
}
2023-05-04 20:25:00 +00:00
return {spaceID, channelKState}
}
/**
* Create a bridge room, store the relationship in the database, and add it to the guild's space.
2023-06-28 12:06:56 +00:00
* @param {DiscordTypes.APIGuildTextChannel} channel
* @param guild
* @param {string} spaceID
* @param {any} kstate
* @returns {Promise<string>} room ID
*/
async function createRoom(channel, guild, spaceID, kstate) {
const roomID = 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
initial_state: ks.kstateToState(kstate)
2023-05-04 20:25:00 +00:00
})
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, roomID)
2023-05-04 20:25:00 +00:00
// Put the newly created child into the space
await api.sendState(spaceID, "m.space.child", roomID, { // TODO: should I deduplicate with the equivalent code from syncRoom?
2023-05-04 20:25:00 +00:00
via: ["cadence.moe"] // TODO: use the proper server
})
return roomID
2023-05-04 20:25:00 +00:00
}
/**
2023-06-28 12:06:56 +00:00
* @param {DiscordTypes.APIGuildChannel} channel
*/
function channelToGuild(channel) {
const guildID = channel.guild_id
assert(guildID)
const guild = discord.guilds.get(guildID)
assert(guild)
return guild
}
/*
Ensure flow:
1. Get IDs
2. Does room exist? If so great!
(it doesn't, so it needs to be created)
3. Get kstate for channel
4. Create room, return new ID
New combined flow with ensure / sync:
1. Get IDs
2. Does room exist?
2.5: If room does exist AND don't need to sync: return here
3. Get kstate for channel
4. Create room with kstate if room doesn't exist
5. Get and update room state with kstate if room does exist
*/
/**
* @param {string} channelID
* @param {boolean} shouldActuallySync false if just need to ensure room exists (which is a quick database check), true if also want to sync room data when it does exist (slow)
* @returns {Promise<string>} room ID
*/
async function _syncRoom(channelID, shouldActuallySync) {
2023-06-28 12:06:56 +00:00
/** @ts-ignore @type {DiscordTypes.APIGuildChannel} */
const channel = discord.channels.get(channelID)
assert.ok(channel)
const guild = channelToGuild(channel)
/** @type {string?} */
const existing = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channel.id)
if (!existing) {
const {spaceID, channelKState} = await channelToKState(channel, guild)
return createRoom(channel, guild, spaceID, channelKState)
} else {
if (!shouldActuallySync) {
return existing // only need to ensure room exists, and it does. return the room ID
}
console.log(`[room sync] to matrix: ${channel.name}`)
const {spaceID, channelKState} = await channelToKState(channel, guild)
// sync channel state to room
const roomKState = await roomToKState(existing)
const roomDiff = ks.diffKState(roomKState, channelKState)
const roomApply = applyKStateDiffToRoom(existing, roomDiff)
// sync room as space member
const spaceKState = await roomToKState(spaceID)
const spaceDiff = ks.diffKState(spaceKState, {
[`m.space.child/${existing}`]: {
via: ["cadence.moe"] // TODO: use the proper server
}
})
const spaceApply = applyKStateDiffToRoom(spaceID, spaceDiff)
await Promise.all([roomApply, spaceApply])
return existing
}
}
function ensureRoom(channelID) {
return _syncRoom(channelID, false)
}
function syncRoom(channelID) {
return _syncRoom(channelID, true)
}
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-06-28 11:38:58 +00:00
if (discord.channels.get(channelID)?.type === DiscordTypes.ChannelType.GuildText) { // TODO: guild sync thread channels and such. maybe make a helper function to check if a given channel is syncable?
await syncRoom(channelID).then(r => console.log(`synced ${channelID}:`, r))
}
2023-05-04 20:25:00 +00:00
}
}
module.exports.createRoom = createRoom
module.exports.ensureRoom = ensureRoom
module.exports.syncRoom = syncRoom
2023-05-04 20:25:00 +00:00
module.exports.createAllForGuild = createAllForGuild
module.exports.channelToKState = channelToKState