diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 549d104d..382e9709 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -16,48 +16,50 @@ async function messageToEvent(message, guild) { const events = [] // Text content appears first - const body = message.content - const html = markdown.toHTML(body, { - discordCallback: { - user: node => { - const mxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(node.id) - if (mxid) { - return "https://matrix.to/#/" + mxid - } else { - return "@" + node.id - } - }, - channel: node => { - const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id) - if (roomID) { - return "https://matrix.to/#/" + roomID - } else { - return "#" + node.id - } - }, - role: node => - "@&" + node.id, - everyone: node => - "@room", - here: node => - "@here" + if (message.content) { + const body = message.content + const html = markdown.toHTML(body, { + discordCallback: { + user: node => { + const mxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(node.id) + if (mxid) { + return "https://matrix.to/#/" + mxid + } else { + return "@" + node.id + } + }, + channel: node => { + const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id) + if (roomID) { + return "https://matrix.to/#/" + roomID + } else { + return "#" + node.id + } + }, + role: node => + "@&" + node.id, + everyone: node => + "@room", + here: node => + "@here" + } + }, null, null) + const isPlaintext = body === html + if (isPlaintext) { + events.push({ + $type: "m.room.message", + msgtype: "m.text", + body: body + }) + } else { + events.push({ + $type: "m.room.message", + msgtype: "m.text", + body: body, + format: "org.matrix.custom.html", + formatted_body: html + }) } - }, null, null) - const isPlaintext = body === html - if (isPlaintext) { - events.push({ - $type: "m.room.message", - msgtype: "m.text", - body: body - }) - } else { - events.push({ - $type: "m.room.message", - msgtype: "m.text", - body: body, - format: "org.matrix.custom.html", - formatted_body: html - }) } // Then attachments diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js index c92cd850..c3183894 100644 --- a/d2m/converters/message-to-event.test.js +++ b/d2m/converters/message-to-event.test.js @@ -3,6 +3,23 @@ const assert = require("assert") const {messageToEvent} = require("./message-to-event") const data = require("../../test/data") +test("message2event: attachment with no content", async t => { + const events = await messageToEvent(data.message.attachment_no_content, data.guild.general) + t.deepEqual(events, [{ + $type: "m.room.message", + msgtype: "m.image", + url: "mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM", + body: "image.png", + external_url: "https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png", + info: { + mimetype: "image/png", + w: 466, + h: 85, + size: 12919, + }, + }]) +}) + test("message2event: stickers", async t => { const events = await messageToEvent(data.message.sticker, data.guild.general) t.deepEqual(events, [{ diff --git a/matrix/api.js b/matrix/api.js index 3ec014d4..ec85795a 100644 --- a/matrix/api.js +++ b/matrix/api.js @@ -60,6 +60,10 @@ async function inviteToRoom(roomID, mxidToInvite, mxid) { }) } +async function leaveRoom(roomID, mxid) { + await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/leave`, mxid), {}) +} + /** * @param {string} roomID * @returns {Promise} @@ -108,6 +112,7 @@ module.exports.register = register module.exports.createRoom = createRoom module.exports.joinRoom = joinRoom module.exports.inviteToRoom = inviteToRoom +module.exports.leaveRoom = leaveRoom module.exports.getAllState = getAllState module.exports.sendState = sendState module.exports.sendEvent = sendEvent diff --git a/stdin.js b/stdin.js index 99345ab5..1a5b8d17 100644 --- a/stdin.js +++ b/stdin.js @@ -11,6 +11,7 @@ const createRoom = sync.require("./d2m/actions/create-room") const registerUser = sync.require("./d2m/actions/register-user") const mreq = sync.require("./matrix/mreq") const api = sync.require("./matrix/api") +const sendMessage = sync.require("./m2d/actions/send-message") const guildID = "112760669178241024" const extraContext = {} diff --git a/test/data.js b/test/data.js index 85b3cd40..5efea362 100644 --- a/test/data.js +++ b/test/data.js @@ -140,6 +140,43 @@ module.exports = { }, message: { // Display order is text content, attachments, then stickers + attachment_no_content: { + id: "1124628646670389348", + type: 0, + content: "", + channel_id: "497161332244742154", + author: { + id: "320067006521147393", + username: "papiophidian", + global_name: "PapiOphidian", + avatar: "fb2b4535f7a108619e3edae12fcb16c5", + discriminator: "0", + public_flags: 4194880, + avatar_decoration: null + }, + attachments: [ + { + id: "1124628646431297546", + filename: "image.png", + size: 12919, + url: "https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png", + proxy_url: "https://media.discordapp.net/attachments/497161332244742154/1124628646431297546/image.png", + width: 466, + height: 85, + content_type: "image/png" + } + ], + embeds: [], + mentions: [], + mention_roles: [], + pinned: false, + mention_everyone: false, + tts: false, + timestamp: "2023-07-01T09:12:43.956000+00:00", + edited_timestamp: null, + flags: 0, + components: [] + }, sticker: { id: "1106366167788044450", type: 0, @@ -180,6 +217,6 @@ module.exports = { format_type: 1, name: "pomu puff" }] - } + } } }