Compare commits

...

7 commits

Author SHA1 Message Date
cf756cb0af 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.
2024-09-29 03:58:51 +13:00
034f8d6b55 Code coverage reporting 2024-09-29 03:27:40 +13:00
0e6e5e61e4 Merge branch 'v3.0-beta3' 2024-09-29 03:13:10 +13:00
bac2deb32f Check existsOrAutocreatable before dispatching 2024-09-29 03:11:59 +13:00
d629e666db Fis messages being double-redacted 2024-09-29 00:21:48 +12:00
d6de57f0c3 Factor out namespace_prefix 2024-09-26 02:16:50 +12:00
734c9a5838 Remove hardcoded cadence.moe 2024-09-25 13:37:54 +12:00
14 changed files with 52 additions and 29 deletions

View file

@ -65,6 +65,6 @@
"addbot": "node addbot.js",
"test": "cross-env FORCE_COLOR=true supertape --no-check-assertions-count --format tap test/test.js | tap-dot",
"test-slow": "cross-env FORCE_COLOR=true supertape --no-check-assertions-count --format tap --no-worker test/test.js -- --slow | tap-dot",
"cover": "c8 -o test/coverage --skip-full -x db/migrations -x matrix/file.js -x matrix/api.js -x matrix/mreq.js -x d2m/converters/rlottie-wasm.js -r html -r text supertape --no-check-assertions-count --format fail --no-worker test/test.js -- --slow"
"cover": "c8 -o test/coverage --skip-full -x db/migrations -x src/matrix/file.js -x src/matrix/api.js -x src/matrix/mreq.js -x src/d2m/converters/rlottie-wasm.js -r html -r text supertape --no-check-assertions-count --format fail --no-worker test/test.js -- --slow"
}
}

View file

