--- a/pkg/connector/chatsync.go +++ b/pkg/connector/chatsync.go @@ -327,6 +327,10 @@ if err != nil { return err } + if xessengerSkipOldDialog(portal, dialog, messages, tc.main.Config.Sync.MaxAgeDays) { + log.Debug().Msg("Not creating portal because the last message is older than create_max_age_days") + return nil + } if dialog.UnreadCount == 0 && !dialog.UnreadMark { portal.Metadata.(*PortalMetadata).ReadUpTo = dialog.TopMessage } --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -80,6 +80,7 @@ Sync struct { UpdateLimit int `yaml:"update_limit"` CreateLimit int `yaml:"create_limit"` + MaxAgeDays int `yaml:"create_max_age_days"` LoginLimit int `yaml:"login_sync_limit"` DirectChats bool `yaml:"direct_chats"` } `yaml:"sync"` @@ -178,6 +179,7 @@ helper.Copy(up.Str|up.Null, "proxy", "password") helper.Copy(up.Int, "sync", "update_limit") helper.Copy(up.Int, "sync", "create_limit") + helper.Copy(up.Int, "sync", "create_max_age_days") helper.Copy(up.Int, "sync", "login_sync_limit") helper.Copy(up.Bool, "sync", "direct_chats") helper.Copy(up.Bool, "takeout", "dialog_sync") --- a/pkg/connector/example-config.yaml +++ b/pkg/connector/example-config.yaml @@ -71,6 +71,9 @@ # Number of most recently active dialogs to create portals for when syncing chats. # Set to -1 to remove limit. create_limit: 15 + # Don't create portals for dialogs whose last message is older than this many days when syncing chats. + # They get one when a new message arrives. Set to 0 to remove limit. + create_max_age_days: 0 # Number of chats to sync immediately on login before the data export is accepted. # The create_limit above still applies. This is ignored if takeout.dialog_sync is false. login_sync_limit: 15 --- /dev/null +++ b/pkg/connector/xessenger_sync_scope.go @@ -0,0 +1,31 @@ +package connector + +import ( + "time" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/networkid" + + "go.mau.fi/mautrix-telegram/pkg/connector/ids" + "go.mau.fi/mautrix-telegram/pkg/gotd/tg" +) + +// xessengerSkipOldDialog tells a dialog sync to leave out a chat that has no room yet and whose last +// message is older than days (sync.create_max_age_days). Like a chat past create_limit, it gets a +// room when a new message arrives. A top message missing from the response, or without a date, +// counts as old. A chat that has a room is synced as upstream does, and zero days keeps every chat. +func xessengerSkipOldDialog(portal *bridgev2.Portal, dialog *tg.Dialog, messages map[networkid.MessageID]tg.MessageClass, days int) bool { + return xessengerSkipOldDialogAt(portal, dialog, messages, days, time.Now()) +} + +func xessengerSkipOldDialogAt(portal *bridgev2.Portal, dialog *tg.Dialog, messages map[networkid.MessageID]tg.MessageClass, days int, now time.Time) bool { + if days <= 0 || portal.MXID != "" { + return false + } + top := messages[ids.MakeMessageID(dialog.Peer, dialog.TopMessage)] + if top == nil { + return true + } + message, ok := top.AsNotEmpty() + return !ok || time.Unix(int64(message.GetDate()), 0).Before(now.AddDate(0, 0, -days)) +} --- /dev/null +++ b/pkg/connector/xessenger_sync_scope_test.go @@ -0,0 +1,73 @@ +package connector + +import ( + "testing" + "time" + + up "go.mau.fi/util/configupgrade" + "gopkg.in/yaml.v3" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + + "go.mau.fi/mautrix-telegram/pkg/connector/ids" + "go.mau.fi/mautrix-telegram/pkg/gotd/tg" +) + +func TestXessengerDialogSyncMakesRoomsOnlyForRecentChats(t *testing.T) { + now := time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC) + ago := func(d time.Duration) int { return int(now.Add(-d).Unix()) } + day := 24 * time.Hour + user := &tg.Dialog{Peer: &tg.PeerUser{UserID: 2}, TopMessage: 10} + group := &tg.Dialog{Peer: &tg.PeerChannel{ChannelID: 3}, TopMessage: 10} + unbridged := &bridgev2.Portal{Portal: &database.Portal{}} + bridged := &bridgev2.Portal{Portal: &database.Portal{MXID: "!room:test"}} + for _, c := range []struct { + name string + portal *bridgev2.Portal + dialog *tg.Dialog + top tg.MessageClass + days int + skip bool + }{ + {"message an hour ago", unbridged, user, &tg.Message{ID: 10, Date: ago(time.Hour)}, 90, false}, + {"message exactly 90 days ago", unbridged, user, &tg.Message{ID: 10, Date: ago(90 * day)}, 90, false}, + {"message a second before that", unbridged, user, &tg.Message{ID: 10, Date: ago(90*day + time.Second)}, 90, true}, + {"service message an hour ago", unbridged, user, &tg.MessageService{ID: 10, Date: ago(time.Hour)}, 90, false}, + {"top message without a date", unbridged, user, &tg.MessageEmpty{ID: 10}, 90, true}, + {"top message missing from the response", unbridged, user, nil, 90, true}, + {"supergroup message an hour ago, keyed by its channel", unbridged, group, &tg.Message{ID: 10, Date: ago(time.Hour)}, 90, false}, + {"supergroup quiet for a year", unbridged, group, &tg.Message{ID: 10, Date: ago(365 * day)}, 90, true}, + {"chat quiet for a year that already has a room", bridged, group, &tg.Message{ID: 10, Date: ago(365 * day)}, 90, false}, + {"no limit", unbridged, user, &tg.Message{ID: 10, Date: ago(365 * day)}, 0, false}, + } { + messages := map[networkid.MessageID]tg.MessageClass{} + if c.top != nil { + messages[ids.MakeMessageID(c.dialog.Peer, c.dialog.TopMessage)] = c.top + } + // The same message ID in another chat is not this chat's top message. + messages[ids.MakeMessageID(&tg.PeerChannel{ChannelID: 9}, 10)] = &tg.Message{ID: 10, Date: ago(time.Minute)} + if got := xessengerSkipOldDialogAt(c.portal, c.dialog, messages, c.days, now); got != c.skip { + t.Errorf("%s: skip = %v, want %v", c.name, got, c.skip) + } + } +} + +func TestXessengerConfigKeepsCreateMaxAgeDays(t *testing.T) { + _, _, upgrader := (&TelegramConnector{}).GetConfig() + // The bridge upgrades its config on every start, dropping any key the example config lacks. + upgraded, err := up.DoBytes([]byte("sync:\n create_max_age_days: 90\n"), upgrader.(up.BaseUpgrader)) + if err != nil { + t.Fatal(err) + } + var config, defaults TelegramConfig + if err = yaml.Unmarshal(upgraded, &config); err != nil { + t.Fatal(err) + } + if config.Sync.MaxAgeDays != 90 { + t.Fatalf("create_max_age_days after the config upgrade = %d, want 90", config.Sync.MaxAgeDays) + } + if err = yaml.Unmarshal([]byte(ExampleConfig), &defaults); err != nil || defaults.Sync.MaxAgeDays != 0 { + t.Fatalf("example create_max_age_days = %d (%v), want 0 for upstream's behaviour", defaults.Sync.MaxAgeDays, err) + } +}