From 65dd63b86bdb2ddb5af85985c0d7001fce37f1f0 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Wed, 5 Oct 2022 17:21:02 +0000
Subject: [PATCH] Modify the fallback logic in receiveHelper to work even if we
 don't get errors like we expect from it

---
 indexedDb/implementation.go | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/indexedDb/implementation.go b/indexedDb/implementation.go
index 0d536811..b97037cd 100644
--- a/indexedDb/implementation.go
+++ b/indexedDb/implementation.go
@@ -382,16 +382,21 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64,
 	err = txn.Await(ctx)
 	cancel()
 	if err != nil {
-		err = errors.Errorf("Upserting Message failed: %+v", err)
+		return 0, errors.Errorf("Upserting Message failed: %+v", err)
+	}
+	res, err := addReq.Result()
+	// NOTE: Sometimes the insert fails to return an error but hits
+	// a duplicate insert, so this fallthrough returns the uuid entry in
+	// that case.
+	if res.IsUndefined() {
 		msgID := cryptoChannel.MessageID{}
 		copy(msgID[:], newMessage.MessageID)
-		uuid, _ := w.msgIDLookup(msgID)
-		if uuid == 0 {
+		uuid, errLookup := w.msgIDLookup(msgID)
+		if uuid != 0 && errLookup == nil {
 			return uuid, nil
 		}
-		return 0, err
+		return 0, errors.Errorf("uuid lookup failure: %+v", err)
 	}
-	res, _ := addReq.Result()
 	uuid := uint64(res.Int())
 	jww.DEBUG.Printf(
 		"Successfully stored message from %s, id %d",
-- 
GitLab