From 714c31d230c013324f241fd32cf77cb0af238ffd Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 12 Sep 2023 20:43:56 +1200 Subject: [PATCH] Fix my homeserver being hard-coded --- d2m/discord-command-handler.js | 4 +++- m2d/converters/utils.js | 2 +- matrix/mreq.js | 2 +- readme.md | 8 ++++---- registration.example.yaml | 1 + scripts/seed.js | 6 ++++-- types.d.ts | 1 + 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/d2m/discord-command-handler.js b/d2m/discord-command-handler.js index 1bd52c8a..fa59822d 100644 --- a/d2m/discord-command-handler.js +++ b/d2m/discord-command-handler.js @@ -3,6 +3,8 @@ const assert = require("assert").strict const util = require("util") const DiscordTypes = require("discord-api-types/v10") +const reg = require("../matrix/read-registration") + const {discord, sync, db} = require("../passthrough") /** @type {import("../matrix/api")}) */ const api = sync.require("../matrix/api") @@ -86,7 +88,7 @@ const commands = [{ const avatarEvent = await api.getStateEvent(roomID, "m.room.avatar", "") const avatarURLParts = avatarEvent?.url.match(/^mxc:\/\/([^/]+)\/(\w+)$/) let currentAvatarMessage = - ( avatarURLParts ? `Current room-specific avatar: https://matrix.cadence.moe/_matrix/media/r0/download/${avatarURLParts[1]}/${avatarURLParts[2]}` + ( avatarURLParts ? `Current room-specific avatar: ${reg.ooye.server_origin}/_matrix/media/r0/download/${avatarURLParts[1]}/${avatarURLParts[2]}` : "No avatar. Now's your time to strike. Use `//icon` again with a link or upload to set the room-specific avatar.") // Next potential avatar diff --git a/m2d/converters/utils.js b/m2d/converters/utils.js index 02ec1478..96892521 100644 --- a/m2d/converters/utils.js +++ b/m2d/converters/utils.js @@ -25,7 +25,7 @@ function eventSenderIsFromDiscord(sender) { */ function getPublicUrlForMxc(mxc) { const avatarURLParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/) - if (avatarURLParts) return `https://matrix.cadence.moe/_matrix/media/r0/download/${avatarURLParts[1]}/${avatarURLParts[2]}` + if (avatarURLParts) return `${reg.ooye.server_origin}/_matrix/media/r0/download/${avatarURLParts[1]}/${avatarURLParts[2]}` else return null } diff --git a/matrix/mreq.js b/matrix/mreq.js index d5ffebad..4291ede8 100644 --- a/matrix/mreq.js +++ b/matrix/mreq.js @@ -8,7 +8,7 @@ const { sync } = passthrough /** @type {import("./read-registration")} */ const reg = sync.require("./read-registration.js") -const baseUrl = "https://matrix.cadence.moe/_matrix" +const baseUrl = `${reg.ooye.server_origin}/_matrix` class MatrixServerError extends Error { constructor(data, opts) { diff --git a/readme.md b/readme.md index 58528f43..0ad010e0 100644 --- a/readme.md +++ b/readme.md @@ -51,15 +51,15 @@ Most features you'd expect in both directions, plus a little extra spice: Node.js version 18 or later is required: https://nodejs.org/en/download/releases (the matrix-appservice dependency demands 18) -Install dependencies: `npm install --save-dev` +Install dependencies: `npm install --save-dev` (only need --save-dev if you will run the automated tests) Copy `config.example.js` to `config.js` and fill in Discord token. -Copy `registration.example.yaml` to `registration.yaml` and fill in bracketed values. Register it in Synapse's `homeserver.yaml` through the usual appservice installation process, then restart Synapse. +Copy `registration.example.yaml` to `registration.yaml` and fill in bracketed values. You could generate each hex string with `dd if=/dev/urandom bs=32 count=1 2> /dev/null | basenc --base16 | dd conv=lcase 2> /dev/null`. Register the registration in Synapse's `homeserver.yaml` through the usual appservice installation process, then restart Synapse. -If developing on a different computer to the one running the homeserver, use SSH port forwarding so that Synapse can connect on its `localhost:6693` to reach the running bridge on your computer. Example: `ssh -T -v -R 6693:localhost:6693 username@matrix.cadence.moe` +If developing on a different computer to the one running the homeserver, use SSH port forwarding so that Synapse can connect on its `localhost:6693` to reach the running bridge on your computer. Example: `ssh -T -v -R 6693:localhost:6693 me@matrix.cadence.moe` -Run `node scripts/seed.js` to check your setup, then create the database and server state (only need to run this once ever) +Run `node scripts/seed.js` to check your setup, create the database and server state (only need to run this once ever) Make sure the tests work: `npm t` diff --git a/registration.example.yaml b/registration.example.yaml index 89ce4e05..802d7430 100644 --- a/registration.example.yaml +++ b/registration.example.yaml @@ -17,3 +17,4 @@ ooye: namespace_prefix: _ooye_ max_file_size: 5000000 server_name: [the part after the colon in your matrix id, like cadence.moe] + server_origin: [the full protocol and domain of your actual matrix server's location, with no trailing slash, like https://matrix.cadence.moe] diff --git a/scripts/seed.js b/scripts/seed.js index be2d5f37..e0bee4d3 100644 --- a/scripts/seed.js +++ b/scripts/seed.js @@ -22,8 +22,10 @@ const utils = require("../m2d/converters/utils") const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}` // ensure registration is correctly set... - assert(reg.sender_localpart.startsWith(reg.ooye.namespace_prefix)) - assert(utils.eventSenderIsFromDiscord(mxid)) + assert(reg.sender_localpart.startsWith(reg.ooye.namespace_prefix)) // appservice's localpart must be in the namespace it controls + assert(utils.eventSenderIsFromDiscord(mxid)) // appservice's mxid must be in the namespace it controls + assert(reg.ooye.server_origin.match(/^https?:\/\//)) // must start with http or https + assert.notEqual(reg.ooye.server_origin.slice(-1), "/") // must not end in slash // database ddl... db.exec(fs.readFileSync("db/ooye-schema.sql", "utf8")) diff --git a/types.d.ts b/types.d.ts index 8d40372d..e3a99091 100644 --- a/types.d.ts +++ b/types.d.ts @@ -20,6 +20,7 @@ export type AppServiceRegistrationConfig = { namespace_prefix: string max_file_size: number server_name: string + server_origin: string } old_bridge?: { as_token: string