From b9ec28a5ad688a458688511e1303522006f4213b Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 25 Nov 2023 22:26:26 +1300 Subject: [PATCH] Handle user creation for Clyde AI --- d2m/converters/user-to-mxid.js | 13 ++++++++++--- d2m/converters/user-to-mxid.test.js | 5 +++++ test/data.js | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/d2m/converters/user-to-mxid.js b/d2m/converters/user-to-mxid.js index 35a3b5e..4d53a9c 100644 --- a/d2m/converters/user-to-mxid.js +++ b/d2m/converters/user-to-mxid.js @@ -5,6 +5,10 @@ const assert = require("assert") const passthrough = require("../../passthrough") const {select} = passthrough +const SPECIAL_USER_MAPPINGS = new Map([ + ["1081004946872352958", ["clyde_ai", "clyde"]] +]) + /** * Downcased and stripped username. Can only include a basic set of characters. * https://spec.matrix.org/v1.6/appendices/#user-identifiers @@ -30,7 +34,7 @@ function downcaseUsername(user) { /** @param {string[]} preferences */ function* generateLocalpartAlternatives(preferences) { const best = preferences[0] - assert.ok(best) + assert(best) // First, suggest the preferences... for (const localpart of preferences) { yield localpart @@ -50,15 +54,18 @@ function* generateLocalpartAlternatives(preferences) { * @returns {string} */ function userToSimName(user) { - assert.notEqual(user.discriminator, "0000", "cannot create user for a webhook") + if (!SPECIAL_USER_MAPPINGS.has(user.id)) { // skip this check for known special users + assert.notEqual(user.discriminator, "0000", `cannot create user for a webhook: ${JSON.stringify(user)}`) + } // 1. Is sim user already registered? const existing = select("sim", "sim_name", {user_id: user.id}).pluck().get() if (existing) return existing // 2. Register based on username (could be new or old format) + // (Unless it's a special user, in which case copy their provided mappings.) const downcased = downcaseUsername(user) - const preferences = [downcased] + const preferences = SPECIAL_USER_MAPPINGS.get(user.id) || [downcased] if (user.discriminator.length === 4) { // Old style tag? If user.username is unavailable, try the full tag next preferences.push(downcased + user.discriminator) } diff --git a/d2m/converters/user-to-mxid.test.js b/d2m/converters/user-to-mxid.test.js index e709473..b080115 100644 --- a/d2m/converters/user-to-mxid.test.js +++ b/d2m/converters/user-to-mxid.test.js @@ -1,6 +1,7 @@ const {test} = require("supertape") const tryToCatch = require("try-to-catch") const assert = require("assert") +const data = require("../../test/data") const {userToSimName} = require("./user-to-mxid") test("user2name: cannot create user for a webhook", async t => { @@ -39,3 +40,7 @@ test("user2name: uses ID if name becomes too short", t => { test("user2name: uses ID when name has only disallowed characters", t => { t.equal(userToSimName({username: "!@#$%^&*", discriminator: "0001", id: "9"}), "9") }) + +test("user2name: works on special user", t => { + t.equal(userToSimName(data.user.clyde_ai), "clyde_ai") +}) diff --git a/test/data.js b/test/data.js index 648c2c8..3e78802 100644 --- a/test/data.js +++ b/test/data.js @@ -183,6 +183,23 @@ module.exports = { safety_alerts_channel_id: null } }, + user: { + clyde_ai: { + id: "1081004946872352958", + username: "clyde", + avatar: "a_6170487d32fdfe9f988720ad80e6ab8c", + discriminator: "0000", + public_flags: 0, + premium_type: 2, + flags: 0, + bot: true, + banner: null, + accent_color: null, + global_name: "Clyde", + avatar_decoration_data: null, + banner_color: null + } + }, member: { kumaccino: { avatar: null,