register -> invite -> join -> send flow
This commit is contained in:
		
							parent
							
								
									3bc29def41
								
							
						
					
					
						commit
						1e7e66dc31
					
				
					 11 changed files with 150 additions and 34 deletions
				
			
		| 
						 | 
				
			
			@ -8,8 +8,8 @@ const api = sync.require("../../matrix/api")
 | 
			
		|||
/**
 | 
			
		||||
 * @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild
 | 
			
		||||
 */
 | 
			
		||||
function createSpace(guild) {
 | 
			
		||||
	return api.createRoom({
 | 
			
		||||
async function createSpace(guild) {
 | 
			
		||||
	const roomID = api.createRoom({
 | 
			
		||||
		name: guild.name,
 | 
			
		||||
		preset: "private_chat",
 | 
			
		||||
		visibility: "private",
 | 
			
		||||
| 
						 | 
				
			
			@ -37,10 +37,9 @@ function createSpace(guild) {
 | 
			
		|||
				}
 | 
			
		||||
			}
 | 
			
		||||
		]
 | 
			
		||||
	}).then(root => {
 | 
			
		||||
		db.prepare("INSERT INTO guild_space (guild_id, space_id) VALUES (?, ?)").run(guild.id, root.room_id)
 | 
			
		||||
		return root
 | 
			
		||||
	})
 | 
			
		||||
	db.prepare("INSERT INTO guild_space (guild_id, space_id) VALUES (?, ?)").run(guild.id, roomID)
 | 
			
		||||
	return roomID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.createSpace = createSpace
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const assert = require("assert")
 | 
			
		||||
const reg = require("../../matrix/read-registration")
 | 
			
		||||
 | 
			
		||||
const passthrough = require("../../passthrough")
 | 
			
		||||
const { discord, sync, db } = passthrough
 | 
			
		||||
| 
						 | 
				
			
			@ -14,10 +15,58 @@ const userToMxid = sync.require("../converters/user-to-mxid")
 | 
			
		|||
/**
 | 
			
		||||
 * A sim is an account that is being simulated by the bridge to copy events from the other side.
 | 
			
		||||
 * @param {import("discord-api-types/v10").APIUser} user
 | 
			
		||||
 * @returns mxid
 | 
			
		||||
 */
 | 
			
		||||
async function createSim(user) {
 | 
			
		||||
	// Choose sim name
 | 
			
		||||
	const simName = userToMxid.userToSimName(user)
 | 
			
		||||
	const appservicePrefix = "_ooye_"
 | 
			
		||||
	const localpart = appservicePrefix + simName
 | 
			
		||||
	const localpart = reg.namespace_prefix + simName
 | 
			
		||||
	const mxid = "@" + localpart + ":cadence.moe"
 | 
			
		||||
 | 
			
		||||
	// Save chosen name in the database forever
 | 
			
		||||
	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)
 | 
			
		||||
	return mxid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Ensure a sim is registered for the user.
 | 
			
		||||
 * If there is already a sim, use that one. If there isn't one yet, register a new sim.
 | 
			
		||||
 * @returns mxid
 | 
			
		||||
 */
 | 
			
		||||
async function ensureSim(user) {
 | 
			
		||||
	let mxid = null
 | 
			
		||||
	const existing = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(user.id)
 | 
			
		||||
	if (existing) {
 | 
			
		||||
		mxid = existing
 | 
			
		||||
	} else {
 | 
			
		||||
		mxid = await createSim(user)
 | 
			
		||||
	}
 | 
			
		||||
	return mxid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Ensure a sim is registered for the user and is joined to the room.
 | 
			
		||||
 * @returns mxid
 | 
			
		||||
 */
 | 
			
		||||
async function ensureSimJoined(user, roomID) {
 | 
			
		||||
	// Ensure room ID is really an ID, not an alias
 | 
			
		||||
	assert.ok(roomID[0] === "!")
 | 
			
		||||
 | 
			
		||||
	// Ensure user
 | 
			
		||||
	const mxid = await ensureSim(user)
 | 
			
		||||
 | 
			
		||||
	// Ensure joined
 | 
			
		||||
	const existing = db.prepare("SELECT * FROM sim_member WHERE room_id = ? and mxid = ?").get(roomID, mxid)
 | 
			
		||||
	if (!existing) {
 | 
			
		||||
		await api.inviteToRoom(roomID, mxid)
 | 
			
		||||
		await api.joinRoom(roomID, mxid)
 | 
			
		||||
		db.prepare("INSERT INTO sim_member (room_id, mxid) VALUES (?, ?)").run(roomID, mxid)
 | 
			
		||||
	}
 | 
			
		||||
	return mxid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.ensureSim = ensureSim
 | 
			
		||||
module.exports.ensureSimJoined = ensureSimJoined
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,27 +1,29 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const reg = require("../../matrix/read-registration.js")
 | 
			
		||||
const makeTxnId = require("../../matrix/txnid.js")
 | 
			
		||||
const fetch = require("node-fetch").default
 | 
			
		||||
const messageToEvent = require("../converters/message-to-event.js")
 | 
			
		||||
const reg = require("../../matrix/read-registration.js")
 | 
			
		||||
 | 
			
		||||
const passthrough = require("../../passthrough")
 | 
			
		||||
const { discord, sync, db } = passthrough
 | 
			
		||||
/** @type {import("../converters/message-to-event")} */
 | 
			
		||||
const messageToEvent = sync.require("../converters/message-to-event")
 | 
			
		||||
/** @type {import("../../matrix/api")} */
 | 
			
		||||
const api = sync.require("../../matrix/api")
 | 
			
		||||
/** @type {import("./register-user")} */
 | 
			
		||||
const registerUser = sync.require("./register-user")
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
 | 
			
		||||
 */
 | 
			
		||||
function sendMessage(message) {
 | 
			
		||||
async function sendMessage(message) {
 | 
			
		||||
	const event = messageToEvent.messageToEvent(message)
 | 
			
		||||
	return fetch(`https://matrix.cadence.moe/_matrix/client/v3/rooms/!VwVlIAjOjejUpDhlbA:cadence.moe/send/m.room.message/${makeTxnId()}?user_id=@_ooye_example:cadence.moe`, {
 | 
			
		||||
		method: "PUT",
 | 
			
		||||
		body: JSON.stringify(event),
 | 
			
		||||
		headers: {
 | 
			
		||||
			Authorization: `Bearer ${reg.as_token}`
 | 
			
		||||
		}
 | 
			
		||||
	}).then(res => res.text()).then(text => {
 | 
			
		||||
		// {"event_id":"$4Zxs0fMmYlbo-sTlMmSEvwIs9b4hcg6yORzK0Ems84Q"}
 | 
			
		||||
		console.log(text)
 | 
			
		||||
	}).catch(err => {
 | 
			
		||||
		console.log(err)
 | 
			
		||||
	})
 | 
			
		||||
	const roomID = "!VwVlIAjOjejUpDhlbA:cadence.moe"
 | 
			
		||||
	let senderMxid = null
 | 
			
		||||
	if (!message.webhook_id) {
 | 
			
		||||
		senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
 | 
			
		||||
	}
 | 
			
		||||
	const eventID = api.sendEvent(roomID, "m.room.message", event, senderMxid)
 | 
			
		||||
	return eventID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.sendMessage = sendMessage
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue