From 817cd21550d3dbddd045f512a52076c3dd1bab39 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 2 Nov 2021 14:06:21 +0200 Subject: [PATCH] Create all portals before backfilling when handling history syncs --- user.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/user.go b/user.go index 30ad5ca..854dcb6 100644 --- a/user.go +++ b/user.go @@ -356,6 +356,11 @@ func containsSupportedMessages(conv *waProto.Conversation) bool { return false } +type portalToBackfill struct { + portal *Portal + conv *waProto.Conversation +} + func (user *User) handleHistorySync(evt *waProto.HistorySync) { if evt.GetSyncType() != waProto.HistorySync_RECENT && evt.GetSyncType() != waProto.HistorySync_FULL { return @@ -364,6 +369,7 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) { maxAge := user.bridge.Config.Bridge.HistorySync.MaxAge minLastMsgToCreate := time.Now().Add(-time.Duration(maxAge) * time.Second) createRooms := user.bridge.Config.Bridge.HistorySync.CreatePortals + portalsToBackfill := make([]portalToBackfill, 0) for _, conv := range evt.GetConversations() { jid, err := types.ParseJID(conv.GetId()) if err != nil { @@ -404,13 +410,17 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) { } else if !user.bridge.Config.Bridge.HistorySync.Backfill { user.log.Debugln("Backfill is disabled, not bridging history sync payload for", portal.Key.JID) } else { - user.log.Debugln("Bridging history sync payload for", portal.Key.JID) - portal.backfill(user, conv.GetMessages()) - if !conv.GetMarkedAsUnread() && conv.GetUnreadCount() == 0 { - user.markSelfReadFull(portal) - } + portalsToBackfill = append(portalsToBackfill, portalToBackfill{portal, conv}) } } + for _, ptb := range portalsToBackfill { + user.log.Debugln("Bridging history sync payload for", ptb.portal.Key.JID) + ptb.portal.backfill(user, ptb.conv.GetMessages()) + if !ptb.conv.GetMarkedAsUnread() && ptb.conv.GetUnreadCount() == 0 { + user.markSelfReadFull(ptb.portal) + } + } + user.log.Infofln("Finished handling history sync with type %s, chunk order %d, progress %d%%", evt.GetSyncType(), evt.GetChunkOrder(), evt.GetProgress()) } func (user *User) HandleEvent(event interface{}) {