From 6e55e6d1b358707821f08b251940c2385757b204 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 3 Jul 2023 01:06:05 +1200 Subject: [PATCH] preparing for m2d --- m2d/actions/send-message.js | 2 +- m2d/converters/event-to-message.js | 30 +++++++++++++++++++++++ m2d/converters/event-to-message.test.js | 32 +++++++++++++++++++++++++ m2d/event-dispatcher.js | 4 ++++ test/test.js | 1 + types.d.ts | 12 ++++++++++ 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 m2d/converters/event-to-message.js create mode 100644 m2d/converters/event-to-message.test.js diff --git a/m2d/actions/send-message.js b/m2d/actions/send-message.js index 9c611071..3505b601 100644 --- a/m2d/actions/send-message.js +++ b/m2d/actions/send-message.js @@ -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) { diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js new file mode 100644 index 00000000..f48b5daa --- /dev/null +++ b/m2d/converters/event-to-message.js @@ -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} 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 diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js new file mode 100644 index 00000000..a41beef7 --- /dev/null +++ b/m2d/converters/event-to-message.test.js @@ -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 + }] + ) +}) diff --git a/m2d/event-dispatcher.js b/m2d/event-dispatcher.js index 136aae0c..789d9edc 100644 --- a/m2d/event-dispatcher.js +++ b/m2d/event-dispatcher.js @@ -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 }) diff --git a/test/test.js b/test/test.js index 5f06ae41..f2f0912d 100644 --- a/test/test.js +++ b/test/test.js @@ -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") diff --git a/types.d.ts b/types.d.ts index aaa8db16..2181793d 100644 --- a/types.d.ts +++ b/types.d.ts @@ -15,6 +15,18 @@ export type WebhookCreds = { } namespace Event { + export type Outer = { + 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