--- a/pkg/connector/backfill.go +++ b/pkg/connector/backfill.go @@ -375,6 +375,8 @@ log.Err(err).Msg("Failed to get recent conversations from database") return } + // Xessenger: a chat quiet for longer than the history sync's days_limit gets its room when a new message arrives. + conversations = xessengerRecentConversations(conversations, wa.Main.Config.HistorySync.FullSyncConfig.DaysLimit, time.Now()) log.Info(). Int("limit", limit). Int("conversation_count", len(conversations)). --- /dev/null +++ b/pkg/connector/xessenger_history_scope.go @@ -0,0 +1,25 @@ +package connector + +import ( + "time" + + "go.mau.fi/mautrix-whatsapp/pkg/connector/wadb" +) + +// xessengerRecentConversations keeps the history sync's chats that had a message in the last +// daysLimit days, the history the phone is asked for (full_sync_config.days_limit). The others get +// no room at login: like a chat past max_initial_conversations, one is made when a new message +// arrives. A chat with no known time is left out too. Zero days keeps every chat, as upstream. +func xessengerRecentConversations(conversations []*wadb.Conversation, daysLimit uint32, now time.Time) []*wadb.Conversation { + if daysLimit == 0 { + return conversations + } + cutoff := now.AddDate(0, 0, -int(daysLimit)) + recent := conversations[:0:0] + for _, conv := range conversations { + if !conv.LastMessageTimestamp.IsZero() && !conv.LastMessageTimestamp.Before(cutoff) { + recent = append(recent, conv) + } + } + return recent +} --- /dev/null +++ b/pkg/connector/xessenger_history_scope_test.go @@ -0,0 +1,38 @@ +package connector + +import ( + "testing" + "time" + + "go.mau.fi/whatsmeow/types" + + "go.mau.fi/mautrix-whatsapp/pkg/connector/wadb" +) + +func TestXessengerHistorySyncMakesRoomsOnlyForRecentChats(t *testing.T) { + now := time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC) + chat := func(user string, last time.Time) *wadb.Conversation { + return &wadb.Conversation{ChatJID: types.NewJID(user, types.DefaultUserServer), LastMessageTimestamp: last} + } + // GetRecent's order: the newest chat first. + all := []*wadb.Conversation{ + chat("1", now.Add(-time.Hour)), + chat("2", now.AddDate(0, 0, -90)), + chat("3", now.AddDate(0, 0, -90).Add(-time.Second)), + chat("4", time.Date(2018, 3, 1, 0, 0, 0, 0, time.UTC)), + chat("5", time.Time{}), + } + var users []string + for _, conv := range xessengerRecentConversations(all, 90, now) { + users = append(users, conv.ChatJID.User) + } + if len(users) != 2 || users[0] != "1" || users[1] != "2" { + t.Fatalf("rooms at login for %v, want the chats of the last 90 days [1 2]", users) + } + if len(all) != 5 || all[2].ChatJID.User != "3" { + t.Fatal("the database's list was changed in place") + } + if kept := xessengerRecentConversations(all, 0, now); len(kept) != len(all) { + t.Fatalf("days_limit 0 kept %d chats, want all %d as upstream", len(kept), len(all)) + } +}