Handle more guild page situations

This commit is contained in:
Cadence Ember 2024-12-02 12:43:00 +13:00
parent bded9296af
commit a35860cb15

View file

@ -5,6 +5,7 @@ const {z} = require("zod")
const {defineEventHandler, sendRedirect, useSession, createError, getValidatedQuery, readValidatedBody} = require("h3") const {defineEventHandler, sendRedirect, useSession, createError, getValidatedQuery, readValidatedBody} = require("h3")
const {randomUUID} = require("crypto") const {randomUUID} = require("crypto")
const {LRUCache} = require("lru-cache") const {LRUCache} = require("lru-cache")
const Ty = require("../../types")
const {discord, as, sync, select} = require("../../passthrough") const {discord, as, sync, select} = require("../../passthrough")
/** @type {import("../pug-sync")} */ /** @type {import("../pug-sync")} */
@ -34,6 +35,10 @@ const schema = {
/** @type {LRUCache<string, string>} nonce to guild id */ /** @type {LRUCache<string, string>} nonce to guild id */
const validNonce = new LRUCache({max: 200}) const validNonce = new LRUCache({max: 200})
/**
* @param {string} guildID
* @param {Ty.R.Hierarchy[]} rooms
*/
function getChannelRoomsLinks(guildID, rooms) { function getChannelRoomsLinks(guildID, rooms) {
function getPosition(channel) { function getPosition(channel) {
let position = 0 let position = 0
@ -62,7 +67,7 @@ function getChannelRoomsLinks(guildID, rooms) {
let unlinkedRooms = rooms.filter(r => !linkedRoomIDs.includes(r.room_id) && !r.room_type) let unlinkedRooms = rooms.filter(r => !linkedRoomIDs.includes(r.room_id) && !r.room_type)
// https://discord.com/developers/docs/topics/threads#active-archived-threads // https://discord.com/developers/docs/topics/threads#active-archived-threads
// need to filter out linked archived threads from unlinkedRooms, will just do that by comparing against the name // need to filter out linked archived threads from unlinkedRooms, will just do that by comparing against the name
unlinkedRooms = unlinkedRooms.filter(r => !r.name.match(/^\[(🔒)?⛓️\]/)) unlinkedRooms = unlinkedRooms.filter(r => r.name && !r.name.match(/^\[(🔒)?⛓️\]/))
return {linkedChannelsWithDetails, unlinkedChannels, unlinkedRooms} return {linkedChannelsWithDetails, unlinkedChannels, unlinkedRooms}
} }
@ -71,18 +76,27 @@ as.router.get("/guild", defineEventHandler(async event => {
const {guild_id} = await getValidatedQuery(event, schema.guild.parse) const {guild_id} = await getValidatedQuery(event, schema.guild.parse)
const session = await useSession(event, {password: reg.as_token}) const session = await useSession(event, {password: reg.as_token})
const row = select("guild_space", ["space_id", "privacy_level"], {guild_id}).get() const row = select("guild_space", ["space_id", "privacy_level"], {guild_id}).get()
if (!guild_id || !row || !discord.guilds.has(guild_id) || !session.data.managedGuilds || !session.data.managedGuilds.includes(guild_id)) {
const links = getChannelRoomsLinks(guild_id, []) // Permission problems
return pugSync.render(event, "guild.pug", {guild_id, ...links}) if (!guild_id || !discord.guilds.has(guild_id) || !session.data.managedGuilds || !session.data.managedGuilds.includes(guild_id)) {
return pugSync.render(event, "guild.pug", {guild_id})
} }
const nonce = randomUUID() const nonce = randomUUID()
validNonce.set(nonce, guild_id) validNonce.set(nonce, guild_id)
// Unlinked guild
if (!row) {
const links = getChannelRoomsLinks(guild_id, [])
return pugSync.render(event, "guild.pug", {guild_id, nonce, ...links})
}
// Linked guild
const mods = await api.getStateEvent(row.space_id, "m.room.power_levels", "") const mods = await api.getStateEvent(row.space_id, "m.room.power_levels", "")
const banned = await api.getMembers(row.space_id, "ban") const banned = await api.getMembers(row.space_id, "ban")
const rooms = await api.getFullHierarchy(row.space_id) const rooms = await api.getFullHierarchy(row.space_id)
const links = getChannelRoomsLinks(guild_id, rooms) const links = getChannelRoomsLinks(guild_id, rooms)
return pugSync.render(event, "guild.pug", {guild_id, nonce, mods, banned, rooms, ...links, ...row}) return pugSync.render(event, "guild.pug", {guild_id, nonce, mods, banned, ...links, ...row})
})) }))
as.router.get("/invite", defineEventHandler(async event => { as.router.get("/invite", defineEventHandler(async event => {