1
0
Fork 0

register -> invite -> join -> send flow

This commit is contained in:
Cadence Ember 2023-05-09 00:58:46 +12:00
parent 3bc29def41
commit 1e7e66dc31
11 changed files with 150 additions and 34 deletions

View file

@ -8,6 +8,15 @@ const { discord, sync, db } = passthrough
const mreq = sync.require("./mreq")
/** @type {import("./file")} */
const file = sync.require("./file")
/** @type {import("./txnid")} */
const makeTxnId = sync.require("./txnid")
function path(p, mxid = null) {
if (!mxid) return p
const u = new URL(p, "http://localhost")
u.searchParams.set("user_id", mxid)
return u.pathname + "?" + u.searchParams.toString()
}
/**
* @param {string} username
@ -21,10 +30,27 @@ function register(username) {
}
/**
* @returns {Promise<import("../types").R.RoomCreated>}
* @returns {Promise<string>} room ID
*/
function createRoom(content) {
return mreq.mreq("POST", "/client/v3/createRoom", content)
async function createRoom(content) {
/** @type {import("../types").R.RoomCreated} */
const root = await mreq.mreq("POST", "/client/v3/createRoom", content)
return root.room_id
}
/**
* @returns {Promise<string>} room ID
*/
async function joinRoom(roomIDOrAlias, mxid) {
/** @type {import("../types").R.RoomJoined} */
const root = await mreq.mreq("POST", path(`/client/v3/join/${roomIDOrAlias}`, mxid))
return root.room_id
}
async function inviteToRoom(roomID, mxidToInvite, mxid) {
await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/invite`, mxid), {
user_id: mxidToInvite
})
}
/**
@ -39,15 +65,27 @@ function getAllState(roomID) {
* @param {string} roomID
* @param {string} type
* @param {string} stateKey
* @returns {Promise<import("../types").R.EventSent>}
* @returns {Promise<string>} event ID
*/
function sendState(roomID, type, stateKey, content) {
async function sendState(roomID, type, stateKey, content, mxid) {
assert.ok(type)
assert.ok(stateKey)
return mreq.mreq("PUT", `/client/v3/rooms/${roomID}/state/${type}/${stateKey}`, content)
/** @type {import("../types").R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/state/${type}/${stateKey}`, mxid), content)
return root.event_id
}
async function sendEvent(roomID, type, content, mxid) {
/** @type {import("../types").R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid), content)
return root.event_id
}
module.exports.path = path
module.exports.register = register
module.exports.createRoom = createRoom
module.exports.joinRoom = joinRoom
module.exports.inviteToRoom = inviteToRoom
module.exports.getAllState = getAllState
module.exports.sendState = sendState
module.exports.sendEvent = sendEvent

23
matrix/api.test.js Normal file
View file

@ -0,0 +1,23 @@
const {test} = require("supertape")
const assert = require("assert")
const {path} = require("./api")
test("api path: no change for plain path", t => {
t.equal(path("/hello/world"), "/hello/world")
})
test("api path: add mxid to the URL", t => {
t.equal(path("/hello/world", "12345"), "/hello/world?user_id=12345")
})
test("api path: empty path with mxid", t => {
t.equal(path("", "12345"), "/?user_id=12345")
})
test("api path: existing query parameters with mxid", t => {
t.equal(path("/hello/world?foo=bar&baz=qux", "12345"), "/hello/world?foo=bar&baz=qux&user_id=12345")
})
test("api path: real world mxid", t => {
t.equal(path("/hello/world", "@cookie_monster:cadence.moe"), "/hello/world?user_id=%40cookie_monster%3Acadence.moe")
})

View file

@ -16,8 +16,6 @@ class MatrixServerError extends Error {
this.data = data
/** @type {string} */
this.errcode = data.errcode
/** @type {string} */
this.error = data.error
}
}

View file

@ -2,6 +2,6 @@
let now = Date.now()
module.exports = function makeTxnId() {
module.exports.makeTxnId = function makeTxnId() {
return now++
}