From 38c3208b76a3ffbe42d8eb0a107f4ddec297d6eb Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 2 Jul 2024 13:48:42 -0500 Subject: [PATCH] Fix message corrections --- snikket/Chat.hx | 6 ++++-- snikket/ChatMessage.hx | 5 +++++ snikket/persistence/browser.js | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/snikket/Chat.hx b/snikket/Chat.hx index 27c6b35..80706d8 100644 --- a/snikket/Chat.hx +++ b/snikket/Chat.hx @@ -514,8 +514,9 @@ class DirectChat extends Chat { @HaxeCBridge.noemit // on superclass as abstract public function correctMessage(localId:String, message:ChatMessage) { - final toSend = message.clone(); + final toSend = prepareOutgoingMessage(message.clone()); message = prepareOutgoingMessage(message); + message.resetLocalId(); message.versions = [toSend]; // This is a correction message.localId = localId; persistence.storeMessage(client.accountId(), message, (corrected) -> { @@ -869,8 +870,9 @@ class Channel extends Chat { @HaxeCBridge.noemit // on superclass as abstract public function correctMessage(localId:String, message:ChatMessage) { - final toSend = message.clone(); + final toSend = prepareOutgoingMessage(message.clone()); message = prepareOutgoingMessage(message); + message.resetLocalId(); message.versions = [toSend]; // This is a correction message.localId = localId; persistence.storeMessage(client.accountId(), message, (corrected) -> { diff --git a/snikket/ChatMessage.hx b/snikket/ChatMessage.hx index 46de52c..6558de9 100644 --- a/snikket/ChatMessage.hx +++ b/snikket/ChatMessage.hx @@ -194,6 +194,11 @@ class ChatMessage { return this.timestamp = timestamp; } + @:allow(snikket) + private function resetLocalId() { + Reflect.setField(this, "localId", null); + } + /** Get HTML version of the message body **/ diff --git a/snikket/persistence/browser.js b/snikket/persistence/browser.js index a18fcfa..51cdaea 100644 --- a/snikket/persistence/browser.js +++ b/snikket/persistence/browser.js @@ -317,7 +317,7 @@ const browser = (dbname) => { const tx = db.transaction(["messages", "reactions"], "readwrite"); const store = tx.objectStore("messages"); return promisifyRequest(store.index("localId").openCursor(IDBKeyRange.only([account, message.localId || [], message.chatId()]))).then((result) => { - if (result?.value && !message.isIncoming() && result?.value.direction === enums.MessageDirection.MessageSent) { + if (result?.value && !message.isIncoming() && result?.value.direction === enums.MessageDirection.MessageSent && message.versions.length < 1) { // Duplicate, we trust our own sent ids return promisifyRequest(result.delete()); } else if (result?.value && result.value.sender == message.senderId() && (message.versions.length > 0 || (result.value.versions || []).length > 0)) {