From ee266f844a673f47e6328dd1022e5ee18e107de6 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 28 Aug 2024 17:18:50 +1200 Subject: [PATCH] Move reaction_part to new events when possible --- d2m/actions/edit-message.js | 2 +- d2m/converters/edit-to-changes.js | 12 +++++++++++- d2m/converters/edit-to-changes.test.js | 20 +++++++++++++++++--- test/ooye-test-data.sql | 3 +++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/d2m/actions/edit-message.js b/d2m/actions/edit-message.js index d52fcbde..d85f925c 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 6bb22106..f93c510b 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -122,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) @@ -142,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 1d8b7300..effb7773 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,14 @@ test("edit2changes: generated embed", async t => { + `
  • Both players present their best five-or-less-card pok...
  • `, "m.mentions": {} }]) - t.deepEqual(promotions, []) // TODO: it would be ideal to promote this to reaction_part = 0. this is OK to do because the main message won't have had any reactions yet. + t.deepEqual(promotions, [{ + "column": "reaction_part", + "eventID": "$mPSzglkCu-6cZHbYro0RW2u5mHvbH9aXDjO5FCzosc0", + "value": 1, + }, { + "column": "reaction_part", + "nextEvent": true, + }]) t.equal(senderMxid, "@_ooye_cadence:cadence.moe") t.equal(called, 1) }) @@ -308,6 +315,13 @@ test("edit2changes: generated embed on a reply", async t => { + `

    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

    `, "m.mentions": {} }]) - t.deepEqual(promotions, []) // TODO: it would be ideal to promote this to reaction_part = 0. this is OK to do because the main message won't have had any reactions yet. + 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/test/ooye-test-data.sql b/test/ooye-test-data.sql index 07be17f0..2c235613 100644 --- a/test/ooye-test-data.sql +++ b/test/ooye-test-data.sql @@ -148,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);