Handle user creation for Clyde AI
This commit is contained in:
		| @@ -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) | ||||
| 	} | ||||
|   | ||||
| @@ -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") | ||||
| }) | ||||
|   | ||||
							
								
								
									
										17
									
								
								test/data.js
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Cadence Ember
					Cadence Ember