1
0
Fork 0

preparing for m2d

This commit is contained in:
Cadence Ember 2023-07-03 01:06:05 +12:00
parent d592a3c82e
commit 6e55e6d1b3
6 changed files with 80 additions and 1 deletions

View file

@ -10,7 +10,7 @@ const registerWebhook = sync.require("./register-webhook")
/**
* @param {string} channelID
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {name: string, file: Buffer}[]} data
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]}[]} data
*/
// param {DiscordTypes.RESTPostAPIWebhookWithTokenQuery & {wait: true, disableEveryone?: boolean}} options
async function sendMessage(channelID, data) {

View file

@ -0,0 +1,30 @@
// @ts-check
const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10")
const markdown = require("discord-markdown")
const passthrough = require("../../passthrough")
const { sync, db, discord } = passthrough
/** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file")
/**
* @param {import("../../types").Event.Outer<import("../../types").Event.M_Room_Message>} event
*/
function eventToMessage(event) {
/** @type {(DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]})[]} */
const messages = []
if (event.content.msgtype === "m.text") {
messages.push({
content: event.content.body,
username: event.sender.replace(/^@/, ""),
avatar_url: undefined, // TODO: provide the URL to the avatar from the homeserver's content repo
})
}
return messages
}
module.exports.eventToMessage = eventToMessage

View file

@ -0,0 +1,32 @@
// @ts-check
const {test} = require("supertape")
const assert = require("assert")
const {eventToMessage} = require("./event-to-message")
const data = require("../../test/data")
test("event2message: janky test", t => {
t.deepEqual(
eventToMessage({
age: 405299,
content: {
body: "test",
msgtype: "m.text"
},
event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
origin_server_ts: 1688301929913,
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
sender: "@cadence:cadence.moe",
type: "m.room.message",
unsigned: {
age: 405299
},
user_id: "@cadence:cadence.moe"
}),
[{
username: "cadence:cadence.moe",
content: "test",
avatar_url: undefined
}]
)
})

View file

@ -1,3 +1,5 @@
// @ts-check
const assert = require("assert").strict
const {sync, as} = require("../passthrough")
@ -5,4 +7,6 @@ const {sync, as} = require("../passthrough")
sync.addTemporaryListener(as, "type:m.room.message", event => {
console.log(event)
// TODO: filter out events that were bridged discord messages (i.e. sent by OOYE)
// TODO: call sendMessage
})

View file

@ -18,3 +18,4 @@ require("../d2m/converters/message-to-event.test")
require("../d2m/actions/create-room.test")
require("../d2m/converters/user-to-mxid.test")
require("../d2m/actions/register-user.test")
require("../m2d/converters/event-to-message.test")

12
types.d.ts vendored
View file

@ -15,6 +15,18 @@ export type WebhookCreds = {
}
namespace Event {
export type Outer<T> = {
type: string
room_id: string
sender: string
content: T
origin_server_ts: number
unsigned: any
event_id: string
user_id: string
age: number
}
export type BaseStateEvent = {
type: string
room_id: string