diff --git a/pkg/connector/backfill.go b/pkg/connector/backfill.go index 22e8c68f..dbfeaefd 100644 --- a/pkg/connector/backfill.go +++ b/pkg/connector/backfill.go @@ -218,9 +218,17 @@ func (t *TelegramClient) FetchMessages(ctx context.Context, fetchParams bridgev2 return nil, err } - var markRead bool // TODO implement messages := msgs.GetMessages() + portal, err := t.main.Bridge.GetPortalByKey(ctx, fetchParams.Portal.PortalKey) + if err != nil { + return nil, err + } + + // If the first message is the last read message, mark the chat as read + // during backfill. + markRead := fetchParams.Forward && portal.Metadata.(*PortalMetadata).ReadUpTo == messages[0].GetID() + var cursor networkid.PaginationCursor if len(messages) > 0 { cursor = ids.MakePaginationCursorID(messages[len(messages)-1].GetID()) @@ -248,11 +256,6 @@ func (t *TelegramClient) FetchMessages(ctx context.Context, fetchParams bridgev2 } message := msg.(*tg.Message) - portal, err := t.main.Bridge.GetPortalByKey(ctx, fetchParams.Portal.PortalKey) - if err != nil { - return nil, err - } - sender := t.getEventSender(message) intent := portal.GetIntentFor(ctx, sender, t.userLogin, bridgev2.RemoteEventBackfill) converted, err := t.convertToMatrix(ctx, portal, intent, message) diff --git a/pkg/connector/config.go b/pkg/connector/config.go index 6634fd2b..cecc32cf 100644 --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -146,6 +146,7 @@ type GhostMetadata struct { type PortalMetadata struct { IsSuperGroup bool `json:"is_supergroup,omitempty"` + ReadUpTo int `json:"read_up_to,omitempty"` } type MessageMetadata struct { diff --git a/pkg/connector/sync.go b/pkg/connector/sync.go index af4c39cd..2cd9279a 100644 --- a/pkg/connector/sync.go +++ b/pkg/connector/sync.go @@ -94,6 +94,9 @@ func (t *TelegramClient) handleDialogs(ctx context.Context, dialogs tg.ModifiedM log.Err(err).Msg("Failed to get portal") continue } + if dialog.UnreadCount == 0 && !dialog.UnreadMark { + portal.Metadata.(*PortalMetadata).ReadUpTo = dialog.TopMessage + } // If this is a DM, make sure that the user isn't deleted. if user, ok := dialog.Peer.(*tg.PeerUser); ok {