diff --git a/indexedDb/implementation.go b/indexedDb/implementation.go index d9d2f7026d246f95a5e45def3863083805ae9434..cf26157e385409424549b323365847cbaaf178f9 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 1de2f45703ac07ff1d1bebd7bdec959d006fe7ef..96add4dc35c8665d15369732058ff1dab6cdf68f 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) }