2023-04-25 20:06:08 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const markdown = require("discord-markdown")
|
|
|
|
|
2023-05-12 05:35:37 +00:00
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const { sync, db } = passthrough
|
|
|
|
/** @type {import("../../matrix/file")} */
|
|
|
|
const file = sync.require("../../matrix/file")
|
|
|
|
|
2023-04-25 20:06:08 +00:00
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").APIMessage} message
|
|
|
|
*/
|
2023-05-12 05:35:37 +00:00
|
|
|
async function messageToEvent(message) {
|
|
|
|
const events = []
|
|
|
|
|
|
|
|
// Text content appears first
|
2023-04-25 20:06:08 +00:00
|
|
|
const body = message.content
|
|
|
|
const html = markdown.toHTML(body, {
|
2023-05-12 05:35:37 +00:00
|
|
|
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"
|
|
|
|
}
|
2023-04-25 20:06:08 +00:00
|
|
|
}, null, null)
|
2023-05-12 05:35:37 +00:00
|
|
|
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
|
|
|
|
})
|
2023-04-25 20:06:08 +00:00
|
|
|
}
|
2023-05-12 05:35:37 +00:00
|
|
|
|
|
|
|
// Then attachments
|
|
|
|
const attachmentEvents = await Promise.all(message.attachments.map(async attachment => {
|
|
|
|
// TODO: handle large files differently - link them instead of uploading
|
|
|
|
if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) {
|
|
|
|
return {
|
|
|
|
$type: "m.room.message",
|
|
|
|
msgtype: "m.image",
|
|
|
|
url: await file.uploadDiscordFileToMxc(attachment.url),
|
|
|
|
external_url: attachment.url,
|
|
|
|
body: attachment.filename,
|
|
|
|
// TODO: filename: attachment.filename and then use body as the caption
|
|
|
|
info: {
|
|
|
|
mimetype: attachment.content_type,
|
|
|
|
w: attachment.width,
|
|
|
|
h: attachment.height,
|
|
|
|
size: attachment.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
$type: "m.room.message",
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Unsupported attachment:\n" + JSON.stringify(attachment, null, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
events.push(...attachmentEvents)
|
|
|
|
|
|
|
|
// Then stickers
|
|
|
|
|
|
|
|
return events
|
2023-04-25 20:06:08 +00:00
|
|
|
}
|
2023-05-05 05:29:08 +00:00
|
|
|
|
2023-05-08 11:37:51 +00:00
|
|
|
module.exports.messageToEvent = messageToEvent
|