Prevent creating portals for non-backfillable conversations

Defer initializing portal on backfill until there are messages.
This commit is contained in:
Toni Spets 2023-12-19 13:28:34 +02:00 committed by Toni Spets
parent 687a90b7b2
commit a1f1c91be1

View file

@ -21,6 +21,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strings" "strings"
"sync"
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -452,26 +453,29 @@ func (user *User) storeHistorySync(evt *waProto.HistorySync) {
continue continue
} }
totalMessageCount += len(conv.GetMessages()) totalMessageCount += len(conv.GetMessages())
portal := user.GetPortalByJID(jid)
log := log.With(). log := log.With().
Str("chat_jid", portal.Key.JID.String()). Str("chat_jid", jid.String()).
Int("msg_count", len(conv.GetMessages())). Int("msg_count", len(conv.GetMessages())).
Logger() Logger()
historySyncConversation := user.bridge.DB.HistorySync.NewConversationWithValues( initPortal := sync.OnceFunc(func() {
user.MXID, portal := user.GetPortalByJID(jid)
conv.GetId(), historySyncConversation := user.bridge.DB.HistorySync.NewConversationWithValues(
&portal.Key, user.MXID,
getConversationTimestamp(conv), conv.GetId(),
conv.GetMuteEndTime(), &portal.Key,
conv.GetArchived(), getConversationTimestamp(conv),
conv.GetPinned(), conv.GetMuteEndTime(),
conv.GetDisappearingMode().GetInitiator(), conv.GetArchived(),
conv.GetEndOfHistoryTransferType(), conv.GetPinned(),
conv.EphemeralExpiration, conv.GetDisappearingMode().GetInitiator(),
conv.GetMarkedAsUnread(), conv.GetEndOfHistoryTransferType(),
conv.GetUnreadCount()) conv.EphemeralExpiration,
historySyncConversation.Upsert() conv.GetMarkedAsUnread(),
conv.GetUnreadCount())
historySyncConversation.Upsert()
})
var minTime, maxTime time.Time var minTime, maxTime time.Time
var minTimeIndex, maxTimeIndex int var minTimeIndex, maxTimeIndex int
@ -479,7 +483,7 @@ func (user *User) storeHistorySync(evt *waProto.HistorySync) {
unsupportedTypes := 0 unsupportedTypes := 0
for i, rawMsg := range conv.GetMessages() { for i, rawMsg := range conv.GetMessages() {
// Don't store messages that will just be skipped. // Don't store messages that will just be skipped.
msgEvt, err := user.Client.ParseWebMessage(portal.Key.JID, rawMsg.GetMessage()) msgEvt, err := user.Client.ParseWebMessage(jid, rawMsg.GetMessage())
if err != nil { if err != nil {
log.Warn().Err(err). log.Warn().Err(err).
Int("msg_index", i). Int("msg_index", i).
@ -503,6 +507,8 @@ func (user *User) storeHistorySync(evt *waProto.HistorySync) {
continue continue
} }
initPortal()
message, err := user.bridge.DB.HistorySync.NewMessageWithValues(user.MXID, conv.GetId(), msgEvt.Info.ID, rawMsg) message, err := user.bridge.DB.HistorySync.NewMessageWithValues(user.MXID, conv.GetId(), msgEvt.Info.ID, rawMsg)
if err != nil { if err != nil {
log.Error().Err(err). log.Error().Err(err).