don't send "" for attachments without content

This commit is contained in:
Cadence Ember 2023-07-02 01:41:31 +12:00
parent 1591bfc578
commit d592a3c82e
5 changed files with 104 additions and 42 deletions

View File

@ -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

View File

@ -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, [{

View File

@ -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<import("../types").Event.BaseStateEvent[]>}
@ -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

View File

@ -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 = {}

View File

@ -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"
}]
}
}
}
}