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:
parent
034f8d6b55
commit
cf756cb0af
4 changed files with 20 additions and 12 deletions
|
@ -137,7 +137,7 @@ block body
|
|||
label.s-label.fl-grow1(for="autocreate")
|
||||
| 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.
|
||||
- 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.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
|
||||
|
|
|
@ -13,6 +13,7 @@ doctype html
|
|||
html(lang="en")
|
||||
head
|
||||
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="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"}')
|
||||
|
@ -52,7 +53,7 @@ html(lang="en")
|
|||
li(role="menuitem")
|
||||
a.s-topbar--item.s-user-card.d-flex.p4(href=`/guild?guild_id=${guild.id}`)
|
||||
+guild(guild)
|
||||
.mx-auto.w100.wmx9.py24#content
|
||||
.mx-auto.w100.wmx9.py24.px8#content
|
||||
block body
|
||||
script.
|
||||
document.querySelectorAll("[popovertarget]").forEach(e => {
|
||||
|
|
|
@ -9,6 +9,8 @@ const {LRUCache} = require("lru-cache")
|
|||
const {discord, as, sync, select} = require("../../passthrough")
|
||||
/** @type {import("../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")
|
||||
|
||||
/** @type {import("../../matrix/api")} */
|
||||
|
@ -71,20 +73,20 @@ as.router.post("/api/invite", defineEventHandler(async event => {
|
|||
}
|
||||
|
||||
// Check guild is bridged
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: guild_id}).pluck().get()
|
||||
if (!spaceID) throw createError({status: 428, message: "Server not bridged", data: "You can only invite Matrix users to servers that are bridged to Matrix."})
|
||||
const guild = discord.guilds.get(guild_id)
|
||||
assert(guild)
|
||||
const spaceID = await createSpace.ensureSpace(guild)
|
||||
|
||||
// Check for existing invite to the space
|
||||
let spaceMember
|
||||
try {
|
||||
spaceMember = await api.getStateEvent(spaceID, "m.room.member", parsedBody.mxid)
|
||||
} catch (e) {}
|
||||
if (spaceMember && (spaceMember.membership === "invite" || spaceMember.membership === "join")) {
|
||||
return sendRedirect(event, `/guild?guild_id=${guild_id}`, 302)
|
||||
}
|
||||
|
||||
// Invite
|
||||
await api.inviteToRoom(spaceID, parsedBody.mxid)
|
||||
if (!spaceMember || spaceMember.membership !== "invite" || spaceMember.membership !== "join") {
|
||||
// Invite
|
||||
await api.inviteToRoom(spaceID, parsedBody.mxid)
|
||||
}
|
||||
|
||||
// Permissions
|
||||
if (parsedBody.permissions === "moderator") {
|
||||
|
|
|
@ -7,7 +7,7 @@ const {SnowTransfer} = require("snowtransfer")
|
|||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const fetch = require("node-fetch")
|
||||
|
||||
const {as} = require("../../passthrough")
|
||||
const {as, db} = require("../../passthrough")
|
||||
const {id} = require("../../../addbot")
|
||||
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}`)
|
||||
try {
|
||||
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})
|
||||
} catch (e) {
|
||||
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) {
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue