From 541c5e1eb524e50c4bbe319e995a9718df4a37a7 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 21 Jul 2024 14:56:58 +1200 Subject: [PATCH] Add include_user_id_in_mxid setting --- d2m/converters/user-to-mxid.js | 5 +++++ d2m/converters/user-to-mxid.test.js | 8 ++++++++ registration.example.yaml | 1 + types.d.ts | 1 + 4 files changed, 15 insertions(+) diff --git a/d2m/converters/user-to-mxid.js b/d2m/converters/user-to-mxid.js index 6b3bbbd..97b655e 100644 --- a/d2m/converters/user-to-mxid.js +++ b/d2m/converters/user-to-mxid.js @@ -1,6 +1,7 @@ // @ts-check const assert = require("assert") +const registration = require("../../matrix/read-registration") const passthrough = require("../../passthrough") const {select} = passthrough @@ -24,6 +25,10 @@ function downcaseUsername(user) { .replace(/[^a-z0-9._=/-]*/g, "") // remove leading and trailing dashes and underscores... .replace(/(?:^[_-]*|[_-]*$)/g, "") + // If requested, also make the Discord user ID part of the username + if (registration.ooye.include_user_id_in_mxid) { + downcased = user.id + "_" + downcased + } // The new length must be at least 2 characters (in other words, it should have some content) if (downcased.length < 2) { downcased = user.id diff --git a/d2m/converters/user-to-mxid.test.js b/d2m/converters/user-to-mxid.test.js index b080115..4576205 100644 --- a/d2m/converters/user-to-mxid.test.js +++ b/d2m/converters/user-to-mxid.test.js @@ -44,3 +44,11 @@ test("user2name: uses ID when name has only disallowed characters", t => { test("user2name: works on special user", t => { t.equal(userToSimName(data.user.clyde_ai), "clyde_ai") }) + +test("user2name: includes ID if requested in config", t => { + const reg = require("../../matrix/read-registration") + reg.ooye.include_user_id_in_mxid = true + t.equal(userToSimName({username: "Harry Styles!", discriminator: "0001", id: "123456"}), "123456_harry_styles") + t.equal(userToSimName({username: "f***", discriminator: "0001", id: "123456"}), "123456_f") + reg.ooye.include_user_id_in_mxid = false +}) diff --git a/registration.example.yaml b/registration.example.yaml index dd217db..d38f5ae 100644 --- a/registration.example.yaml +++ b/registration.example.yaml @@ -19,6 +19,7 @@ ooye: 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] content_length_workaround: false + include_user_id_in_mxid: false invite: # uncomment this to auto-invite the named user to newly created spaces and mark them as admin (PL 100) everywhere # - '@cadence:cadence.moe' diff --git a/types.d.ts b/types.d.ts index 68430d9..6036867 100644 --- a/types.d.ts +++ b/types.d.ts @@ -22,6 +22,7 @@ export type AppServiceRegistrationConfig = { server_name: string server_origin: string content_length_workaround: boolean + include_user_id_in_mxid: boolean invite: string[] } old_bridge?: {