diff --git a/indexedDb/channels/implementation.go b/indexedDb/channels/implementation.go index fd3b0db795f6e6735066c6d93638a19766ed00b0..6ac4d47db8be84f2ab56a648f2bcd41a168b0317 100644 --- a/indexedDb/channels/implementation.go +++ b/indexedDb/channels/implementation.go @@ -13,6 +13,7 @@ import ( "crypto/ed25519" "encoding/base64" "encoding/json" + "strings" "sync" "syscall/js" "time" @@ -423,7 +424,10 @@ func (w *wasmModel) receiveHelper(newMessage *Message, isUpdate bool) (uint64, // Store message to database result, err := indexedDb.Put(w.db, messageStoreName, messageObj) - if err != nil { + if err != nil && !strings.Contains(err.Error(), + "at least one key does not satisfy the uniqueness requirements") { + // Only return non-unique constraint errors so that the case + // below this one can be hit and handle duplicate entries properly. return 0, errors.Errorf("Unable to put Message: %+v", err) } diff --git a/indexedDb/channels/implementation_test.go b/indexedDb/channels/implementation_test.go index 5f5fbebadc8d99d48cdca14bc1240314b1de7413..92d9b62204bf5e7f7b255fc34c79ba626259e180 100644 --- a/indexedDb/channels/implementation_test.go +++ b/indexedDb/channels/implementation_test.go @@ -446,15 +446,18 @@ func TestWasmModel_receiveHelper_UniqueIndex(t *testing.T) { testMsg := buildMessage([]byte(testString), testMsgId.Bytes(), nil, testString, []byte(testString), []byte{8, 6, 7, 5}, 0, 0, netTime.Now(), time.Second, 0, 0, false, false, channels.Sent) - _, err = eventModel.receiveHelper(testMsg, false) + uuid, err := eventModel.receiveHelper(testMsg, false) if err != nil { t.Fatal(err) } - // The duplicate entry should fail - _, err = eventModel.receiveHelper(testMsg, false) - if err == nil { - t.Fatalf("Expected duplicate insert to fail!") + // The duplicate entry should return the same UUID + duplicateUuid, err := eventModel.receiveHelper(testMsg, false) + if err != nil { + t.Fatal(err) + } + if uuid != duplicateUuid { + t.Fatalf("Expected UUID %d to match %d", uuid, duplicateUuid) } // Now insert a message with a different message ID from the first @@ -462,18 +465,24 @@ func TestWasmModel_receiveHelper_UniqueIndex(t *testing.T) { testMsg = buildMessage([]byte(testString), testMsgId2.Bytes(), nil, testString, []byte(testString), []byte{8, 6, 7, 5}, 0, 0, netTime.Now(), time.Second, 0, 0, false, false, channels.Sent) - primaryKey, err := eventModel.receiveHelper(testMsg, false) + uuid2, err := eventModel.receiveHelper(testMsg, false) if err != nil { t.Fatal(err) } + if uuid2 == uuid { + t.Fatalf("Expected UUID %d to NOT match %d", uuid, duplicateUuid) + } // Except this time, we update the second entry to have the same - // message ID as the first, which needs to fail - testMsg.ID = primaryKey + // message ID as the first + testMsg.ID = uuid testMsg.MessageID = testMsgId.Bytes() - _, err = eventModel.receiveHelper(testMsg, true) - if err == nil { - t.Fatal("Expected duplicate update to fail!") + duplicateUuid2, err := eventModel.receiveHelper(testMsg, true) + if err != nil { + t.Fatal(err) + } + if duplicateUuid2 != duplicateUuid { + t.Fatalf("Expected UUID %d to match %d", uuid, duplicateUuid) } }) }