Create space as needed in oauth flow

I have manually tested this with both web flows, the link flow, the
/invite command, and the toggle switch, and they all work.
This commit is contained in:
Cadence Ember 2024-09-29 03:58:51 +13:00
parent 034f8d6b55
commit cf756cb0af
4 changed files with 20 additions and 12 deletions

View file

@ -137,7 +137,7 @@ block body
label.s-label.fl-grow1(for="autocreate") label.s-label.fl-grow1(for="autocreate")
| Create new Matrix rooms automatically | Create new Matrix rooms automatically
p.s-description If you want, OOYE can automatically create new Matrix rooms and link them when an unlinked Discord channel is spoken in. p.s-description If you want, OOYE can automatically create new Matrix rooms and link them when an unlinked Discord channel is spoken in.
- let value = select("guild_active", "autocreate", {guild_id}).pluck().get() - let value = !!select("guild_active", "autocreate", {guild_id}).pluck().get()
input(type="hidden" name="guild_id" value=guild_id) input(type="hidden" name="guild_id" value=guild_id)
input.s-toggle-switch.order-last#autocreate(name="autocreate" type="checkbox" hx-post="/api/autocreate" hx-indicator="#autocreate-loading" hx-disabled-elt="this" hx-swap="none" checked=value) input.s-toggle-switch.order-last#autocreate(name="autocreate" type="checkbox" hx-post="/api/autocreate" hx-indicator="#autocreate-loading" hx-disabled-elt="this" hx-swap="none" checked=value)
.is-loading#autocreate-loading .is-loading#autocreate-loading

View file

@ -13,6 +13,7 @@ doctype html
html(lang="en") html(lang="en")
head head
title Out Of Your Element title Out Of Your Element
<meta name="viewport" content="width=device-width, initial-scale=1">
link(rel="stylesheet" type="text/css" href="/static/stacks.min.css") link(rel="stylesheet" type="text/css" href="/static/stacks.min.css")
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 80%22><text y=%22.83em%22 font-size=%2283%22>💬</text></svg>"> <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 80%22><text y=%22.83em%22 font-size=%2283%22>💬</text></svg>">
meta(name="htmx-config" content='{"indicatorClass":"is-loading"}') meta(name="htmx-config" content='{"indicatorClass":"is-loading"}')
@ -52,7 +53,7 @@ html(lang="en")
li(role="menuitem") li(role="menuitem")
a.s-topbar--item.s-user-card.d-flex.p4(href=`/guild?guild_id=${guild.id}`) a.s-topbar--item.s-user-card.d-flex.p4(href=`/guild?guild_id=${guild.id}`)
+guild(guild) +guild(guild)
.mx-auto.w100.wmx9.py24#content .mx-auto.w100.wmx9.py24.px8#content
block body block body
script. script.
document.querySelectorAll("[popovertarget]").forEach(e => { document.querySelectorAll("[popovertarget]").forEach(e => {

View file

@ -9,6 +9,8 @@ const {LRUCache} = require("lru-cache")
const {discord, as, sync, select} = require("../../passthrough") const {discord, as, sync, select} = require("../../passthrough")
/** @type {import("../pug-sync")} */ /** @type {import("../pug-sync")} */
const pugSync = sync.require("../pug-sync") const pugSync = sync.require("../pug-sync")
/** @type {import("../../d2m/actions/create-space")} */
const createSpace = sync.require("../../d2m/actions/create-space")
const {reg} = require("../../matrix/read-registration") const {reg} = require("../../matrix/read-registration")
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
@ -71,20 +73,20 @@ as.router.post("/api/invite", defineEventHandler(async event => {
} }
// Check guild is bridged // Check guild is bridged
const spaceID = select("guild_space", "space_id", {guild_id: guild_id}).pluck().get() const guild = discord.guilds.get(guild_id)
if (!spaceID) throw createError({status: 428, message: "Server not bridged", data: "You can only invite Matrix users to servers that are bridged to Matrix."}) assert(guild)
const spaceID = await createSpace.ensureSpace(guild)
// Check for existing invite to the space // Check for existing invite to the space
let spaceMember let spaceMember
try { try {
spaceMember = await api.getStateEvent(spaceID, "m.room.member", parsedBody.mxid) spaceMember = await api.getStateEvent(spaceID, "m.room.member", parsedBody.mxid)
} catch (e) {} } catch (e) {}
if (spaceMember && (spaceMember.membership === "invite" || spaceMember.membership === "join")) {
return sendRedirect(event, `/guild?guild_id=${guild_id}`, 302)
}
if (!spaceMember || spaceMember.membership !== "invite" || spaceMember.membership !== "join") {
// Invite // Invite
await api.inviteToRoom(spaceID, parsedBody.mxid) await api.inviteToRoom(spaceID, parsedBody.mxid)
}
// Permissions // Permissions
if (parsedBody.permissions === "moderator") { if (parsedBody.permissions === "moderator") {

View file

@ -7,7 +7,7 @@ const {SnowTransfer} = require("snowtransfer")
const DiscordTypes = require("discord-api-types/v10") const DiscordTypes = require("discord-api-types/v10")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const {as} = require("../../passthrough") const {as, db} = require("../../passthrough")
const {id} = require("../../../addbot") const {id} = require("../../../addbot")
const {reg} = require("../../matrix/read-registration") const {reg} = require("../../matrix/read-registration")
@ -77,14 +77,19 @@ as.router.get("/oauth", defineEventHandler(async event => {
const client = new SnowTransfer(`Bearer ${parsedToken.data.access_token}`) const client = new SnowTransfer(`Bearer ${parsedToken.data.access_token}`)
try { try {
const guilds = await client.user.getGuilds() const guilds = await client.user.getGuilds()
const managedGuilds = guilds.filter(g => BigInt(g.permissions) & DiscordTypes.PermissionFlagsBits.ManageGuild).map(g => g.id) var managedGuilds = guilds.filter(g => BigInt(g.permissions) & DiscordTypes.PermissionFlagsBits.ManageGuild).map(g => g.id)
await session.update({managedGuilds}) await session.update({managedGuilds})
} catch (e) { } catch (e) {
throw createError({status: 502, message: "API call failed", data: e.message}) throw createError({status: 502, message: "API call failed", data: e.message})
} }
// Set auto-create for the guild
// @ts-ignore
if (managedGuilds.includes(parsedQuery.data.guild_id)) {
db.prepare("INSERT OR IGNORE INTO guild_active (guild_id, autocreate) VALUES (?, ?)").run(parsedQuery.data.guild_id, +!session.data.selfService)
}
if (parsedQuery.data.guild_id) { if (parsedQuery.data.guild_id) {
// TODO: we probably need to create a matrix space and database entry immediately here so that self-service settings apply and so matrix users can be invited
return sendRedirect(event, `/guild?guild_id=${parsedQuery.data.guild_id}`, 302) return sendRedirect(event, `/guild?guild_id=${parsedQuery.data.guild_id}`, 302)
} }