Forwarded messages code coverage and plaintext fix
This commit is contained in:
parent
1b539cfa64
commit
0d8b9d5705
3 changed files with 103 additions and 15 deletions
|
@ -199,9 +199,10 @@ async function attachmentToEvent(mentions, attachment) {
|
||||||
/**
|
/**
|
||||||
* @param {DiscordTypes.APIMessage} message
|
* @param {DiscordTypes.APIMessage} message
|
||||||
* @param {DiscordTypes.APIGuild} guild
|
* @param {DiscordTypes.APIGuild} guild
|
||||||
* @param {{includeReplyFallback?: boolean, includeEditFallbackStar?: boolean}} options default values:
|
* @param {{includeReplyFallback?: boolean, includeEditFallbackStar?: boolean, alwaysReturnFormattedBody?: boolean}} options default values:
|
||||||
* - includeReplyFallback: true
|
* - includeReplyFallback: true
|
||||||
* - includeEditFallbackStar: false
|
* - includeEditFallbackStar: false
|
||||||
|
* - alwaysReturnFormattedBody: false - formatted_body will be skipped if it is the same as body because the message is plaintext. if you want the formatted_body to be returned anyway, for example to merge it with another message, then set this to true.
|
||||||
* @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API
|
* @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API
|
||||||
*/
|
*/
|
||||||
async function messageToEvent(message, guild, options = {}, di) {
|
async function messageToEvent(message, guild, options = {}, di) {
|
||||||
|
@ -496,7 +497,7 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
|
|
||||||
const isPlaintext = body === html
|
const isPlaintext = body === html
|
||||||
|
|
||||||
if (!isPlaintext) {
|
if (!isPlaintext || options.alwaysReturnFormattedBody) {
|
||||||
Object.assign(newTextMessageEvent, {
|
Object.assign(newTextMessageEvent, {
|
||||||
format: "org.matrix.custom.html",
|
format: "org.matrix.custom.html",
|
||||||
formatted_body: html
|
formatted_body: html
|
||||||
|
@ -520,18 +521,20 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
const eventID = select("event_message", "event_id", {message_id: message.message_reference.message_id}).pluck().get()
|
const eventID = select("event_message", "event_id", {message_id: message.message_reference.message_id}).pluck().get()
|
||||||
const room = select("channel_room", ["room_id", "name", "nick"], {channel_id: message.message_reference.channel_id}).get()
|
const room = select("channel_room", ["room_id", "name", "nick"], {channel_id: message.message_reference.channel_id}).get()
|
||||||
const forwardedNotice = new mxUtils.MatrixStringBuilder()
|
const forwardedNotice = new mxUtils.MatrixStringBuilder()
|
||||||
if (eventID && room) {
|
if (room) {
|
||||||
|
const roomName = room && (room.nick || room.name)
|
||||||
const via = await getViaServersMemo(room.room_id)
|
const via = await getViaServersMemo(room.room_id)
|
||||||
forwardedNotice.addLine(
|
if (eventID) {
|
||||||
`[🔀 Forwarded from #${room.nick || room.name}]`,
|
forwardedNotice.addLine(
|
||||||
tag`🔀 <em>Forwarded from <a href="https://matrix.to/#/${room.room_id}/${eventID}?${via}">${room.nick || room.name}</a></em>`
|
`[🔀 Forwarded from #${roomName}]`,
|
||||||
)
|
tag`🔀 <em>Forwarded from <a href="https://matrix.to/#/${room.room_id}/${eventID}?${via}">${roomName}</a></em>`
|
||||||
} else if (room) {
|
)
|
||||||
const via = await getViaServersMemo(room.room_id)
|
} else {
|
||||||
forwardedNotice.addLine(
|
forwardedNotice.addLine(
|
||||||
`[🔀 Forwarded from #${room.nick || room.name}]`,
|
`[🔀 Forwarded from #${roomName}]`,
|
||||||
tag`🔀 <em>Forwarded from <a href="https://matrix.to/#/${room.room_id}?${via}">${room.nick || room.name}</a></em>`
|
tag`🔀 <em>Forwarded from <a href="https://matrix.to/#/${room.room_id}?${via}">${roomName}</a></em>`
|
||||||
)
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
forwardedNotice.addLine(
|
forwardedNotice.addLine(
|
||||||
`[🔀 Forwarded message]`,
|
`[🔀 Forwarded message]`,
|
||||||
|
@ -541,7 +544,7 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
|
|
||||||
// Forwarded content
|
// Forwarded content
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const forwardedEvents = await messageToEvent(message.message_snapshots[0].message, guild, {includeReplyFallback: false, includeEditFallbackStar: false}, di)
|
const forwardedEvents = await messageToEvent(message.message_snapshots[0].message, guild, {includeReplyFallback: false, includeEditFallbackStar: false, alwaysReturnFormattedBody: true}, di)
|
||||||
|
|
||||||
// Indent
|
// Indent
|
||||||
for (const event of forwardedEvents) {
|
for (const event of forwardedEvents) {
|
||||||
|
|
|
@ -1047,7 +1047,7 @@ test("message2event: forwarded image", async t => {
|
||||||
test("message2event: constructed forwarded message", async t => {
|
test("message2event: constructed forwarded message", async t => {
|
||||||
const events = await messageToEvent(data.message.constructed_forwarded_message, {}, {}, {
|
const events = await messageToEvent(data.message.constructed_forwarded_message, {}, {}, {
|
||||||
api: {
|
api: {
|
||||||
async getJoinedMembers(roomID) {
|
async getJoinedMembers() {
|
||||||
return {
|
return {
|
||||||
joined: {
|
joined: {
|
||||||
"@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
|
"@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
|
||||||
|
@ -1101,3 +1101,36 @@ test("message2event: constructed forwarded message", async t => {
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("message2event: constructed forwarded text", async t => {
|
||||||
|
const events = await messageToEvent(data.message.constructed_forwarded_text, {}, {}, {
|
||||||
|
api: {
|
||||||
|
async getJoinedMembers() {
|
||||||
|
return {
|
||||||
|
joined: {
|
||||||
|
"@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
|
||||||
|
"@user:matrix.org": {display_name: null, avatar_url: null}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.deepEqual(events, [
|
||||||
|
{
|
||||||
|
$type: "m.room.message",
|
||||||
|
body: "[🔀 Forwarded from #amanda-spam]"
|
||||||
|
+ "\n» What's cooking, good looking?",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: `🔀 <em>Forwarded from <a href="https://matrix.to/#/!CzvdIdUQXgUjDVKxeU:cadence.moe?via=cadence.moe&via=matrix.org">amanda-spam</a></em>`
|
||||||
|
+ `<br><blockquote>What's cooking, good looking?</blockquote>`,
|
||||||
|
"m.mentions": {},
|
||||||
|
msgtype: "m.notice",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$type: "m.room.message",
|
||||||
|
body: "What's cooking everybody ‼️",
|
||||||
|
"m.mentions": {},
|
||||||
|
msgtype: "m.text",
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
52
test/data.js
52
test/data.js
|
@ -2264,6 +2264,58 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
constructed_forwarded_text: { type: 0,
|
||||||
|
content: "What's cooking everybody ‼️",
|
||||||
|
mentions: [],
|
||||||
|
mention_roles: [],
|
||||||
|
attachments: [],
|
||||||
|
embeds: [],
|
||||||
|
timestamp: "2024-10-16T22:25:01.973000+00:00",
|
||||||
|
edited_timestamp: null,
|
||||||
|
flags: 16384,
|
||||||
|
components: [],
|
||||||
|
id: "1296237495993892916",
|
||||||
|
channel_id: "112760669178241024",
|
||||||
|
author: {
|
||||||
|
id: "113340068197859328",
|
||||||
|
username: "kumaccino",
|
||||||
|
avatar: "a8829abe66866d7797b36f0bfac01086",
|
||||||
|
discriminator: "0",
|
||||||
|
public_flags: 128,
|
||||||
|
flags: 128,
|
||||||
|
banner: null,
|
||||||
|
accent_color: null,
|
||||||
|
global_name: "kumaccino",
|
||||||
|
avatar_decoration_data: null,
|
||||||
|
banner_color: null,
|
||||||
|
clan: null
|
||||||
|
},
|
||||||
|
pinned: false,
|
||||||
|
mention_everyone: false,
|
||||||
|
tts: false,
|
||||||
|
message_reference: {
|
||||||
|
type: 1,
|
||||||
|
channel_id: "497161350934560778",
|
||||||
|
message_id: "0"
|
||||||
|
},
|
||||||
|
position: 0,
|
||||||
|
message_snapshots: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
type: 0,
|
||||||
|
content: "What's cooking, good looking?",
|
||||||
|
mentions: [],
|
||||||
|
mention_roles: [],
|
||||||
|
attachments: [],
|
||||||
|
embeds: [],
|
||||||
|
timestamp: "2022-09-15T01:20:58.177000+00:00",
|
||||||
|
edited_timestamp: null,
|
||||||
|
flags: 0,
|
||||||
|
components: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
pk_message: {
|
pk_message: {
|
||||||
|
|
Loading…
Reference in a new issue