Skip to content
Snippets Groups Projects
Commit f9d0310f authored by Richard T. Carback III's avatar Richard T. Carback III Committed by Jono Wenger
Browse files

WIP: Add uuid

parent 8abe1554
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!8Updates to match the client fullyDecentrilizedChannels branch
......@@ -11,7 +11,6 @@ package indexedDb
import (
"context"
"encoding/base64"
"encoding/json"
"syscall/js"
"time"
......@@ -152,13 +151,13 @@ func (w *wasmModel) ReceiveMessage(channelID *id.ID,
status channels.SentStatus) uint64 {
parentErr := errors.New("failed to ReceiveMessage")
err := w.receiveHelper(buildMessage(channelID.Marshal(),
uuid, err := w.receiveHelper(buildMessage(channelID.Marshal(),
messageID.Bytes(), nil, nickname, text, identity,
timestamp, lease, status))
if err != nil {
jww.ERROR.Printf("%+v", errors.Wrap(parentErr, err.Error()))
}
return 0
return uuid
}
// ReceiveReply is called whenever a message is received that is a reply on a
......@@ -174,13 +173,13 @@ func (w *wasmModel) ReceiveReply(channelID *id.ID,
lease time.Duration, round rounds.Round, status channels.SentStatus) uint64 {
parentErr := errors.New("failed to ReceiveReply")
err := w.receiveHelper(buildMessage(channelID.Marshal(),
uuid, err := w.receiveHelper(buildMessage(channelID.Marshal(),
messageID.Bytes(), replyTo.Bytes(), nickname, text, identity,
timestamp, lease, status))
if err != nil {
jww.ERROR.Printf("%+v", errors.Wrap(parentErr, err.Error()))
}
return 0
return uuid
}
// ReceiveReaction is called whenever a reaction to a message is received on a
......@@ -195,13 +194,13 @@ func (w *wasmModel) ReceiveReaction(channelID *id.ID, messageID cryptoChannel.Me
lease time.Duration, round rounds.Round, status channels.SentStatus) uint64 {
parentErr := errors.New("failed to ReceiveReaction")
err := w.receiveHelper(buildMessage(channelID.Marshal(),
uuid, err := w.receiveHelper(buildMessage(channelID.Marshal(),
messageID.Bytes(), reactionTo.Bytes(), nickname, reaction,
identity, timestamp, lease, status))
if err != nil {
jww.ERROR.Printf("%+v", errors.Wrap(parentErr, err.Error()))
}
return 0
return uuid
}
// UpdateSentStatus is called whenever the [channels.SentStatus] of a message
......@@ -212,7 +211,7 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, messageID cryptoChannel.Messag
parentErr := errors.New("failed to UpdateSentStatus")
// Convert messageID to the key generated by json.Marshal
key := js.ValueOf(base64.StdEncoding.EncodeToString(messageID[:]))
key := js.ValueOf(uuid)
// Use the key to get the existing Message
currentMsg, err := w.get(messageStoreName, key)
......@@ -229,7 +228,7 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, messageID cryptoChannel.Messag
newMessage.Status = uint8(status)
// Store the updated Message
err = w.receiveHelper(newMessage)
_, err = w.receiveHelper(newMessage)
if err != nil {
jww.ERROR.Printf("%+v", errors.Wrap(parentErr, err.Error()))
}
......@@ -262,31 +261,32 @@ func buildMessage(channelID, messageID, parentID []byte, nickname,
}
// receiveHelper is a private helper for receiving any sort of message.
func (w *wasmModel) receiveHelper(newMessage *Message) error {
func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) {
// Convert to jsObject
newMessageJson, err := json.Marshal(newMessage)
if err != nil {
return errors.Errorf("Unable to marshal Message: %+v", err)
return 0, errors.Errorf("Unable to marshal Message: %+v", err)
}
messageObj, err := utils.JsonToJS(newMessageJson)
if err != nil {
return errors.Errorf("Unable to marshal Message: %+v", err)
return 0, errors.Errorf("Unable to marshal Message: %+v", err)
}
// Prepare the Transaction
txn, err := w.db.Transaction(idb.TransactionReadWrite, messageStoreName)
if err != nil {
return errors.Errorf("Unable to create Transaction: %+v", err)
return 0, errors.Errorf("Unable to create Transaction: %+v",
err)
}
store, err := txn.ObjectStore(messageStoreName)
if err != nil {
return errors.Errorf("Unable to get ObjectStore: %+v", err)
return 0, errors.Errorf("Unable to get ObjectStore: %+v", err)
}
// Perform the upsert (put) operation
_, err = store.Put(messageObj)
putReq, err := store.Put(messageObj)
if err != nil {
return errors.Errorf("Unable to upsert Message: %+v", err)
return 0, errors.Errorf("Unable to upsert Message: %+v", err)
}
// Wait for the operation to return
......@@ -294,12 +294,14 @@ func (w *wasmModel) receiveHelper(newMessage *Message) error {
err = txn.Await(ctx)
cancel()
if err != nil {
return errors.Errorf("Upserting Message failed: %+v", err)
return 0, errors.Errorf("Upserting Message failed: %+v", err)
}
res, _ := putReq.Result()
uuid := uint64(res.Int())
jww.DEBUG.Printf(
"Successfully stored message from %s",
newMessage.Codename)
return nil
"Successfully stored message from %s, id %d",
newMessage.Codename, uuid)
return uuid, nil
}
// get is a generic private helper for getting values from the given
......
......@@ -11,14 +11,16 @@ package indexedDb
import (
"encoding/json"
"os"
"testing"
"time"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/channels"
"gitlab.com/elixxir/client/cmix/rounds"
cryptoBroadcast "gitlab.com/elixxir/crypto/broadcast"
"gitlab.com/elixxir/crypto/channel"
"gitlab.com/xx_network/primitives/id"
"os"
"testing"
"time"
)
func TestMain(m *testing.M) {
......@@ -35,11 +37,13 @@ func TestWasmModel_UpdateSentStatus(t *testing.T) {
t.Fatalf("%+v", err)
}
cid := channel.Identity{}
// Store a test message
testMsg := buildMessage([]byte(testString), testMsgId.Bytes(),
nil, testString, testString, nil, time.Now(),
nil, testString, testString, cid, time.Now(),
time.Second, channels.Sent)
err = eventModel.receiveHelper(testMsg)
uuid, err := eventModel.receiveHelper(testMsg)
if err != nil {
t.Fatalf("%+v", err)
}
......@@ -55,7 +59,8 @@ func TestWasmModel_UpdateSentStatus(t *testing.T) {
// Update the sentStatus
expectedStatus := channels.Failed
eventModel.UpdateSentStatus(testMsgId, expectedStatus)
eventModel.UpdateSentStatus(uuid, testMsgId, time.Now(),
rounds.Round{ID: 8675309}, expectedStatus)
// Check the resulting status
results, err = eventModel.dump(messageStoreName)
......@@ -75,8 +80,8 @@ func TestWasmModel_UpdateSentStatus(t *testing.T) {
}
// Make sure other fields didn't change
if resultMsg.SenderUsername != testString {
t.Fatalf("Unexpected SenderUsername: %v", resultMsg.SenderUsername)
if resultMsg.Nickname != testString {
t.Fatalf("Unexpected Nickname: %v", resultMsg.Nickname)
}
}
......
......@@ -46,7 +46,7 @@ const (
// The user's nickname can change each message, but the rest does not. We
// still duplicate all of it for each entry to simplify code for now.
type Message struct {
ID []byte `json:"id"` // Matches pkeyName
ID uint64 `json:"id"` // Matches pkeyName
Nickname string `json:"nickname"`
MessageID []byte `json:"message_id"` // Index
ChannelID []byte `json:"channel_id"` // Index
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment