From 1c77f2ed6eb2fe730d95b2c9f033ba180b9135ee Mon Sep 17 00:00:00 2001 From: flexsurfer Date: Tue, 21 Jun 2022 18:31:15 +0200 Subject: [PATCH] fix offline messages marked as seen (#2713) --- protocol/message_persistence.go | 19 +++++++++++++++++++ protocol/messenger.go | 16 ++++++++++++---- protocol/messenger_handler.go | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 57735a9f5..cac6c2fdd 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -604,6 +604,25 @@ func (db sqlitePersistence) MessageByChatID(chatID string, currCursor string, li return result, newCursor, nil } +func (db sqlitePersistence) latestIncomingMessageClock(chatID string) (uint64, error) { + var clock uint64 + err := db.db.QueryRow( + ` + SELECT + clock_value + FROM + user_messages m1 + WHERE + m1.local_chat_id = ? AND m1.outgoing_status = '' + ORDER BY substr('0000000000000000000000000000000000000000000000000000000000000000' || m1.clock_value, -64, 64) || m1.id DESC + LIMIT 1 + `, chatID).Scan(&clock) + if err != nil { + return 0, err + } + return clock, nil +} + func (db sqlitePersistence) PendingContactRequests(currCursor string, limit int) ([]*common.Message, string, error) { cursorWhere := "" if currCursor != "" { diff --git a/protocol/messenger.go b/protocol/messenger.go index 991512383..461aaab77 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -4218,6 +4218,10 @@ func (m *Messenger) MessagesExist(ids []string) (map[string]bool, error) { return m.persistence.MessagesExist(ids) } +func (m *Messenger) latestIncomingMessageClock(chatID string) (uint64, error) { + return m.persistence.latestIncomingMessageClock(chatID) +} + func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*common.Message, string, error) { chat, err := m.persistence.Chat(chatID) if err != nil { @@ -4395,11 +4399,15 @@ func (m *Messenger) MarkAllRead(chatID string) error { return err } - chat, ok := m.allChats.Load(chatID) - if !ok { - return errors.New("chat not found") + clock, _ := m.latestIncomingMessageClock(chatID) + + if clock == 0 { + chat, ok := m.allChats.Load(chatID) + if !ok { + return errors.New("chat not found") + } + clock, _ = chat.NextClockAndTimestamp(m.getTimesource()) } - clock, _ := chat.NextClockAndTimestamp(m.getTimesource()) return m.markAllRead(chatID, clock, true) } diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index ee1c845db..c00b779af 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -1137,7 +1137,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error { return err // matchChatEntity returns a descriptive error message } - if chat.ReadMessagesAtClockValue > state.CurrentMessageState.WhisperTimestamp { + if chat.ReadMessagesAtClockValue >= receivedMessage.Clock { receivedMessage.Seen = true }