Move reaction_part to new events when possible

This commit is contained in:
Cadence Ember 2024-08-28 17:18:50 +12:00
parent 65de8aaf98
commit ee266f844a
4 changed files with 32 additions and 5 deletions

View file

@ -53,7 +53,7 @@ async function editMessage(message, guild, row) {
const sendNewEventParts = new Set() const sendNewEventParts = new Set()
for (const promotion of promotions) { for (const promotion of promotions) {
if ("eventID" in promotion) { 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) { } else if ("nextEvent" in promotion) {
sendNewEventParts.add(promotion.column) sendNewEventParts.add(promotion.column)
} }

View file

@ -122,7 +122,7 @@ async function editToChanges(message, guild, api) {
eventsToReplace = eventsToReplace.filter(eventCanBeEdited) eventsToReplace = eventsToReplace.filter(eventCanBeEdited)
// We want to maintain exactly one part = 0 and one reaction_part = 0 database row at all times. // 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 = [] const promotions = []
for (const column of ["part", "reaction_part"]) { for (const column of ["part", "reaction_part"]) {
const candidatesForParts = unchangedEvents.concat(eventsToReplace) const candidatesForParts = unchangedEvents.concat(eventsToReplace)
@ -142,6 +142,16 @@ async function editToChanges(message, guild, api) {
promotions.push({column, nextEvent: true}) 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 // Removing unnecessary properties before returning

View file

@ -109,7 +109,7 @@ test("edit2changes: change file type", async t => {
t.deepEqual(promotions, [{column: "part", nextEvent: true}, {column: "reaction_part", nextEvent: true}]) 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, {}) const {eventsToRedact, eventsToReplace, eventsToSend, promotions} = await editToChanges(data.message_update.added_caption_to_image, data.guild.general, {})
t.deepEqual(eventsToRedact, []) t.deepEqual(eventsToRedact, [])
t.deepEqual(eventsToSend, [{ t.deepEqual(eventsToSend, [{
@ -266,7 +266,14 @@ test("edit2changes: generated embed", async t => {
+ `</li><li>Both players present their best five-or-less-card pok...</li></ul></p></blockquote>`, + `</li><li>Both players present their best five-or-less-card pok...</li></ul></p></blockquote>`,
"m.mentions": {} "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(senderMxid, "@_ooye_cadence:cadence.moe")
t.equal(called, 1) t.equal(called, 1)
}) })
@ -308,6 +315,13 @@ test("edit2changes: generated embed on a reply", async t => {
+ `</p><p>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</p></blockquote>`, + `</p><p>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</p></blockquote>`,
"m.mentions": {} "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") t.equal(senderMxid, "@_ooye_cadence:cadence.moe")
}) })

View file

@ -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), ('!kLRqKKUQXcibIMtOpl:cadence.moe', '@test_auto_invite:example.org', NULL, NULL, 0),
('!BpMdOUkWWhFxmTrENV:cadence.moe', '@test_auto_invite:example.org', NULL, NULL, 100); ('!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 INSERT INTO member_power (mxid, room_id, power_level) VALUES
('@test_auto_invite:example.org', '*', 100); ('@test_auto_invite:example.org', '*', 100);