diff --git a/client/client.go b/client/client.go index 19d4235..4e28a0e 100644 --- a/client/client.go +++ b/client/client.go @@ -95,11 +95,10 @@ func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) (s c.Lock() defer c.Unlock() - lastMessage := c.lastMessages[chat] msg := &protobuf.Message{ MessageType: protobuf.Message_MessageType(t), Body: body, - PreviousMessage: lastMessage[:], + PreviousMessage: c.lastMessage(chat), } err := crypto.Sign(c.identity, msg) @@ -173,3 +172,12 @@ func (c *Client) handlePreviousMessage(group state.GroupID, previousMessage stat log.Printf("error while requesting message: %s", err.Error()) } } + +func (c *Client) lastMessage(chat Chat) []byte { + last, ok := c.lastMessages[chat] + if !ok { + return nil + } + + return last[:] +} diff --git a/client/client_test.go b/client/client_test.go index ecff605..609b0e4 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -18,6 +18,7 @@ import ( "github.com/vacp2p/dasy/event" "github.com/vacp2p/dasy/protobuf" mvdsproto "github.com/vacp2p/mvds/protobuf" + "github.com/vacp2p/mvds/state" ) func TestMain(m *testing.M) { @@ -27,6 +28,30 @@ func TestMain(m *testing.M) { // @todo think about turning feed into an interface so we can mock it and ensure its never called when sigs fail +func TestClient_Post(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + node := internal.NewMockDataSyncNode(ctrl) + identity, _ := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader) + + client := Client{ + node: node, + lastMessages: make(map[Chat]state.MessageID), + identity: identity, + } + + chat := Chat([32]byte{0x01, 0x2, 0x3, 0x4}) + msgid := state.MessageID([32]byte{0x01, 0x2, 0x3}) + + node.EXPECT().AppendMessage(state.GroupID(chat), gomock.Any()).Return(msgid, nil) + + ret, _ := client.Post(chat, []byte("string")) + if msgid != ret { + t.Error("returned message ID does not match expected") + } +} + func TestClient_Listen_MessageSentToFeed(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()