From 19ddcd63d145cfd6ba6abf090c215375bec3a4f2 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Mon, 12 Jun 2023 21:04:34 +0000
Subject: [PATCH] Do not error out when this message already exists inside the
 DB. Instead, set the ID and re-attempt as an upsert.

---
 indexedDb/impl/channels/implementation.go | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/indexedDb/impl/channels/implementation.go b/indexedDb/impl/channels/implementation.go
index dfc000eb..91dd1cbf 100644
--- a/indexedDb/impl/channels/implementation.go
+++ b/indexedDb/impl/channels/implementation.go
@@ -435,6 +435,22 @@ func (w *wasmModel) upsertMessage(msg *Message) (uint64, error) {
 	// Store message to database
 	msgIdObj, err := impl.Put(w.db, messageStoreName, messageObj)
 	if err != nil {
+		// Do not error out when this message already exists inside
+		// the DB. Instead, set the ID and re-attempt as an upsert.
+		if msg.ID == 0 { // always error out when not an insert attempt
+			msgID, inErr := message.UnmarshalID(msg.MessageID)
+			if inErr == nil {
+				jww.WARN.Printf("upsertMessage duplicate: %+v",
+					err)
+				existingMsg, inErr := w.GetMessage(msgID)
+				if inErr == nil && existingMsg.UUID != 0 {
+					msg.ID = existingMsg.UUID
+					return w.upsertMessage(msg)
+				}
+				jww.ERROR.Printf("upsertMessage no UUID: %+v",
+					inErr)
+			}
+		}
 		return 0, errors.Errorf("Unable to put Message: %+v\n%s",
 			err, newMessageJson)
 	}
-- 
GitLab