out-of-your-element/matrix/api.js

122 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-05-08 05:22:20 +00:00
// @ts-check
2023-07-04 05:19:17 +00:00
const Ty = require("../types")
const assert = require("assert")
2023-05-08 05:22:20 +00:00
const passthrough = require("../passthrough")
const { discord, sync, db } = passthrough
/** @type {import("./mreq")} */
const mreq = sync.require("./mreq")
/** @type {import("./file")} */
const file = sync.require("./file")
/** @type {import("./txnid")} */
const makeTxnId = sync.require("./txnid")
/**
* @param {string} p endpoint to access
* @param {string} [mxid] optional: user to act as, for the ?user_id parameter
* @returns {string} the new endpoint
*/
function path(p, mxid) {
if (!mxid) return p
const u = new URL(p, "http://localhost")
u.searchParams.set("user_id", mxid)
return u.pathname + "?" + u.searchParams.toString()
}
2023-05-08 05:22:20 +00:00
/**
* @param {string} username
2023-07-04 05:19:17 +00:00
* @returns {Promise<Ty.R.Registered>}
2023-05-08 05:22:20 +00:00
*/
function register(username) {
2023-05-09 03:29:46 +00:00
console.log(`[api] register: ${username}`)
2023-05-08 05:22:20 +00:00
return mreq.mreq("POST", "/client/v3/register", {
type: "m.login.application_service",
username
})
}
/**
* @returns {Promise<string>} room ID
*/
async function createRoom(content) {
2023-05-09 03:29:46 +00:00
console.log(`[api] create room:`, content)
2023-07-04 05:19:17 +00:00
/** @type {Ty.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) {
2023-07-04 05:19:17 +00:00
/** @type {Ty.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
})
}
async function leaveRoom(roomID, mxid) {
await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/leave`, mxid), {})
}
/**
* @param {string} roomID
2023-07-04 05:19:17 +00:00
* @returns {Promise<Ty.Event.BaseStateEvent[]>}
*/
function getAllState(roomID) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`)
}
/**
* @param {string} roomID
* @param {string} type
* @param {string} stateKey
* @param {string} [mxid]
* @returns {Promise<string>} event ID
*/
async function sendState(roomID, type, stateKey, content, mxid) {
2023-05-09 03:29:46 +00:00
console.log(`[api] state: ${roomID}: ${type}/${stateKey}`)
assert.ok(type)
2023-06-28 11:38:58 +00:00
assert.ok(typeof stateKey === "string")
2023-07-04 05:19:17 +00:00
/** @type {Ty.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) {
2023-05-09 03:29:46 +00:00
console.log(`[api] event to ${roomID} as ${mxid || "default sim"}`)
2023-07-04 05:19:17 +00:00
/** @type {Ty.R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid), content)
return root.event_id
}
2023-05-10 11:17:37 +00:00
async function profileSetDisplayname(mxid, displayname) {
await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/displayname`, mxid), {
displayname
})
}
async function profileSetAvatarUrl(mxid, avatar_url) {
await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/avatar_url`, mxid), {
avatar_url
})
}
module.exports.path = path
module.exports.register = register
module.exports.createRoom = createRoom
module.exports.joinRoom = joinRoom
module.exports.inviteToRoom = inviteToRoom
module.exports.leaveRoom = leaveRoom
module.exports.getAllState = getAllState
module.exports.sendState = sendState
module.exports.sendEvent = sendEvent
2023-05-10 11:17:37 +00:00
module.exports.profileSetDisplayname = profileSetDisplayname
module.exports.profileSetAvatarUrl = profileSetAvatarUrl