Skip to content

Commit

Permalink
Prevent creating portals for non-backfillable conversations
Browse files Browse the repository at this point in the history
Defer initializing portal on backfill until there are messages.
  • Loading branch information
hifi committed Dec 19, 2023
1 parent 687a90b commit 413b12c
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions historysync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/base64"
"fmt"
"strings"
"sync"
"time"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -452,34 +453,37 @@ func (user *User) storeHistorySync(evt *waProto.HistorySync) {
continue
}
totalMessageCount += len(conv.GetMessages())
portal := user.GetPortalByJID(jid)
log := log.With().
Str("chat_jid", portal.Key.JID.String()).
Str("chat_jid", jid.String()).
Int("msg_count", len(conv.GetMessages())).
Logger()

historySyncConversation := user.bridge.DB.HistorySync.NewConversationWithValues(
user.MXID,
conv.GetId(),
&portal.Key,
getConversationTimestamp(conv),
conv.GetMuteEndTime(),
conv.GetArchived(),
conv.GetPinned(),
conv.GetDisappearingMode().GetInitiator(),
conv.GetEndOfHistoryTransferType(),
conv.EphemeralExpiration,
conv.GetMarkedAsUnread(),
conv.GetUnreadCount())
historySyncConversation.Upsert()
initPortal := sync.OnceFunc(func() {
portal := user.GetPortalByJID(jid)
historySyncConversation := user.bridge.DB.HistorySync.NewConversationWithValues(
user.MXID,
conv.GetId(),
&portal.Key,
getConversationTimestamp(conv),
conv.GetMuteEndTime(),
conv.GetArchived(),
conv.GetPinned(),
conv.GetDisappearingMode().GetInitiator(),
conv.GetEndOfHistoryTransferType(),
conv.EphemeralExpiration,
conv.GetMarkedAsUnread(),
conv.GetUnreadCount())
historySyncConversation.Upsert()
})

var minTime, maxTime time.Time
var minTimeIndex, maxTimeIndex int

successfullySaved := 0
unsupportedTypes := 0
for i, rawMsg := range conv.GetMessages() {
// 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 {
log.Warn().Err(err).
Int("msg_index", i).
Expand All @@ -503,6 +507,8 @@ func (user *User) storeHistorySync(evt *waProto.HistorySync) {
continue
}

initPortal()

message, err := user.bridge.DB.HistorySync.NewMessageWithValues(user.MXID, conv.GetId(), msgEvt.Info.ID, rawMsg)
if err != nil {
log.Error().Err(err).
Expand Down

0 comments on commit 413b12c

Please sign in to comment.