From ba904f8ea7f4394574aa7a268fd481af72965555 Mon Sep 17 00:00:00 2001 From: Jake Taylor <jake@elixxir.io> Date: Mon, 24 Oct 2022 17:35:36 -0500 Subject: [PATCH] added update flag for message inserts to prevent weird PK logic --- indexedDb/implementation.go | 15 +++++++-------- indexedDb/implementation_test.go | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/indexedDb/implementation.go b/indexedDb/implementation.go index d9d2f702..cf26157e 100644 --- a/indexedDb/implementation.go +++ b/indexedDb/implementation.go @@ -221,7 +221,7 @@ func (w *wasmModel) ReceiveMessage(channelID *id.ID, channelID.Marshal(), messageID.Bytes(), nil, nickname, text, pubKey, codeset, timestamp, lease, round.ID, mType, status) - uuid, err := w.receiveHelper(msgToInsert) + uuid, err := w.receiveHelper(msgToInsert, false) if err != nil { jww.ERROR.Printf("Failed to receive Message: %+v", err) } @@ -246,7 +246,7 @@ func (w *wasmModel) ReceiveReply(channelID *id.ID, replyTo.Bytes(), nickname, text, pubKey, codeset, timestamp, lease, round.ID, mType, status) - uuid, err := w.receiveHelper(msgToInsert) + uuid, err := w.receiveHelper(msgToInsert, false) if err != nil { jww.ERROR.Printf("Failed to receive reply: %+v", err) @@ -271,7 +271,7 @@ func (w *wasmModel) ReceiveReaction(channelID *id.ID, channelID.Marshal(), messageID.Bytes(), reactionTo.Bytes(), nickname, reaction, pubKey, codeset, timestamp, lease, round.ID, mType, status) - uuid, err := w.receiveHelper(msgToInsert) + uuid, err := w.receiveHelper(msgToInsert, false) if err != nil { jww.ERROR.Printf("Failed to receive reaction: %+v", err) } @@ -324,7 +324,7 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, } // Store the updated Message - _, err = w.receiveHelper(newMessage) + _, err = w.receiveHelper(newMessage, true) if err != nil { jww.ERROR.Printf("%+v", errors.Wrap(parentErr, err.Error())) } @@ -363,7 +363,7 @@ func buildMessage(channelID, messageID, parentID []byte, nickname, text string, } // receiveHelper is a private helper for receiving any sort of message. -func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, +func (w *wasmModel) receiveHelper(newMessage *Message, isUpdate bool) (uint64, error) { // Convert to jsObject newMessageJson, err := json.Marshal(newMessage) @@ -375,9 +375,8 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, return 0, errors.Errorf("Unable to marshal Message: %+v", err) } - // NOTE: This is weird, but correct. When the "ID" field is 0, we unset it - // from the JSValue so that it is auto-populated and incremented. - if newMessage.ID == 0 { + // Unset the primaryKey for inserts so it can be auto-populated and incremented. + if !isUpdate { messageObj.JSValue().Delete("id") } diff --git a/indexedDb/implementation_test.go b/indexedDb/implementation_test.go index 1de2f457..96add4dc 100644 --- a/indexedDb/implementation_test.go +++ b/indexedDb/implementation_test.go @@ -48,7 +48,7 @@ func Test_wasmModel_UpdateSentStatus(t *testing.T) { testMsg := buildMessage([]byte(testString), testMsgId.Bytes(), nil, testString, testString, []byte{8, 6, 7, 5}, 0, netTime.Now(), time.Second, 0, 0, channels.Sent) - uuid, err := eventModel.receiveHelper(testMsg) + uuid, err := eventModel.receiveHelper(testMsg, false) if err != nil { t.Fatalf("%+v", err) } @@ -287,13 +287,13 @@ func TestWasmModel_receiveHelper_UniqueIndex(t *testing.T) { testMsg := buildMessage([]byte(testString), testMsgId.Bytes(), nil, testString, testString, []byte{8, 6, 7, 5}, 0, netTime.Now(), time.Second, 0, 0, channels.Sent) - _, err = eventModel.receiveHelper(testMsg) + _, err = eventModel.receiveHelper(testMsg, false) if err != nil { t.Fatal(err) } // The duplicate entry won't fail, it just silently shouldn't happen - _, err = eventModel.receiveHelper(testMsg) + _, err = eventModel.receiveHelper(testMsg, true) if err != nil { t.Fatalf("%+v", err) } -- GitLab