1
0
Fork 0

send sim messages to the proper rooms

This commit is contained in:
Cadence Ember 2023-05-09 15:29:46 +12:00
parent 7526d63690
commit da6603d258
10 changed files with 38 additions and 10 deletions

View file

@ -24,10 +24,18 @@ async function createSim(user) {
const mxid = "@" + localpart + ":cadence.moe"
// Save chosen name in the database forever
// Making this database change right away so that in a concurrent registration, the 2nd registration will already have generated a different localpart because it can see this row when it generates
db.prepare("INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run(user.id, simName, localpart, mxid)
// Register matrix user with that name
await api.register(localpart)
try {
await api.register(localpart)
} catch (e) {
// If user creation fails, manually undo the database change. Still isn't perfect, but should help.
// (A transaction would be preferable, but I don't think it's safe to leave transaction open across event loop ticks.)
db.prepare("DELETE FROM sim WHERE discord_id = ?").run(user.id)
throw e
}
return mxid
}

View file

@ -28,4 +28,4 @@ async function sendMessage(message) {
return eventID
}
module.exports.sendMessage = sendMessage
module.exports.sendMessage = sendMessage

View file

@ -44,6 +44,7 @@ function* generateLocalpartAlternatives(preferences) {
/**
* Whole process for checking the database and generating the right sim name.
* It is very important this is not an async function: once the name has been chosen, the calling function should be able to immediately claim that name into the database in the same event loop tick.
* @param {import("discord-api-types/v10").APIUser} user
* @returns {string}
*/

View file

@ -31,3 +31,7 @@ test("user2name: adds number suffix if name is unavailable (new username format)
test("user2name: uses ID if name becomes too short", t => {
t.equal(userToSimName({username: "f***", discriminator: "0001", id: "9"}), "9")
})
test("user2name: uses ID when name has only disallowed characters", t => {
t.equal(userToSimName({username: "!@#$%^&*", discriminator: "0001", id: "9"}), "9")
})

View file

@ -6,15 +6,16 @@ const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../passthrough")
const { sync } = passthrough
/** @type {typeof import("./event-dispatcher")} */
const eventDispatcher = sync.require("./event-dispatcher")
const utils = {
/**
* @param {import("./discord-client")} client
* @param {import("cloudstorm").IGatewayMessage} message
*/
onPacket(client, message) {
// requiring this later so that the client is already constructed by the time event-dispatcher is loaded
/** @type {typeof import("./event-dispatcher")} */
const eventDispatcher = sync.require("./event-dispatcher")
if (message.t === "READY") {
if (client.ready) return
client.ready = true

View file

@ -2,9 +2,6 @@
const {sync} = require("../passthrough")
/** @type {import("./actions/create-space")}) */
const createSpace = sync.require("./actions/create-space")
/** @type {import("./actions/send-message")}) */
const sendMessage = sync.require("./actions/send-message")