2023-05-08 05:22:20 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-05-08 11:37:51 +00:00
|
|
|
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")
|
2023-05-08 12:58:46 +00:00
|
|
|
/** @type {import("./txnid")} */
|
|
|
|
const makeTxnId = sync.require("./txnid")
|
|
|
|
|
2023-05-08 20:03:57 +00:00
|
|
|
/**
|
|
|
|
* @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) {
|
2023-05-08 12:58:46 +00:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2023-05-08 11:37:51 +00:00
|
|
|
* @param {string} username
|
2023-05-08 05:22:20 +00:00
|
|
|
* @returns {Promise<import("../types").R.Registered>}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-08 11:37:51 +00:00
|
|
|
/**
|
2023-05-08 12:58:46 +00:00
|
|
|
* @returns {Promise<string>} room ID
|
2023-05-08 11:37:51 +00:00
|
|
|
*/
|
2023-05-08 12:58:46 +00:00
|
|
|
async function createRoom(content) {
|
2023-05-09 03:29:46 +00:00
|
|
|
console.log(`[api] create room:`, content)
|
2023-05-08 12:58:46 +00:00
|
|
|
/** @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
|
|
|
|
})
|
2023-05-08 11:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} roomID
|
|
|
|
* @returns {Promise<import("../types").Event.BaseStateEvent[]>}
|
|
|
|
*/
|
|
|
|
function getAllState(roomID) {
|
|
|
|
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} roomID
|
|
|
|
* @param {string} type
|
|
|
|
* @param {string} stateKey
|
2023-05-08 20:03:57 +00:00
|
|
|
* @param {string} [mxid]
|
2023-05-08 12:58:46 +00:00
|
|
|
* @returns {Promise<string>} event ID
|
2023-05-08 11:37:51 +00:00
|
|
|
*/
|
2023-05-08 12:58:46 +00:00
|
|
|
async function sendState(roomID, type, stateKey, content, mxid) {
|
2023-05-09 03:29:46 +00:00
|
|
|
console.log(`[api] state: ${roomID}: ${type}/${stateKey}`)
|
2023-05-08 11:37:51 +00:00
|
|
|
assert.ok(type)
|
|
|
|
assert.ok(stateKey)
|
2023-05-08 12:58:46 +00:00
|
|
|
/** @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) {
|
2023-05-09 03:29:46 +00:00
|
|
|
console.log(`[api] event to ${roomID} as ${mxid || "default sim"}`)
|
2023-05-08 12:58:46 +00:00
|
|
|
/** @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
|
2023-05-08 11:37:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-08 12:58:46 +00:00
|
|
|
module.exports.path = path
|
2023-05-08 11:37:51 +00:00
|
|
|
module.exports.register = register
|
|
|
|
module.exports.createRoom = createRoom
|
2023-05-08 12:58:46 +00:00
|
|
|
module.exports.joinRoom = joinRoom
|
|
|
|
module.exports.inviteToRoom = inviteToRoom
|
2023-05-08 11:37:51 +00:00
|
|
|
module.exports.getAllState = getAllState
|
|
|
|
module.exports.sendState = sendState
|
2023-05-08 12:58:46 +00:00
|
|
|
module.exports.sendEvent = sendEvent
|
2023-05-10 11:17:37 +00:00
|
|
|
module.exports.profileSetDisplayname = profileSetDisplayname
|
|
|
|
module.exports.profileSetAvatarUrl = profileSetAvatarUrl
|