start getting d2m formatted body conversion
This commit is contained in:
parent
9569fda168
commit
1a4f92db97
4 changed files with 137 additions and 26 deletions
|
@ -8,6 +8,34 @@ const { sync, db, discord } = passthrough
|
|||
/** @type {import("../../matrix/file")} */
|
||||
const file = sync.require("../../matrix/file")
|
||||
|
||||
function getDiscordParseCallbacks(message, useHTML) {
|
||||
return {
|
||||
user: node => {
|
||||
const mxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(node.id)
|
||||
const username = message.mentions.find(ment => ment.id === node.id)?.username || node.id
|
||||
if (mxid && useHTML) {
|
||||
return `<a href="https://matrix.to/#/${mxid}">@${username}</a>`
|
||||
} else {
|
||||
return `@${username}:`
|
||||
}
|
||||
},
|
||||
channel: node => {
|
||||
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id)
|
||||
if (roomID && useHTML) {
|
||||
return "https://matrix.to/#/" + roomID
|
||||
} else {
|
||||
return "#" + node.id
|
||||
}
|
||||
},
|
||||
role: node =>
|
||||
"@&" + node.id,
|
||||
everyone: node =>
|
||||
"@room",
|
||||
here: node =>
|
||||
"@here"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").APIMessage} message
|
||||
* @param {import("discord-api-types/v10").APIGuild} guild
|
||||
|
@ -17,34 +45,18 @@ async function messageToEvent(message, guild) {
|
|||
|
||||
// Text content appears first
|
||||
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"
|
||||
}
|
||||
const html = markdown.toHTML(message.content, {
|
||||
discordCallback: getDiscordParseCallbacks(message, true)
|
||||
}, null, null)
|
||||
|
||||
const body = markdown.toHTML(message.content, {
|
||||
discordCallback: getDiscordParseCallbacks(message, false), //TODO: library bug!!
|
||||
discordOnly: true,
|
||||
escapeHTML: false,
|
||||
}, null, null)
|
||||
|
||||
const isPlaintext = body === html
|
||||
|
||||
if (isPlaintext) {
|
||||
events.push({
|
||||
$type: "m.room.message",
|
||||
|
|
|
@ -2,6 +2,26 @@ const {test} = require("supertape")
|
|||
const {messageToEvent} = require("./message-to-event")
|
||||
const data = require("../../test/data")
|
||||
|
||||
test("message2event: simple plaintext", async t => {
|
||||
const events = await messageToEvent(data.message.simple_plaintext, data.guild.general)
|
||||
t.deepEqual(events, [{
|
||||
$type: "m.room.message",
|
||||
msgtype: "m.text",
|
||||
body: "ayy lmao"
|
||||
}])
|
||||
})
|
||||
|
||||
test("message2event: simple user mention", async t => {
|
||||
const events = await messageToEvent(data.message.simple_user_mention, data.guild.general)
|
||||
t.deepEqual(events, [{
|
||||
$type: "m.room.message",
|
||||
msgtype: "m.text",
|
||||
body: "@crunch god: Tell me about Phil, renowned martial arts master and creator of the Chin Trick",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: '<span class="d-mention d-user"><a href="https://matrix.to/#/@_ooye_crunch_god:cadence.moe">@crunch god</a></span> Tell me about Phil, renowned martial arts master and creator of the Chin Trick'
|
||||
}])
|
||||
})
|
||||
|
||||
test("message2event: attachment with no content", async t => {
|
||||
const events = await messageToEvent(data.message.attachment_no_content, data.guild.general)
|
||||
t.deepEqual(events, [{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue