forked from cadence/out-of-your-element
switch to using api functions over mreq
This commit is contained in:
parent
7ee04d085f
commit
3bc29def41
9 changed files with 64 additions and 31 deletions
|
@ -5,10 +5,10 @@ const DiscordTypes = require("discord-api-types/v10")
|
|||
|
||||
const passthrough = require("../../passthrough")
|
||||
const { discord, sync, db } = passthrough
|
||||
/** @type {import("../../matrix/mreq")} */
|
||||
const mreq = sync.require("../../matrix/mreq")
|
||||
/** @type {import("../../matrix/file")} */
|
||||
const file = sync.require("../../matrix/file")
|
||||
/** @type {import("../../matrix/api")} */
|
||||
const api = sync.require("../../matrix/api")
|
||||
|
||||
function kstateStripConditionals(kstate) {
|
||||
for (const [k, content] of Object.entries(kstate)) {
|
||||
|
@ -51,8 +51,7 @@ function stateToKState(events) {
|
|||
* @param {string} roomID
|
||||
*/
|
||||
async function roomToKState(roomID) {
|
||||
/** @type {import("../../types").Event.BaseStateEvent[]} */
|
||||
const root = await mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`)
|
||||
const root = await api.getAllState(roomID)
|
||||
return stateToKState(root)
|
||||
}
|
||||
|
||||
|
@ -63,7 +62,7 @@ async function roomToKState(roomID) {
|
|||
function applyKStateDiffToRoom(roomID, kstate) {
|
||||
const events = kstateToState(kstate)
|
||||
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
|
||||
*/
|
||||
async function createRoom(channel, guild, spaceID, kstate) {
|
||||
/** @type {import("../../types").R.RoomCreated} */
|
||||
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
|
||||
const root = await api.createRoom({
|
||||
name: channel.name,
|
||||
topic: channel.topic || undefined,
|
||||
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)
|
||||
|
||||
// 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
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
const passthrough = require("../../passthrough")
|
||||
const { sync, db } = passthrough
|
||||
/** @type {import("../../matrix/mreq")} */
|
||||
const mreq = sync.require("../../matrix/mreq")
|
||||
/** @type {import("../../matrix/api")} */
|
||||
const api = sync.require("../../matrix/api")
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild
|
||||
*/
|
||||
function createSpace(guild) {
|
||||
return mreq.mreq("POST", "/client/v3/createRoom", {
|
||||
return api.createRoom({
|
||||
name: guild.name,
|
||||
preset: "private_chat",
|
||||
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)
|
||||
return root
|
||||
})
|
||||
|
|
|
@ -8,12 +8,16 @@ const { discord, sync, db } = passthrough
|
|||
const api = sync.require("../../matrix/api")
|
||||
/** @type {import("../../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.
|
||||
* @param {import("discord-api-types/v10").APIUser} user
|
||||
*/
|
||||
async function createSim(user) {
|
||||
assert.notEqual(user.discriminator, "0000", "user is not a webhook")
|
||||
api.register("_ooye_example")
|
||||
const simName = userToMxid.userToSimName(user)
|
||||
const appservicePrefix = "_ooye_"
|
||||
const localpart = appservicePrefix + simName
|
||||
await api.register(localpart)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ const markdown = require("discord-markdown")
|
|||
|
||||
/**
|
||||
* @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) {
|
||||
const body = message.content
|
||||
|
@ -25,4 +25,4 @@ function messageToEvent(message) {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports.messageToEvent = messageToEvent
|
||||
module.exports.messageToEvent = messageToEvent
|
||||
|
|
|
@ -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
|
||||
* @returns {string}
|
||||
*/
|
||||
|
@ -71,4 +72,4 @@ function userToSimName(user) {
|
|||
throw new Error(`Ran out of suggestions when generating sim name. downcased: "${downcased}"`)
|
||||
}
|
||||
|
||||
module.exports.userToSimName = userToSimName
|
||||
module.exports.userToSimName = userToSimName
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue