Skip to content
Snippets Groups Projects
Commit 0aa974cc authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

WIP: Add uuid

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