From 23d85547f34b7998a4eddeebb3051aff7de1125c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 17 Mar 2024 01:07:50 +1300 Subject: [PATCH] Send generated embeds as original user --- d2m/converters/edit-to-changes.js | 15 ++++++++++++++- d2m/converters/edit-to-changes.test.js | 12 +++++++++++- d2m/event-dispatcher.js | 3 ++- scripts/capture-message-update-events.js | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/d2m/converters/edit-to-changes.js b/d2m/converters/edit-to-changes.js index ef170ca4..dd8e39f8 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -6,6 +6,8 @@ const passthrough = require("../../passthrough") const {sync, select, from} = passthrough /** @type {import("./message-to-event")} */ const messageToEvent = sync.require("../converters/message-to-event") +/** @type {import("../../m2d/converters/utils")} */ +const utils = sync.require("../../m2d/converters/utils") function eventCanBeEdited(ev) { // Discord does not allow files, images, attachments, or videos to be edited. @@ -37,7 +39,18 @@ async function editToChanges(message, guild, api) { const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get() assert(roomID) /** @type {string?} Null if we don't have a sender in the room, which will happen if it's a webhook's message. The bridge bot will do the edit instead. */ - const senderMxid = message.author && from("sim").join("sim_member", "mxid").where({user_id: message.author.id, room_id: roomID}).pluck("mxid").get() || null + let senderMxid = null + if (message.author) { + senderMxid = from("sim").join("sim_member", "mxid").where({user_id: message.author.id, room_id: roomID}).pluck("mxid").get() || null + } else { + // Should be a system generated embed. We want the embed to be sent by the same user who sent the message, so that the messages get grouped in most clients. + const eventID = select("event_message", "event_id", {message_id: message.id}).pluck().get() + assert(eventID) // this should have been checked earlier in a calling function + const event = await api.getEvent(roomID, eventID) + if (utils.eventSenderIsFromDiscord(event.sender)) { + senderMxid = event.sender + } + } const oldEventRows = select("event_message", ["event_id", "event_type", "event_subtype", "part", "reaction_part"], {message_id: message.id}).all() diff --git a/d2m/converters/edit-to-changes.test.js b/d2m/converters/edit-to-changes.test.js index 1004c347..b00a2c40 100644 --- a/d2m/converters/edit-to-changes.test.js +++ b/d2m/converters/edit-to-changes.test.js @@ -237,7 +237,15 @@ test("edit2changes: promotes the text event when multiple rows have part = 1 (sh }) test("edit2changes: generated embed", async t => { - const {eventsToRedact, eventsToReplace, eventsToSend, promotions} = await editToChanges(data.message_update.embed_generated_social_media_image, data.guild.general, {}) + let called = 0 + const {senderMxid, eventsToRedact, eventsToReplace, eventsToSend, promotions} = await editToChanges(data.message_update.embed_generated_social_media_image, data.guild.general, { + async getEvent(roomID, eventID) { + called++ + t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") + t.equal(eventID, "$mPSzglkCu-6cZHbYro0RW2u5mHvbH9aXDjO5FCzosc0") + return {sender: "@_ooye_cadence:cadence.moe"} + } + }) t.deepEqual(eventsToRedact, []) t.deepEqual(eventsToReplace, []) t.deepEqual(eventsToSend, [{ @@ -259,4 +267,6 @@ test("edit2changes: generated embed", async t => { "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.equal(senderMxid, "@_ooye_cadence:cadence.moe") + t.equal(called, 1) }) diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 2c4dcd51..54d858af 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -268,7 +268,8 @@ module.exports = { // Based on looking at data they've sent me over the gateway, this is the best way to check for meaningful changes. // If the message content is a string then it includes all interesting fields and is meaningful. - if (typeof data.content === "string") { + // Otherwise, if there are embeds, then the system generated URL preview embeds. + if (typeof data.content === "string" || "embeds" in data) { /** @type {DiscordTypes.GatewayMessageCreateDispatchData} */ // @ts-ignore const message = data diff --git a/scripts/capture-message-update-events.js b/scripts/capture-message-update-events.js index f345d3fb..671b0625 100644 --- a/scripts/capture-message-update-events.js +++ b/scripts/capture-message-update-events.js @@ -37,7 +37,7 @@ passthrough.discord = discord })() const events = new sqlite("scripts/events.db") -const sql = "INSERT INTO \"update\" (json, " + interestingFields.join(", ") + ") VALUES (" + "?".repeat(interestingFields.length + 1).split("").join(", ") + ")" +const sql = "INSERT INTO update_event (json, " + interestingFields.join(", ") + ") VALUES (" + "?".repeat(interestingFields.length + 1).split("").join(", ") + ")" console.log(sql) const prepared = events.prepare(sql)