Fix lastMessage when receive an emoji out of order

This commit is contained in:
Andrea Maria Piana 2020-07-30 21:14:47 +02:00
parent b346d08d91
commit 5e4d58a807
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 38 additions and 3 deletions

View File

@ -219,15 +219,19 @@ func (c *Chat) NextClockAndTimestamp(timesource TimeSource) (uint64, uint64) {
func (c *Chat) UpdateFromMessage(message *Message, timesource TimeSource) error {
c.Timestamp = int64(timesource.GetCurrentTime())
if c.LastClockValue <= message.Clock {
higherClock := c.LastClockValue <= message.Clock
// If the clock is higher, or last message is nil, we set the message
if higherClock || c.LastMessage == nil {
jsonMessage, err := json.Marshal(message)
if err != nil {
return err
}
c.LastClockValue = message.Clock
c.LastMessage = jsonMessage
}
// If the clock is higher we set the clock
if higherClock {
c.LastClockValue = message.Clock
}
return nil
}

View File

@ -70,3 +70,34 @@ func (s *ChatTestSuite) TestValidateChat() {
}
}
func (s *ChatTestSuite) TestUpdateFromMessage() {
// Base case, clock is higher
message := &Message{}
chat := &Chat{}
message.Clock = 1
s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
s.Require().NotNil(chat.LastMessage)
s.Require().Equal(uint64(1), chat.LastClockValue)
// Clock is lower and lastMessage is not nil
message = &Message{}
lastMessage := []byte("test")
chat = &Chat{LastClockValue: 2, LastMessage: lastMessage}
message.Clock = 1
s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
s.Require().Equal(lastMessage, chat.LastMessage)
s.Require().Equal(uint64(2), chat.LastClockValue)
// Clock is lower and lastMessage is nil
message = &Message{}
chat = &Chat{LastClockValue: 2}
message.Clock = 1
s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
s.Require().NotNil(chat.LastMessage)
s.Require().Equal(uint64(2), chat.LastClockValue)
}