category subspaces #113

Open
mschae wants to merge 9 commits from mschae/out-of-your-element:category-subspaces into main
4 changed files with 41 additions and 6 deletions
Showing only changes of commit 0ef44734c7 - Show all commits

allow manually linking categories to subspaces in the web UI

mschae23 2026-11-22 14:12:48 +01:00
Signed by untrusted user: mschae
GPG key ID: A87721D300DE88AB

View file

@ -654,7 +654,7 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id, tryRemoveFro
let oldSpaceID
let oldSpaceKState
if (tryRemoveFromOld) {
if (tryRemoveFromOld && autocreate) {
oldSpaceID = select("channel_room", "parent_space_id", {channel_id: channel.id}).pluck().get()
if (oldSpaceID) {
oldSpaceKState = await ks.roomToKState(oldSpaceID)
@ -715,7 +715,7 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id, tryRemoveFro
})
actions.push(ks.applyKStateDiffToRoom(spaceID, spaceDiff))
if (tryRemoveFromOld) {
if (tryRemoveFromOld && autocreate) {
if (oldSpaceID) {
if (oldSpaceID !== spaceID) {
const oldSpaceDiff = ks.diffKState(oldSpaceKState, {

View file

@ -23,6 +23,8 @@ mixin discord(channel, radio=false)
!= icons.Icons.IconPhone
else if channel.type === 11 || channel.type === 12
!= icons.Icons.IconCollection
else if channel.type === 4
!= icons.Icons.IconCollection
else
include includes/hash.svg
.s-user-card--info.ws-nowrap
@ -288,6 +290,30 @@ block body
!= icons.Icons.IconMerge
= ` Link`
if space_id
h3.mt32.fs-category Manually link categories
form.d-flex.g16.ai-start(hx-post=rel("/api/link") hx-trigger="submit" hx-disabled-elt="input, button" hx-indicator="#link-button")
.fl-grow2.s-btn-group.fd-column.w40
each channel in unlinkedCategories
input.s-btn--radio(type="radio" name="discord" required id=channel.id value=channel.id)
label.s-btn.s-btn__muted.ta-left.truncate(for=channel.id)
+discord(channel, true, "Announcement")
else
.s-empty-state.p8 No Discord categories available.
.fl-grow1.s-btn-group.fd-column.w30
each room in unlinkedSubspaces
input.s-btn--radio(type="radio" name="matrix" required id=room.room_id value=room.room_id)
label.s-btn.s-btn__muted.ta-left.truncate(for=room.room_id)
+matrix(room, true)
else
.s-empty-state.p8 No Matrix subspaces available.
input(type="hidden" name="guild_id" value=guild_id)
input(type="hidden" name="type" value="category")
div
button.s-btn.s-btn__icon.s-btn__filled#link-button
!= icons.Icons.IconMerge
= ` Link`
h3.mt32.fs-category Unlink server
form.s-card.d-flex.gx16.ai-center(method="post" action=rel("/api/unlink-space"))
input(type="hidden" name="guild_id" value=guild.id)

View file

@ -99,6 +99,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
let channelIDs = discord.guildChannelMap.get(guild.id)
assert(channelIDs)
let linkedGuildSpaceID = select("guild_space", "space_id", {guild_id: guild.id}).pluck().get()
let linkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick", "room_mention_role"], {channel_id: channelIDs}).all()
let linkedChannelsWithDetails = linkedChannels.map(c => ({
// @ts-ignore
@ -112,6 +113,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
let unlinkedChannelIDs = channelIDs.filter(c => !linkedChannelIDs.includes(c))
/** @type {DiscordTypes.APIGuildChannel[]} */ // @ts-ignore
let unlinkedChannels = unlinkedChannelIDs.map(c => discord.channels.get(c))
let unlinkedCategories = dUtils.filterTo(unlinkedChannels, c => !c || c.type !== DiscordTypes.ChannelType.GuildCategory)
let removedWrongTypeChannels = dUtils.filterTo(unlinkedChannels, c => c && [0, 5].includes(c.type))
let removedPrivateChannels = dUtils.filterTo(unlinkedChannels, c => {
const permissions = dUtils.getPermissions(guild.id, roles, guild.roles, botID, c["permission_overwrites"])
@ -122,6 +124,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
let linkedRoomIDs = linkedChannels.map(c => c.room_id)
let unlinkedRooms = [...rooms]
let removedLinkedRooms = dUtils.filterTo(unlinkedRooms, r => !linkedRoomIDs.includes(r.room_id))
let unlinkedSubspaces = dUtils.filterTo(unlinkedRooms, r => r.room_id === linkedGuildSpaceID || r.room_type !== "m.space")
let removedWrongTypeRooms = dUtils.filterTo(unlinkedRooms, r => !r.room_type)
let removedEncryptedRooms = dUtils.filterTo(unlinkedRooms, r => !r.encryption && !r["im.nheko.summary.encryption"])
// https://discord.com/developers/docs/topics/threads#active-archived-threads
@ -129,7 +132,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
let removedArchivedThreadRooms = dUtils.filterTo(unlinkedRooms, r => r.name && !r.name.match(/^\[(🔒)?⛓️\]/))
return {
linkedChannelsWithDetails, unlinkedChannels, unlinkedRooms,
linkedChannelsWithDetails, unlinkedChannels, unlinkedCategories, unlinkedRooms, unlinkedSubspaces,
removedUncachedChannels, removedWrongTypeChannels, removedPrivateChannels, removedLinkedRooms, removedWrongTypeRooms, removedArchivedThreadRooms, removedEncryptedRooms
}
}

View file

@ -58,7 +58,8 @@ const schema = {
link: z.object({
guild_id: z.string(),
matrix: z.string(),
discord: z.string()
discord: z.string(),
type: z.enum(["category"]).optional()
}),
unlink: z.object({
guild_id: z.string(),
@ -175,20 +176,25 @@ as.router.post("/api/link", defineEventHandler(async event => {
const row = from("channel_room").select("channel_id", "room_id").and("WHERE channel_id = ? OR room_id = ?").get(channel.id, parsedBody.matrix)
if (row) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${row.channel_id} or room ID ${parsedBody.matrix} are already bridged and cannot be reused`})
const channelType = parsedBody.type
// Check room is part of the guild's space
let foundRoom = false
/** @type {string[]?} */
let foundVia = null
/** @type {string | null} */
let foundParentSpaceID = null
for await (const room of api.generateFullHierarchy(spaceID)) {
// When finding a space during iteration, look at space's children state, because we need a `via` to join the room (when we find it later)
for (const state of room.children_state) {
if (state.type === "m.space.child" && state.state_key === parsedBody.matrix) {
foundVia = state.content.via
foundParentSpaceID = room.room_id
}
}
// When finding a room during iteration, see if it was the requested room (to confirm that the room is in the space)
if (room.room_id === parsedBody.matrix && !room.room_type) {
if (room.room_id === parsedBody.matrix && (!channelType && !room.room_type || channelType === "category" && room.room_type === "m.space")) {
foundRoom = true
}
@ -221,7 +227,7 @@ as.router.post("/api/link", defineEventHandler(async event => {
const avatar = await api.getStateEvent(parsedBody.matrix, "m.room.avatar", "").then(content => content.url || null).catch(() => null)
const topic = await api.getStateEvent(parsedBody.matrix, "m.room.topic", "").then(content => content.topic || null).catch(() => null)
db.transaction(() => {
db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id, nick, custom_avatar, custom_topic) VALUES (?, ?, ?, ?, ?, ?, ?)").run(channel.id, parsedBody.matrix, channel.name, guildID, nick, avatar, topic)
db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id, nick, custom_avatar, custom_topic, parent_space_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)").run(channel.id, parsedBody.matrix, channel.name, guildID, channel.name === nick ? null : nick, avatar, topic, foundParentSpaceID)
db.prepare("INSERT INTO historical_channel_room (reference_channel_id, room_id, upgraded_timestamp) VALUES (?, ?, 0)").run(channel.id, parsedBody.matrix)
})()