diff --git a/d2m/actions/edit-message.js b/d2m/actions/edit-message.js index d52fcbd..d85f925 100644 --- a/d2m/actions/edit-message.js +++ b/d2m/actions/edit-message.js @@ -53,7 +53,7 @@ async function editMessage(message, guild, row) { const sendNewEventParts = new Set() for (const promotion of promotions) { if ("eventID" in promotion) { - db.prepare(`UPDATE event_message SET ${promotion.column} = 0 WHERE event_id = ?`).run(promotion.eventID) + db.prepare(`UPDATE event_message SET ${promotion.column} = ? WHERE event_id = ?`).run(promotion.value ?? 0, promotion.eventID) } else if ("nextEvent" in promotion) { sendNewEventParts.add(promotion.column) } diff --git a/d2m/converters/edit-to-changes.js b/d2m/converters/edit-to-changes.js index c80bb49..f93c510 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -24,14 +24,13 @@ function eventCanBeEdited(ev) { /** * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message - * IMPORTANT: This may not have all the normal fields! The API documentation doesn't provide possible types, just says it's all optional! - * Since I don't have a spec, I will have to capture some real traffic and add it as test cases... I hope they don't change anything later... * @param {import("discord-api-types/v10").APIGuild} guild * @param {import("../../matrix/api")} api simple-as-nails dependency injection for the matrix API */ async function editToChanges(message, guild, api) { // If it is a user edit, allow deleting old messages (e.g. they might have removed text from an image). // If it is the system adding a generated embed to a message, don't delete old messages since the system only sends partial data. + // Since an update in August 2024, the system always provides the full data of message updates. I'll leave in the old code since it won't cause problems. const isGeneratedEmbed = !("content" in message) @@ -123,7 +122,7 @@ async function editToChanges(message, guild, api) { eventsToReplace = eventsToReplace.filter(eventCanBeEdited) // We want to maintain exactly one part = 0 and one reaction_part = 0 database row at all times. - /** @type {({column: string, eventID: string} | {column: string, nextEvent: true})[]} */ + /** @type {({column: string, eventID: string, value?: number} | {column: string, nextEvent: true})[]} */ const promotions = [] for (const column of ["part", "reaction_part"]) { const candidatesForParts = unchangedEvents.concat(eventsToReplace) @@ -143,6 +142,16 @@ async function editToChanges(message, guild, api) { promotions.push({column, nextEvent: true}) } } + // If adding events, try to keep reactions attached to the bottom of the group (unless reactions have already been added) + if (eventsToSend.length && !promotions.length) { + const existingReaction = select("reaction", "message_id", {message_id: message.id}).pluck().get() + if (!existingReaction) { + const existingPartZero = candidatesForParts.find(p => p.old.reaction_part === 0) + assert(existingPartZero) // will exist because a reaction_part=0 always exists and no events are being removed + promotions.push({column: "reaction_part", eventID: existingPartZero.old.event_id, value: 1}) // update the current reaction_part to 1 + promotions.push({column: "reaction_part", nextEvent: true}) // the newly created event will have reaction_part = 0 + } + } } // Removing unnecessary properties before returning diff --git a/d2m/converters/edit-to-changes.test.js b/d2m/converters/edit-to-changes.test.js index b00a2c4..effb777 100644 --- a/d2m/converters/edit-to-changes.test.js +++ b/d2m/converters/edit-to-changes.test.js @@ -109,7 +109,7 @@ test("edit2changes: change file type", async t => { t.deepEqual(promotions, [{column: "part", nextEvent: true}, {column: "reaction_part", nextEvent: true}]) }) -test("edit2changes: add caption back to that image", async t => { +test("edit2changes: add caption back to that image (due to it having a reaction, the reaction_part will not be moved)", async t => { const {eventsToRedact, eventsToReplace, eventsToSend, promotions} = await editToChanges(data.message_update.added_caption_to_image, data.guild.general, {}) t.deepEqual(eventsToRedact, []) t.deepEqual(eventsToSend, [{ @@ -266,7 +266,62 @@ test("edit2changes: generated embed", async t => { + `
In reply to a Discord user
[Replied-to message content wasn't provided by Discord]
`, + "m.mentions": {} + }]) + t.deepEqual(promotions, [{ + "column": "reaction_part", + "eventID": "$UTqiL3Zj3FC4qldxRLggN1fhygpKl8sZ7XGY5f9MNbF", + "value": 1, + }, { + "column": "reaction_part", + "nextEvent": true, + }]) + t.equal(senderMxid, "@_ooye_cadence:cadence.moe") +}) diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js index fbfe06d..02fb4bb 100644 --- a/d2m/converters/message-to-event.test.js +++ b/d2m/converters/message-to-event.test.js @@ -64,6 +64,44 @@ test("message2event: simple user mention", async t => { test("message2event: simple room mention", async t => { let called = 0 const events = await messageToEvent(data.message.simple_room_mention, data.guild.general, {}, { + api: { + async getStateEvent(roomID, type, key) { + called++ + t.equal(roomID, "!BnKuBPCvyfOkhcUjEu:cadence.moe") + t.equal(type, "m.room.power_levels") + t.equal(key, "") + return { + users: { + "@_ooye_bot:cadence.moe": 100 + } + } + }, + async getJoinedMembers(roomID) { + called++ + t.equal(roomID, "!BnKuBPCvyfOkhcUjEu:cadence.moe") + 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", + "m.mentions": {}, + msgtype: "m.text", + body: "#worm-farm", + format: "org.matrix.custom.html", + formatted_body: '#worm-farm' + }]) + t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each") +}) + +test("message2event: nicked room mention", async t => { + let called = 0 + const events = await messageToEvent(data.message.nicked_room_mention, data.guild.general, {}, { api: { async getStateEvent(roomID, type, key) { called++ diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 2984e8e..5234755 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -206,11 +206,10 @@ function getCodeContent(preCode) { */ function convertEmoji(mxcUrl, nameForGuess, allowSpriteSheetIndicator, allowLink) { // Get the known emoji from the database. - let row - if (mxcUrl) row = select("emoji", ["emoji_id", "name", "animated"], {mxc_url: mxcUrl}).get() + if (mxcUrl) var row = select("emoji", ["emoji_id", "name", "animated"], {mxc_url: mxcUrl}).get() // Now we have to search all servers to see if we're able to send this emoji. if (row) { - const found = [...discord.guilds.values()].find(g => g.emojis.find(e => e.id === row.id)) + const found = [...discord.guilds.values()].find(g => g.emojis.find(e => e.id === row?.emoji_id)) if (!found) row = null } // Or, if we don't have an emoji right now, we search for the name instead. @@ -220,7 +219,7 @@ function convertEmoji(mxcUrl, nameForGuess, allowSpriteSheetIndicator, allowLink /** @type {{name: string, id: string, animated: number}[]} */ // @ts-ignore const emojis = guild.emojis - const found = emojis.find(e => e.id === row?.id || e.name?.toLowerCase() === nameForGuessLower) + const found = emojis.find(e => e.name?.toLowerCase() === nameForGuessLower) if (found) { row = { animated: found.animated, @@ -686,7 +685,7 @@ async function eventToMessage(event, guild, di) { let preNode if (node.nodeType === 3 && node.nodeValue.includes("```") && (preNode = nodeIsChildOf(node, ["PRE"]))) { if (preNode.firstChild?.nodeName === "CODE") { - const ext = (preNode.firstChild.className.match(/language-(\S+)/) || [null, "txt"])[1] + const ext = preNode.firstChild.className.match(/language-(\S+)/)?.[1] || "txt" const filename = `inline_code.${ext}` // Build the replacementMatrix - Decentralised and secure communication` + + `
You're invited to talk on Matrix. If you don't already have a client this link will help you pick one, and join the conversation. If you already have one, this link will help you join the conversation
node
const replacementCode = doc.createElement("code")
diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js
index b404e59..d6a20cd 100644
--- a/m2d/converters/event-to-message.test.js
+++ b/m2d/converters/event-to-message.test.js
@@ -772,6 +772,38 @@ test("event2message: code blocks are uploaded as attachments instead if they con
)
})
+test("event2message: code blocks are uploaded as attachments instead if they contain incompatible backticks (default to txt file extension)", async t => {
+ t.deepEqual(
+ await eventToMessage({
+ type: "m.room.message",
+ sender: "@cadence:cadence.moe",
+ content: {
+ msgtype: "m.text",
+ body: "wrong body",
+ format: "org.matrix.custom.html",
+ formatted_body: 'So if you run code like thisSystem.out.println("```");
it should print a markdown formatted code block'
+ },
+ event_id: "$pGkWQuGVmrPNByrFELxhzI6MCBgJecr5I2J3z88Gc2s",
+ room_id: "!BpMdOUkWWhFxmTrENV:cadence.moe"
+ }),
+ {
+ ensureJoined: [],
+ messagesToDelete: [],
+ messagesToEdit: [],
+ messagesToSend: [{
+ username: "cadence [they]",
+ content: "So if you run code like this `[inline_code.txt]` it should print a markdown formatted code block",
+ attachments: [{id: "0", filename: "inline_code.txt"}],
+ pendingFiles: [{name: "inline_code.txt", buffer: Buffer.from('System.out.println("```");')}],
+ avatar_url: undefined,
+ allowed_mentions: {
+ parse: ["users", "roles"]
+ }
+ }]
+ }
+ )
+})
+
test("event2message: characters are encoded properly in code blocks", async t => {
t.deepEqual(
await eventToMessage({
@@ -3794,6 +3826,36 @@ test("event2message: static emojis work", async t => {
)
})
+test("event2message: emojis in other servers are reused if they have the same title text", async t => {
+ t.deepEqual(
+ await eventToMessage({
+ type: "m.room.message",
+ sender: "@cadence:cadence.moe",
+ content: {
+ msgtype: "m.text",
+ body: ":hippo:",
+ format: "org.matrix.custom.html",
+ formatted_body: '
'
+ },
+ event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
+ room_id: "!CzvdIdUQXgUjDVKxeU:cadence.moe"
+ }),
+ {
+ ensureJoined: [],
+ messagesToDelete: [],
+ messagesToEdit: [],
+ messagesToSend: [{
+ username: "cadence [they]",
+ content: "<:hippo:230201364309868544>",
+ avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
+ allowed_mentions: {
+ parse: ["users", "roles"]
+ }
+ }]
+ }
+ )
+})
+
test("event2message: animated emojis work", async t => {
t.deepEqual(
await eventToMessage({
diff --git a/scripts/seed.js b/scripts/seed.js
index 2ab3e02..a3ffbbe 100644
--- a/scripts/seed.js
+++ b/scripts/seed.js
@@ -59,6 +59,7 @@ async function uploadAutoEmoji(guild, name, filename) {
assert.notEqual(reg.ooye.server_origin.slice(-1), "/", "server origin must not end in slash")
const botID = Buffer.from(config.discordToken.split(".")[0], "base64").toString()
assert(botID.match(/^[0-9]{10,}$/), "discord token must follow the correct format")
+ assert.match(reg.url, /^https?:/, "url must start with http:// or https://")
console.log("✅ Configuration looks good...")
// database ddl...
diff --git a/test/data.js b/test/data.js
index 456033a..18aaa1a 100644
--- a/test/data.js
+++ b/test/data.js
@@ -450,6 +450,63 @@ module.exports = {
components: []
},
simple_room_mention: {
+ type: 0,
+ tts: false,
+ timestamp: "2023-07-10T20:04:25.939000+00:00",
+ referenced_message: null,
+ pinned: false,
+ nonce: "1128054139385806848",
+ mentions: [],
+ mention_roles: [],
+ mention_everyone: false,
+ member: {
+ roles: [
+ "112767366235959296", "118924814567211009",
+ "204427286542417920", "199995902742626304",
+ "222168467627835392", "238028326281805825",
+ "259806643414499328", "265239342648131584",
+ "271173313575780353", "287733611912757249",
+ "225744901915148298", "305775031223320577",
+ "318243902521868288", "348651574924541953",
+ "349185088157777920", "378402925128712193",
+ "392141548932038658", "393912152173576203",
+ "482860581670486028", "495384759074160642",
+ "638988388740890635", "373336013109461013",
+ "530220455085473813", "454567553738473472",
+ "790724320824655873", "1123518980456452097",
+ "1040735082610167858", "695946570482450442",
+ "1123460940935991296", "849737964090556488"
+ ],
+ premium_since: null,
+ pending: false,
+ nick: null,
+ mute: false,
+ joined_at: "2015-11-11T09:55:40.321000+00:00",
+ flags: 0,
+ deaf: false,
+ communication_disabled_until: null,
+ avatar: null
+ },
+ id: "1128054143064494233",
+ flags: 0,
+ embeds: [],
+ edited_timestamp: null,
+ content: "<#1100319550446252084>",
+ components: [],
+ channel_id: "266767590641238027",
+ author: {
+ username: "kumaccino",
+ public_flags: 128,
+ id: "113340068197859328",
+ global_name: "kumaccino",
+ discriminator: "0",
+ avatar_decoration: null,
+ avatar: "b48302623a12bc7c59a71328f72ccb39"
+ },
+ attachments: [],
+ guild_id: "112760669178241024"
+ },
+ nicked_room_mention: {
type: 0,
tts: false,
timestamp: "2023-07-10T20:04:25.939000+00:00",
@@ -1990,6 +2047,91 @@ module.exports = {
edited_timestamp: null,
flags: 0,
components: []
+ },
+ embed_will_be_generated_on_reply: {
+ type: 19,
+ content: "https://matrix.to/#/!BnKuBPCvyfOkhcUjEu:cadence.moe/$aLVZyiC3HlOu-prCSIaXlQl68I8leUdnPFiCwkgn6qM",
+ mentions: [],
+ mention_roles: [],
+ attachments: [],
+ embeds: [
+ {
+ type: "link",
+ url: "https://matrix.to/",
+ title: "Matrix - Decentralised and secure communication",
+ description: "You're invited to talk on Matrix. If you don't already have a client this link will help you pick one, and join the conversation. If you already have one, this link will help you join the conversation",
+ reference_id: "1278002262400176128",
+ thumbnail: {
+ url: "https://matrix.org/blog/img/matrix-logo.png",
+ proxy_url: "https://images-ext-1.discordapp.net/external/3yPmfN-_U_7Xn8hLSG77nY9IvdtITH0GPrB6OX3JjEI/https/matrix.org/blog/img/matrix-logo.png",
+ width: 800,
+ height: 400,
+ placeholder: "OAgOBIComJeHeId/dXgAAAAAAA==",
+ placeholder_version: 1
+ }
+ }
+ ],
+ timestamp: "2024-08-27T14:44:43.490000+00:00",
+ edited_timestamp: null,
+ flags: 0,
+ components: [],
+ id: "1278002262400176128",
+ channel_id: "1100319550446252084",
+ author: {
+ id: "772659086046658620",
+ username: "cadence.worm",
+ avatar: "4b5c4b28051144e4c111f0113a0f1cf1",
+ discriminator: "0",
+ public_flags: 0,
+ flags: 0,
+ banner: null,
+ accent_color: null,
+ global_name: "cadence",
+ avatar_decoration_data: null,
+ banner_color: null,
+ clan: null
+ },
+ pinned: false,
+ mention_everyone: false,
+ tts: false,
+ message_reference: {
+ type: 0,
+ channel_id: "1100319550446252084",
+ message_id: "1278001833876525057",
+ guild_id: "1100319549670301727"
+ },
+ position: 0,
+ referenced_message: {
+ type: 0,
+ content: "b",
+ mentions: [],
+ mention_roles: [],
+ attachments: [],
+ embeds: [],
+ timestamp: "2024-08-27T14:43:01.322000+00:00",
+ edited_timestamp: "2024-08-27T14:43:06.277000+00:00",
+ flags: 0,
+ components: [],
+ id: "1278001833876525057",
+ channel_id: "1100319550446252084",
+ author: {
+ id: "772659086046658620",
+ username: "cadence.worm",
+ avatar: "4b5c4b28051144e4c111f0113a0f1cf1",
+ discriminator: "0",
+ public_flags: 0,
+ flags: 0,
+ banner: null,
+ accent_color: null,
+ global_name: "cadence",
+ avatar_decoration_data: null,
+ banner_color: null,
+ clan: null
+ },
+ pinned: false,
+ mention_everyone: false,
+ tts: false
+ }
}
},
pk_message: {
@@ -3709,6 +3851,70 @@ module.exports = {
],
guild_id: "112760669178241024",
id: "1210387798297682020"
+ },
+ embed_generated_on_reply: {
+ attachments: [],
+ author: {
+ avatar: "4b5c4b28051144e4c111f0113a0f1cf1",
+ avatar_decoration_data: null,
+ clan: null,
+ discriminator: "0",
+ global_name: "cadence",
+ id: "772659086046658620",
+ public_flags: 0,
+ username: "cadence.worm"
+ },
+ channel_id: "1100319550446252084",
+ components: [],
+ content: "https://matrix.to/#/!BnKuBPCvyfOkhcUjEu:cadence.moe/$aLVZyiC3HlOu-prCSIaXlQl68I8leUdnPFiCwkgn6qM",
+ edited_timestamp: null,
+ embeds: [
+ {
+ description: "You're invited to talk on Matrix. If you don't already have a client this link will help you pick one, and join the conversation. If you already have one, this link will help you join the conversation",
+ reference_id: "1278002262400176128",
+ thumbnail: {
+ height: 400,
+ placeholder: "OAgOBIComJeHeId/dXgAAAAAAA==",
+ placeholder_version: 1,
+ proxy_url: "https://images-ext-1.discordapp.net/external/3yPmfN-_U_7Xn8hLSG77nY9IvdtITH0GPrB6OX3JjEI/https/matrix.org/blog/img/matrix-logo.png",
+ url: "https://matrix.org/blog/img/matrix-logo.png",
+ width: 800
+ },
+ title: "Matrix - Decentralised and secure communication",
+ type: "link",
+ url: "https://matrix.to/"
+ }
+ ],
+ flags: 0,
+ guild_id: "1100319549670301727",
+ id: "1278002262400176128",
+ member: {
+ avatar: null,
+ banner: null,
+ communication_disabled_until: null,
+ deaf: false,
+ flags: 0,
+ joined_at: "2023-04-25T07:17:03.696000+00:00",
+ mute: false,
+ nick: "worm",
+ pending: false,
+ premium_since: null,
+ roles: []
+ },
+ mention_everyone: false,
+ mention_roles: [],
+ mentions: [],
+ message_reference: {
+ channel_id: "1100319550446252084",
+ guild_id: "1100319549670301727",
+ message_id: "1278001833876525057",
+ type: 0
+ },
+ pinned: false,
+ position: 0,
+ timestamp: "2024-08-27T14:44:43.490000+00:00",
+ tts: false,
+ type: 19
}
},
special_message: {
diff --git a/test/ooye-test-data.sql b/test/ooye-test-data.sql
index 4666b4d..2c23561 100644
--- a/test/ooye-test-data.sql
+++ b/test/ooye-test-data.sql
@@ -24,13 +24,15 @@ INSERT INTO sim (user_id, sim_name, localpart, mxid) VALUES
('111604486476181504', 'kyuugryphon', '_ooye_kyuugryphon', '@_ooye_kyuugryphon:cadence.moe'),
('1109360903096369153', 'amanda', '_ooye_amanda', '@_ooye_amanda:cadence.moe'),
('43d378d5-1183-47dc-ab3c-d14e21c3fe58', '_pk_zoego', '_ooye__pk_zoego', '@_ooye__pk_zoego:cadence.moe'),
-('320067006521147393', 'papiophidian', '_ooye_papiophidian', '@_ooye_papiophidian:cadence.moe');
+('320067006521147393', 'papiophidian', '_ooye_papiophidian', '@_ooye_papiophidian:cadence.moe'),
+('772659086046658620', 'cadence', '_ooye_cadence', '@_ooye_cadence:cadence.moe');
INSERT INTO sim_proxy (user_id, proxy_owner_id, displayname) VALUES
('43d378d5-1183-47dc-ab3c-d14e21c3fe58', '196188877885538304', 'Azalea &flwr; 🌺');
INSERT INTO sim_member (mxid, room_id, hashed_profile_content) VALUES
-('@_ooye_bojack_horseman:cadence.moe', '!hYnGGlPHlbujVVfktC:cadence.moe', NULL);
+('@_ooye_bojack_horseman:cadence.moe', '!hYnGGlPHlbujVVfktC:cadence.moe', NULL),
+('@_ooye_cadence:cadence.moe', '!BnKuBPCvyfOkhcUjEu:cadence.moe', NULL);
INSERT INTO message_channel (message_id, channel_id) VALUES
('1106366167788044450', '122155380120748034'),
@@ -57,7 +59,9 @@ INSERT INTO message_channel (message_id, channel_id) VALUES
('1207486471489986620', '1160894080998461480'),
('1210387798297682020', '112760669178241024'),
('1273204543739396116', '687028734322147344'),
-('1273743950028607530', '1100319550446252084');
+('1273743950028607530', '1100319550446252084'),
+('1278002262400176128', '1100319550446252084'),
+('1278001833876525057', '1100319550446252084');
INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, reaction_part, source) VALUES
('$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg', 'm.room.message', 'm.text', '1126786462646550579', 0, 0, 1),
@@ -94,7 +98,9 @@ INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part
('$OEEK-Wam2FTh6J-6kVnnJ6KnLA_lLRnLTHatKKL62-Y', 'm.room.message', 'm.image', '1207486471489986620', 0, 0, 0),
('$mPSzglkCu-6cZHbYro0RW2u5mHvbH9aXDjO5FCzosc0', 'm.room.message', 'm.text', '1210387798297682020', 0, 0, 1),
('$qmyjr-ISJtnOM5WTWLI0fT7uSlqRLgpyin2d2NCglCU', 'm.room.message', 'm.text', '1273204543739396116', 0, 0, 0),
-('$W1nsDhNIojWrcQOdnOD9RaEvrz2qyZErQoNhPRs1nK4', 'm.room.message', 'm.text', '1273743950028607530', 0, 0, 0);
+('$W1nsDhNIojWrcQOdnOD9RaEvrz2qyZErQoNhPRs1nK4', 'm.room.message', 'm.text', '1273743950028607530', 0, 0, 0),
+('$UTqiL3Zj3FC4qldxRLggN1fhygpKl8sZ7XGY5f9MNbF', 'm.room.message', 'm.text', '1278002262400176128', 0, 0, 1),
+('$aLVZyiC3HlOu-prCSIaXlQl68I8leUdnPFiCwkgn6qM', 'm.room.message', 'm.text', '1278001833876525057', 0, 0, 1);
INSERT INTO file (discord_url, mxc_url) VALUES
('https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png', 'mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM'),
@@ -142,6 +148,9 @@ INSERT INTO member_cache (room_id, mxid, displayname, avatar_url, power_level) V
('!kLRqKKUQXcibIMtOpl:cadence.moe', '@test_auto_invite:example.org', NULL, NULL, 0),
('!BpMdOUkWWhFxmTrENV:cadence.moe', '@test_auto_invite:example.org', NULL, NULL, 100);
+INSERT INTO reaction (hashed_event_id, message_id, encoded_emoji) VALUES
+(5162930312280790092, '1141501302736695317', '%F0%9F%90%88');
+
INSERT INTO member_power (mxid, room_id, power_level) VALUES
('@test_auto_invite:example.org', '*', 100);