send sim messages to the proper rooms
This commit is contained in:
parent
7526d63690
commit
da6603d258
10 changed files with 38 additions and 10 deletions
|
@ -28,6 +28,7 @@ function path(p, mxid) {
|
|||
* @returns {Promise<import("../types").R.Registered>}
|
||||
*/
|
||||
function register(username) {
|
||||
console.log(`[api] register: ${username}`)
|
||||
return mreq.mreq("POST", "/client/v3/register", {
|
||||
type: "m.login.application_service",
|
||||
username
|
||||
|
@ -38,6 +39,7 @@ function register(username) {
|
|||
* @returns {Promise<string>} room ID
|
||||
*/
|
||||
async function createRoom(content) {
|
||||
console.log(`[api] create room:`, content)
|
||||
/** @type {import("../types").R.RoomCreated} */
|
||||
const root = await mreq.mreq("POST", "/client/v3/createRoom", content)
|
||||
return root.room_id
|
||||
|
@ -74,6 +76,7 @@ function getAllState(roomID) {
|
|||
* @returns {Promise<string>} event ID
|
||||
*/
|
||||
async function sendState(roomID, type, stateKey, content, mxid) {
|
||||
console.log(`[api] state: ${roomID}: ${type}/${stateKey}`)
|
||||
assert.ok(type)
|
||||
assert.ok(stateKey)
|
||||
/** @type {import("../types").R.EventSent} */
|
||||
|
@ -82,6 +85,7 @@ async function sendState(roomID, type, stateKey, content, mxid) {
|
|||
}
|
||||
|
||||
async function sendEvent(roomID, type, content, mxid) {
|
||||
console.log(`[api] event to ${roomID} as ${mxid || "default sim"}`)
|
||||
/** @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
|
||||
|
|
|
@ -11,11 +11,12 @@ const reg = sync.require("./read-registration.js")
|
|||
const baseUrl = "https://matrix.cadence.moe/_matrix"
|
||||
|
||||
class MatrixServerError extends Error {
|
||||
constructor(data) {
|
||||
constructor(data, opts) {
|
||||
super(data.error || data.errcode)
|
||||
this.data = data
|
||||
/** @type {string} */
|
||||
this.errcode = data.errcode
|
||||
this.opts = opts
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,7 +39,7 @@ async function mreq(method, url, body, extra = {}) {
|
|||
const res = await fetch(baseUrl + url, opts)
|
||||
const root = await res.json()
|
||||
|
||||
if (!res.ok || root.errcode) throw new MatrixServerError(root)
|
||||
if (!res.ok || root.errcode) throw new MatrixServerError(root, opts)
|
||||
return root
|
||||
}
|
||||
|
||||
|
|
11
matrix/read-registration.test.js
Normal file
11
matrix/read-registration.test.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
const {test} = require("supertape")
|
||||
const assert = require("assert")
|
||||
const reg = require("./read-registration")
|
||||
|
||||
test("reg: has necessary parameters", t => {
|
||||
const propertiesToCheck = ["sender_localpart", "id", "as_token", "namespace_prefix"]
|
||||
t.deepEqual(
|
||||
propertiesToCheck.filter(p => p in reg),
|
||||
propertiesToCheck
|
||||
)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue