Fix mark messages seen

If a chat had a last message the persistence call `Chat()` would not
decode `LastMessage` correctly.
This commit fixes the issue.
This commit is contained in:
Andrea Maria Piana 2020-08-17 08:37:18 +02:00
parent ba126e9fe0
commit 2e231e690e
3 changed files with 27 additions and 2 deletions

View File

@ -1 +1 @@
0.56.7 0.56.8

View File

@ -277,6 +277,7 @@ func (db sqlitePersistence) Chat(chatID string) (*Chat, error) {
chat Chat chat Chat
encodedMembers []byte encodedMembers []byte
encodedMembershipUpdates []byte encodedMembershipUpdates []byte
lastMessageBytes []byte
) )
err := db.db.QueryRow(` err := db.db.QueryRow(`
@ -305,7 +306,7 @@ func (db sqlitePersistence) Chat(chatID string) (*Chat, error) {
&chat.DeletedAtClockValue, &chat.DeletedAtClockValue,
&chat.UnviewedMessagesCount, &chat.UnviewedMessagesCount,
&chat.LastClockValue, &chat.LastClockValue,
&chat.LastMessage, &lastMessageBytes,
&encodedMembers, &encodedMembers,
&encodedMembershipUpdates, &encodedMembershipUpdates,
&chat.Muted, &chat.Muted,
@ -328,6 +329,15 @@ func (db sqlitePersistence) Chat(chatID string) (*Chat, error) {
return nil, err return nil, err
} }
// Restore last message
if lastMessageBytes != nil {
message := &Message{}
if err = json.Unmarshal(lastMessageBytes, message); err != nil {
return nil, err
}
chat.LastMessage = message
}
return &chat, nil return &chat, nil
} }

View File

@ -514,3 +514,18 @@ func TestMessagesAudioDurationMsNull(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Len(t, m, 1) require.Len(t, m, 1)
} }
func TestSaveChat(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := sqlitePersistence{db: db}
chat := CreatePublicChat("test-chat", &testTimeSource{})
chat.LastMessage = &Message{}
err = p.SaveChat(chat)
require.NoError(t, err)
retrievedChat, err := p.Chat(chat.ID)
require.NoError(t, err)
require.Equal(t, &chat, retrievedChat)
}