Fix scanning forwarded messages for mentions
This commit is contained in:
parent
b2078620be
commit
6a1be91071
3 changed files with 94 additions and 3 deletions
|
@ -199,10 +199,11 @@ 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, alwaysReturnFormattedBody?: boolean}} options default values:
|
* @param {{includeReplyFallback?: boolean, includeEditFallbackStar?: boolean, alwaysReturnFormattedBody?: boolean, scanTextForMentions?: 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.
|
* - 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.
|
||||||
|
* - scanTextForMentions: true - needs to be set to false when converting forwarded messages etc which may be from a different channel that can't be scanned.
|
||||||
* @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) {
|
||||||
|
@ -544,7 +545,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, alwaysReturnFormattedBody: true}, di)
|
const forwardedEvents = await messageToEvent(message.message_snapshots[0].message, guild, {includeReplyFallback: false, includeEditFallbackStar: false, alwaysReturnFormattedBody: true, scanTextForMentions: false}, di)
|
||||||
|
|
||||||
// Indent
|
// Indent
|
||||||
for (const event of forwardedEvents) {
|
for (const event of forwardedEvents) {
|
||||||
|
@ -570,7 +571,7 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
if (message.content) {
|
if (message.content) {
|
||||||
// Mentions scenario 3: scan the message content for written @mentions of matrix users. Allows for up to one space between @ and mention.
|
// Mentions scenario 3: scan the message content for written @mentions of matrix users. Allows for up to one space between @ and mention.
|
||||||
const matches = [...message.content.matchAll(/@ ?([a-z0-9._]+)\b/gi)]
|
const matches = [...message.content.matchAll(/@ ?([a-z0-9._]+)\b/gi)]
|
||||||
if (matches.length && matches.some(m => m[1].match(/[a-z]/i) && m[1] !== "everyone" && m[1] !== "here")) {
|
if (options.scanTextForMentions !== false && matches.length && matches.some(m => m[1].match(/[a-z]/i) && m[1] !== "everyone" && m[1] !== "here")) {
|
||||||
const writtenMentionsText = matches.map(m => m[1].toLowerCase())
|
const writtenMentionsText = matches.map(m => m[1].toLowerCase())
|
||||||
const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get()
|
const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get()
|
||||||
assert(roomID)
|
assert(roomID)
|
||||||
|
|
|
@ -1134,3 +1134,20 @@ test("message2event: constructed forwarded text", async t => {
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
test("message2event: don't scan forwarded messages for mentions", async t => {
|
||||||
|
const events = await messageToEvent(data.message.forwarded_dont_scan_for_mentions, {}, {}, {})
|
||||||
|
t.deepEqual(events, [
|
||||||
|
{
|
||||||
|
$type: "m.room.message",
|
||||||
|
body: "[🔀 Forwarded message]"
|
||||||
|
+ "\n» If some folks have spare bandwidth then helping out ArchiveTeam with archiving soon to be deleted research and government data might be worthwhile https://social.luca.run/@luca/113950834185678114",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: `🔀 <em>Forwarded message</em>`
|
||||||
|
+ `<br><blockquote>If some folks have spare bandwidth then helping out ArchiveTeam with archiving soon to be deleted research and government data might be worthwhile <a href="https://social.luca.run/@luca/113950834185678114">https://social.luca.run/@luca/113950834185678114</a></blockquote>`,
|
||||||
|
"m.mentions": {},
|
||||||
|
msgtype: "m.notice"
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
73
test/data.js
73
test/data.js
|
@ -2517,6 +2517,79 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
forwarded_dont_scan_for_mentions: {
|
||||||
|
type: 0,
|
||||||
|
tts: false,
|
||||||
|
timestamp: "2025-02-08T09:07:45.547000+00:00",
|
||||||
|
position: 0,
|
||||||
|
pinned: false,
|
||||||
|
nonce: "1337711633497063424",
|
||||||
|
message_snapshots: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
type: 0,
|
||||||
|
timestamp: "2025-02-08T09:00:07.662000+00:00",
|
||||||
|
mentions: [],
|
||||||
|
flags: 0,
|
||||||
|
embeds: [],
|
||||||
|
edited_timestamp: null,
|
||||||
|
content: "If some folks have spare bandwidth then helping out ArchiveTeam with archiving soon to be deleted research and government data might be worthwhile https://social.luca.run/@luca/113950834185678114",
|
||||||
|
components: [],
|
||||||
|
attachments: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
message_reference: {
|
||||||
|
type: 1,
|
||||||
|
message_id: "1337709539516223539",
|
||||||
|
guild_id: "500415824616620032",
|
||||||
|
channel_id: "794935364182867968"
|
||||||
|
},
|
||||||
|
mentions: [],
|
||||||
|
mention_roles: [],
|
||||||
|
mention_everyone: false,
|
||||||
|
member: {
|
||||||
|
roles: [
|
||||||
|
"1152297516755337248",
|
||||||
|
"300045569441660938",
|
||||||
|
"365531770420199435",
|
||||||
|
"1035943385338482698",
|
||||||
|
"1205645591212990515",
|
||||||
|
"1084555882341339259"
|
||||||
|
],
|
||||||
|
premium_since: null,
|
||||||
|
pending: false,
|
||||||
|
nick: null,
|
||||||
|
mute: false,
|
||||||
|
joined_at: "2023-12-27T13:02:41.614000+00:00",
|
||||||
|
flags: 0,
|
||||||
|
deaf: false,
|
||||||
|
communication_disabled_until: null,
|
||||||
|
banner: null,
|
||||||
|
avatar: null
|
||||||
|
},
|
||||||
|
id: "1337711460024844350",
|
||||||
|
flags: 16384,
|
||||||
|
embeds: [],
|
||||||
|
edited_timestamp: null,
|
||||||
|
content: "",
|
||||||
|
components: [],
|
||||||
|
channel_type: 0,
|
||||||
|
channel_id: "286888431945252874",
|
||||||
|
author: {
|
||||||
|
username: "athenna2000",
|
||||||
|
public_flags: 0,
|
||||||
|
primary_guild: null,
|
||||||
|
id: "620341774984151063",
|
||||||
|
global_name: "Amelia 🍄",
|
||||||
|
discriminator: "0",
|
||||||
|
clan: null,
|
||||||
|
avatar_decoration_data: null,
|
||||||
|
avatar: "a30f5b1bf17b5a5f387f1bb49771a2f8"
|
||||||
|
},
|
||||||
|
attachments: [],
|
||||||
|
guild_id: "286888431945252874"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
pk_message: {
|
pk_message: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue