From c850f6f3739a1420cc03ed69439d30d903aff586 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 19 Jun 2023 21:12:36 +0300 Subject: [PATCH] Fix things in legacy backfill --- backfillqueue.go | 2 +- database/historysync.go | 13 ++++++++++++- historysync.go | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/backfillqueue.go b/backfillqueue.go index 51e50c2..ab79644 100644 --- a/backfillqueue.go +++ b/backfillqueue.go @@ -65,7 +65,7 @@ func (user *User) HandleBackfillRequestsLoop(backfillTypes []database.BackfillTy req := user.BackfillQueue.GetNextBackfill(user.MXID, backfillTypes, waitForBackfillTypes, reCheckChannel) user.log.Infofln("Handling backfill request %s", req) - conv := user.bridge.DB.HistorySync.GetConversation(user.MXID, req.Portal) + conv := user.bridge.DB.HistorySync.GetConversation(user.MXID, *req.Portal) if conv == nil { user.log.Debugfln("Could not find history sync conversation data for %s", req.Portal.String()) req.MarkDone() diff --git a/database/historysync.go b/database/historysync.go index 214440b..4d9d0bf 100644 --- a/database/historysync.go +++ b/database/historysync.go @@ -183,7 +183,7 @@ func (hsq *HistorySyncQuery) GetNMostRecentConversations(userID id.UserID, n int return } -func (hsq *HistorySyncQuery) GetConversation(userID id.UserID, portalKey *PortalKey) (conversation *HistorySyncConversation) { +func (hsq *HistorySyncQuery) GetConversation(userID id.UserID, portalKey PortalKey) (conversation *HistorySyncConversation) { rows, err := hsq.db.Query(getConversationByPortal, userID, portalKey.JID, portalKey.Receiver) defer rows.Close() if err != nil || rows == nil { @@ -323,3 +323,14 @@ func (hsq *HistorySyncQuery) DeleteAllMessagesForPortal(userID id.UserID, portal hsq.log.Warnfln("Failed to delete historical messages for %s/%s: %v", userID, portalKey.JID, err) } } + +func (hsq *HistorySyncQuery) DeleteConversation(userID id.UserID, jid string) { + // This will also clear history_sync_message as there's a foreign key constraint + _, err := hsq.db.Exec(` + DELETE FROM history_sync_conversation + WHERE user_mxid=$1 AND conversation_id=$2 + `, userID, jid) + if err != nil { + hsq.log.Warnfln("Failed to delete historical messages for %s/%s: %v", userID, jid, err) + } +} diff --git a/historysync.go b/historysync.go index ec9369c..e09d1b4 100644 --- a/historysync.go +++ b/historysync.go @@ -151,7 +151,7 @@ func (user *User) backfillAll() { user.zlog.Debug(). Str("portal_jid", portal.Key.JID.String()). Msg("Chat already has a room, deleting messages from database") - user.bridge.DB.HistorySync.DeleteAllMessagesForPortal(user.MXID, portal.Key) + user.bridge.DB.HistorySync.DeleteConversation(user.MXID, portal.Key.JID.String()) } else if i < user.bridge.Config.Bridge.HistorySync.MaxInitialConversations { err = portal.CreateMatrixRoom(user, nil, true, true) if err != nil { @@ -173,6 +173,7 @@ func (portal *Portal) legacyBackfill(user *User) { Str("portal_jid", portal.Key.JID.String()). Str("action", "legacy backfill"). Logger() + conv := user.bridge.DB.HistorySync.GetConversation(user.MXID, portal.Key) messages := user.bridge.DB.HistorySync.GetMessagesBetween(user.MXID, portal.Key.JID.String(), nil, nil, portal.bridge.Config.Bridge.HistorySync.MessageCount) log.Debug().Int("message_count", len(messages)).Msg("Got messages to backfill from database") for i := len(messages) - 1; i >= 0; i-- { @@ -187,8 +188,16 @@ func (portal *Portal) legacyBackfill(user *User) { } portal.handleMessage(user, msgEvt) } + if conv != nil { + isUnread := conv.MarkedAsUnread || conv.UnreadCount > 0 + isTooOld := user.bridge.Config.Bridge.HistorySync.UnreadHoursThreshold > 0 && conv.LastMessageTimestamp.Before(time.Now().Add(time.Duration(-user.bridge.Config.Bridge.HistorySync.UnreadHoursThreshold)*time.Hour)) + shouldMarkAsRead := !isUnread || isTooOld + if shouldMarkAsRead { + user.markSelfReadFull(portal) + } + } log.Debug().Msg("Backfill complete, deleting leftover messages from database") - user.bridge.DB.HistorySync.DeleteAllMessagesForPortal(user.MXID, portal.Key) + user.bridge.DB.HistorySync.DeleteConversation(user.MXID, portal.Key.JID.String()) } func (user *User) dailyMediaRequestLoop() {