@ -105,7 +105,8 @@ async function validateHomeserverOrigin(serverUrlPrompt, url) {
const serverNameResponse = await prompt({
type: "input",
name: "server_name",
message: "Homeserver name"
message: "Homeserver name",
validate: serverName => !!serverName.match(/[a-z][a-z.]+[a-z]/)
})
console.log("What is the URL of your homeserver?")
@ -176,7 +177,7 @@ async function validateHomeserverOrigin(serverUrlPrompt, url) {
message: "Client secret"
})
const template = getTemplateRegistration()
const template = getTemplateRegistration(serverNameResponse.server_name)
reg = {
...template,
url: bridgeOriginResponse.bridge_origin,

View file

@ -504,5 +504,5 @@ module.exports.postApplyPowerLevels = postApplyPowerLevels
module.exports._convertNameAndTopic = convertNameAndTopic
module.exports._unbridgeRoom = _unbridgeRoom
module.exports.unbridgeDeletedChannel = unbridgeDeletedChannel
module.exports.assertExistsOrAutocreatable = assertExistsOrAutocreatable
module.exports.existsOrAutocreatable = existsOrAutocreatable
module.exports.assertExistsOrAutocreatable = assertExistsOrAutocreatable

View file

@ -191,7 +191,7 @@ module.exports = {
async onThreadCreate(client, thread) {
const channelID = thread.parent_id || undefined
const parentRoomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel (won't autocreate)
const threadRoomID = await createRoom.syncRoom(thread.id) // Create room (will share the same inflight as the initial message to the thread)
await announceThread.announceThread(parentRoomID, threadRoomID, thread)
},
@ -249,6 +249,7 @@ module.exports = {
if (message.author.username === "Deleted User") return // Nothing we can do for deleted users.
const channel = client.channels.get(message.channel_id)
if (!channel || !("guild_id" in channel) || !channel.guild_id) return // Nothing we can do in direct messages.
const guild = client.guilds.get(channel.guild_id)
assert(guild)
@ -259,11 +260,13 @@ module.exports = {
if (dUtils.isEphemeralMessage(message)) return // Ephemeral messages are for the eyes of the receiver only!
if (!createRoom.existsOrAutocreatable(channel, guild.id)) return // Check that the sending-to room exists or is autocreatable
const {affected, row} = await speedbump.maybeDoSpeedbump(message.channel_id, message.id)
if (affected) return
// @ts-ignore
await sendMessage.sendMessage(message, channel, guild, row),
await sendMessage.sendMessage(message, channel, guild, row)
retrigger.messageFinishedBridging(message.id)
},
@ -278,7 +281,7 @@ module.exports = {
// Otherwise, if there are embeds, then the system generated URL preview embeds.
if (!(typeof data.content === "string" || "embeds" in data)) return
// Deal with Eventual Consistency(TM)
// Check that the sending-to room exists, and deal with Eventual Consistency(TM)
if (retrigger.eventNotFoundThenRetrigger(data.id, module.exports.onMessageUpdate, client, data)) return
if (data.webhook_id) {
@ -295,11 +298,11 @@ module.exports = {
/** @type {DiscordTypes.GatewayMessageCreateDispatchData} */
// @ts-ignore
const message = data
const channel = client.channels.get(message.channel_id)
if (!channel || !("guild_id" in channel) || !channel.guild_id) return // Nothing we can do in direct messages.
const guild = client.guilds.get(channel.guild_id)
assert(guild)
// @ts-ignore
await editMessage.editMessage(message, guild, row)
},

View file

@ -3,6 +3,7 @@ module.exports = async function(db) {
const contents = db.prepare("SELECT distinct hashed_profile_content FROM sim_member WHERE hashed_profile_content IS NOT NULL").pluck().all()
const stmt = db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE hashed_profile_content = ?")
db.transaction(() => {
/* c8 ignore next 6 */
for (let s of contents) {
let b = Buffer.isBuffer(s) ? Uint8Array.from(s) : Uint8Array.from(Buffer.from(s))
const unsignedHash = hasher.h64Raw(b)

View file

@ -124,3 +124,4 @@ export type PickTypeOf<T, K extends AllKeys<T>> = T extends { [k in K]?: any } ?
export type Merge<U> = {[x in AllKeys<U>]: PickTypeOf<U, x>}
export type Nullable<T> = {[k in keyof T]: T[k] | null}
export type Numberish<T> = {[k in keyof T]: T[k] extends number ? (number | bigint) : T[k]}
export type ValueOrArray<T> = {[k in keyof T]: T[k][] | T[k]}

View file

@ -8,7 +8,7 @@ const U = require("./orm-defs")
* @template {keyof U.Models[Table]} Col
* @param {Table} table
* @param {Col[] | Col} cols
* @param {Partial<U.Numberish<U.Models[Table]>>} where
* @param {Partial<U.ValueOrArray<U.Numberish<U.Models[Table]>>>} where
* @param {string} [e]
*/
function select(table, cols, where = {}, e = "") {

View file

@ -30,6 +30,11 @@ test("orm: select: all, where and pluck works on multiple columns", t => {
t.deepEqual(names, ["cadence [they]"])
})
test("orm: select: in array works", t => {
const ids = select("emoji", "emoji_id", {name: ["online", "upstinky"]}).pluck().all()
t.deepEqual(ids, ["288858540888686602", "606664341298872324"])
})
test("orm: from: get pluck works", t => {
const guildID = from("guild_space").pluck("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
t.equal(guildID, data.guild.general.id)

View file

@ -13,7 +13,7 @@ const utils = sync.require("../converters/utils")
*/
async function deleteMessage(event) {
const rows = from("event_message").join("message_channel", "message_id").select("channel_id", "message_id").where({event_id: event.redacts}).all()
db.prepare("DELETE FROM event_message WHERE event_id = ?").run(event.event_id)
db.prepare("DELETE FROM event_message WHERE event_id = ?").run(event.redacts)
for (const row of rows) {
db.prepare("DELETE FROM message_channel WHERE message_id = ?").run(row.message_id)
await discord.snow.channel.deleteMessage(row.channel_id, row.message_id, event.content.reason)

View file

@ -24,8 +24,12 @@ function writeRegistration(reg) {
fs.writeFileSync(registrationFilePath, JSON.stringify(reg, null, 2))
}
/** @returns {import("../types").InitialAppServiceRegistrationConfig} reg */
function getTemplateRegistration() {
/**
* @param {string} serverName
* @returns {import("../types").InitialAppServiceRegistrationConfig} reg
*/
function getTemplateRegistration(serverName) {
const namespace_prefix = "_ooye_"
return {
id: "ooye",
as_token: crypto.randomBytes(32).toString("hex"),
@ -33,21 +37,21 @@ function getTemplateRegistration() {
namespaces: {
users: [{
exclusive: true,
regex: "@_ooye_.*:cadence.moe"
regex: `@${namespace_prefix}.*:${serverName}`
}],
aliases: [{
exclusive: true,
regex: "#_ooye_.*:cadence.moe"
regex: `#${namespace_prefix}.*:${serverName}`
}]
},
protocols: [
"discord"
],
sender_localpart: "_ooye_bot",
sender_localpart: `${namespace_prefix}bot`,
rate_limited: false,
socket: 6693,
ooye: {
namespace_prefix: "_ooye_",
namespace_prefix,
max_file_size: 5000000,
content_length_workaround: false,
include_user_id_in_mxid: false,

View file

@ -136,8 +136,8 @@ block body
form.d-flex.ai-center.g8
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 a new Discord channel is spoken in.
- let value = select("guild_active", "autocreate", {guild_id}).pluck().get()
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()
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

View file

@ -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 => {

View file

@ -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)
}
if (!spaceMember || spaceMember.membership !== "invite" || spaceMember.membership !== "join") {
// Invite
await api.inviteToRoom(spaceID, parsedBody.mxid)
}
// Permissions
if (parsedBody.permissions === "moderator") {

View file

@ -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)
}