switch to using api functions over mreq

This commit is contained in:
Cadence Ember 2023-05-08 23:37:51 +12:00
parent 7ee04d085f
commit 3bc29def41
9 changed files with 64 additions and 31 deletions

12
.vscode/tasks.json vendored
View file

@ -10,15 +10,7 @@
}, },
"problemMatcher": [], "problemMatcher": [],
"label": "npm: test", "label": "npm: test",
"detail": "cross-env FORCE_COLOR=true supertape --format tap test/test.js | tap-dot", "detail": "cross-env FORCE_COLOR=true supertape --format tap test/test.js | tap-dot"
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
}
} }
] ]
} }

View file

@ -5,10 +5,10 @@ const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const { discord, sync, db } = passthrough
/** @type {import("../../matrix/mreq")} */
const mreq = sync.require("../../matrix/mreq")
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file") const file = sync.require("../../matrix/file")
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
function kstateStripConditionals(kstate) { function kstateStripConditionals(kstate) {
for (const [k, content] of Object.entries(kstate)) { for (const [k, content] of Object.entries(kstate)) {
@ -51,8 +51,7 @@ function stateToKState(events) {
* @param {string} roomID * @param {string} roomID
*/ */
async function roomToKState(roomID) { async function roomToKState(roomID) {
/** @type {import("../../types").Event.BaseStateEvent[]} */ const root = await api.getAllState(roomID)
const root = await mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`)
return stateToKState(root) return stateToKState(root)
} }
@ -63,7 +62,7 @@ async function roomToKState(roomID) {
function applyKStateDiffToRoom(roomID, kstate) { function applyKStateDiffToRoom(roomID, kstate) {
const events = kstateToState(kstate) const events = kstateToState(kstate)
return Promise.all(events.map(({type, state_key, content}) => return Promise.all(events.map(({type, state_key, content}) =>
mreq.mreq("PUT", `/client/v3/rooms/${roomID}/state/${type}/${state_key}`, content) api.sendState(roomID, type, state_key, content)
)) ))
} }
@ -131,8 +130,7 @@ async function channelToKState(channel, guild) {
* @param {any} kstate * @param {any} kstate
*/ */
async function createRoom(channel, guild, spaceID, kstate) { async function createRoom(channel, guild, spaceID, kstate) {
/** @type {import("../../types").R.RoomCreated} */ const root = await api.createRoom({
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
name: channel.name, name: channel.name,
topic: channel.topic || undefined, topic: channel.topic || undefined,
preset: "private_chat", preset: "private_chat",
@ -144,7 +142,7 @@ async function createRoom(channel, guild, spaceID, kstate) {
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id) db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id)
// Put the newly created child into the space // Put the newly created child into the space
await mreq.mreq("PUT", `/client/v3/rooms/${spaceID}/state/m.space.child/${root.room_id}`, { await api.sendState(spaceID, "m.space.child", root.room_id, {
via: ["cadence.moe"] // TODO: use the proper server via: ["cadence.moe"] // TODO: use the proper server
}) })
} }

View file

@ -2,14 +2,14 @@
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { sync, db } = passthrough const { sync, db } = passthrough
/** @type {import("../../matrix/mreq")} */ /** @type {import("../../matrix/api")} */
const mreq = sync.require("../../matrix/mreq") const api = sync.require("../../matrix/api")
/** /**
* @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild * @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild
*/ */
function createSpace(guild) { function createSpace(guild) {
return mreq.mreq("POST", "/client/v3/createRoom", { return api.createRoom({
name: guild.name, name: guild.name,
preset: "private_chat", preset: "private_chat",
visibility: "private", visibility: "private",
@ -37,7 +37,7 @@ function createSpace(guild) {
} }
} }
] ]
}).then(/** @param {import("../../types").R.RoomCreated} root */ root => { }).then(root => {
db.prepare("INSERT INTO guild_space (guild_id, space_id) VALUES (?, ?)").run(guild.id, root.room_id) db.prepare("INSERT INTO guild_space (guild_id, space_id) VALUES (?, ?)").run(guild.id, root.room_id)
return root return root
}) })

View file

@ -8,12 +8,16 @@ const { discord, sync, db } = passthrough
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file") const file = sync.require("../../matrix/file")
/** @type {import("../converters/user-to-mxid")} */
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. * 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 * @param {import("discord-api-types/v10").APIUser} user
*/ */
async function createSim(user) { async function createSim(user) {
assert.notEqual(user.discriminator, "0000", "user is not a webhook") const simName = userToMxid.userToSimName(user)
api.register("_ooye_example") const appservicePrefix = "_ooye_"
const localpart = appservicePrefix + simName
await api.register(localpart)
} }

View file

@ -4,7 +4,7 @@ const markdown = require("discord-markdown")
/** /**
* @param {import("discord-api-types/v10").APIMessage} message * @param {import("discord-api-types/v10").APIMessage} message
* @returns {import("../../types").M_Room_Message_content} * @returns {import("../../types").Event.M_Room_Message}
*/ */
function messageToEvent(message) { function messageToEvent(message) {
const body = message.content const body = message.content
@ -25,4 +25,4 @@ function messageToEvent(message) {
} }
} }
module.exports.messageToEvent = messageToEvent module.exports.messageToEvent = messageToEvent

View file

@ -43,6 +43,7 @@ function* generateLocalpartAlternatives(preferences) {
} }
/** /**
* Whole process for checking the database and generating the right sim name.
* @param {import("discord-api-types/v10").APIUser} user * @param {import("discord-api-types/v10").APIUser} user
* @returns {string} * @returns {string}
*/ */
@ -71,4 +72,4 @@ function userToSimName(user) {
throw new Error(`Ran out of suggestions when generating sim name. downcased: "${downcased}"`) throw new Error(`Ran out of suggestions when generating sim name. downcased: "${downcased}"`)
} }
module.exports.userToSimName = userToSimName module.exports.userToSimName = userToSimName

View file

@ -1,5 +1,7 @@
// @ts-check // @ts-check
const assert = require("assert")
const passthrough = require("../passthrough") const passthrough = require("../passthrough")
const { discord, sync, db } = passthrough const { discord, sync, db } = passthrough
/** @type {import("./mreq")} */ /** @type {import("./mreq")} */
@ -8,6 +10,7 @@ const mreq = sync.require("./mreq")
const file = sync.require("./file") const file = sync.require("./file")
/** /**
* @param {string} username
* @returns {Promise<import("../types").R.Registered>} * @returns {Promise<import("../types").R.Registered>}
*/ */
function register(username) { function register(username) {
@ -17,4 +20,34 @@ function register(username) {
}) })
} }
module.exports.register = register /**
* @returns {Promise<import("../types").R.RoomCreated>}
*/
function createRoom(content) {
return mreq.mreq("POST", "/client/v3/createRoom", content)
}
/**
* @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
* @returns {Promise<import("../types").R.EventSent>}
*/
function sendState(roomID, type, stateKey, content) {
assert.ok(type)
assert.ok(stateKey)
return mreq.mreq("PUT", `/client/v3/rooms/${roomID}/state/${type}/${stateKey}`, content)
}
module.exports.register = register
module.exports.createRoom = createRoom
module.exports.getAllState = getAllState
module.exports.sendState = sendState

View file

@ -6,9 +6,10 @@ const util = require("util")
const passthrough = require("./passthrough") const passthrough = require("./passthrough")
const { discord, config, sync, db } = passthrough const { discord, config, sync, db } = passthrough
const createSpace = sync.require("./d2m/actions/create-space.js") const createSpace = sync.require("./d2m/actions/create-space")
const createRoom = sync.require("./d2m/actions/create-room.js") const createRoom = sync.require("./d2m/actions/create-room")
const mreq = sync.require("./matrix/mreq.js") const mreq = sync.require("./matrix/mreq")
const api = sync.require("./matrix/api")
const guildID = "112760669178241024" const guildID = "112760669178241024"
const extraContext = {} const extraContext = {}

4
types.d.ts vendored
View file

@ -54,4 +54,8 @@ namespace R {
access_token: string access_token: string
device_id: string device_id: string
} }
export type EventSent = {
event_id: string
}
} }