From 7b7cdb30fbc5a672b9554ef8138b4c7f3537b204 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 14 Aug 2024 17:46:36 +0300 Subject: [PATCH] backfill: ignore read up to ts if unread flag is false --- pkg/connector/backfill.go | 2 +- pkg/connector/chatsync.go | 8 ++------ pkg/connector/client.go | 1 + 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/connector/backfill.go b/pkg/connector/backfill.go index 7f2f419..6fd32f1 100644 --- a/pkg/connector/backfill.go +++ b/pkg/connector/backfill.go @@ -127,7 +127,7 @@ func (gc *GMClient) FetchMessages(ctx context.Context, params bridgev2.FetchMess if meta != nil { lastWrappedMsg := fetchResp.Messages[len(fetchResp.Messages)-1] lastRawMsg := resp.Messages[len(resp.Messages)-1] - fetchResp.MarkRead = !meta.readUpToTS.Before(lastWrappedMsg.Timestamp) || meta.readUpTo == lastRawMsg.MessageID + fetchResp.MarkRead = !meta.unread || !meta.readUpToTS.Before(lastWrappedMsg.Timestamp) || meta.readUpTo == lastRawMsg.MessageID } gc.conversationMetaLock.Unlock() } else { diff --git a/pkg/connector/chatsync.go b/pkg/connector/chatsync.go index 413dd5d..d052973 100644 --- a/pkg/connector/chatsync.go +++ b/pkg/connector/chatsync.go @@ -71,14 +71,10 @@ func (gc *GMClient) syncConversationMeta(v *gmproto.Conversation) (meta *convers meta = &conversationMeta{} gc.conversationMeta[v.ConversationID] = meta } + meta.unread = v.Unread if !v.Unread { meta.readUpTo = v.LatestMessageID - newLastMsgTS := time.UnixMicro(v.LastMessageTimestamp) - // Don't allow read up to timestamp to move backwards. - // There are some weird race conditions where it causes backfills to be left unread. - if meta.readUpToTS.Before(newLastMsgTS) { - meta.readUpToTS = newLastMsgTS - } + meta.readUpToTS = time.UnixMicro(v.LastMessageTimestamp) } else if meta.readUpTo == v.LatestMessageID { meta.readUpTo = "" meta.readUpToTS = time.Time{} diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 4e778fb..e21a675 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -36,6 +36,7 @@ import ( type conversationMeta struct { markedSpamAt time.Time cancelPendingBackfill atomic.Pointer[context.CancelFunc] + unread bool readUpTo string readUpToTS time.Time }