From e3aed76f7ae3a66011ae9054d90b1f349cb1e8ac Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 30 Oct 2021 21:54:35 +0300 Subject: [PATCH] Add option to limit age of chats to create portals for --- commands.go | 2 +- config/bridge.go | 12 +++++++----- example-config.yaml | 2 ++ user.go | 10 +++++++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index 741d96a..fb97f7b 100644 --- a/commands.go +++ b/commands.go @@ -720,7 +720,7 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) { var portalsToDelete []*Portal if ce.User.Admin { - portals = portalsToDelete + portalsToDelete = portals } else { portalsToDelete = portals[:0] for _, portal := range portals { diff --git a/config/bridge.go b/config/bridge.go index bf1d367..83aa1e5 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -31,8 +31,8 @@ type BridgeConfig struct { UsernameTemplate string `yaml:"username_template"` DisplaynameTemplate string `yaml:"displayname_template"` - DeliveryReceipts bool `yaml:"delivery_receipts"` - PortalMessageBuffer int `yaml:"portal_message_buffer"` + DeliveryReceipts bool `yaml:"delivery_receipts"` + PortalMessageBuffer int `yaml:"portal_message_buffer"` CallNotices struct { Start bool `yaml:"start"` @@ -40,9 +40,10 @@ type BridgeConfig struct { } `yaml:"call_notices"` HistorySync struct { - CreatePortals bool `yaml:"create_portals"` - Backfill bool `yaml:"backfill"` - DoublePuppetBackfill bool `yaml:"double_puppet_backfill"` + CreatePortals bool `yaml:"create_portals"` + MaxAge int64 `yaml:"max_age"` + Backfill bool `yaml:"backfill"` + DoublePuppetBackfill bool `yaml:"double_puppet_backfill"` } `yaml:"history_sync"` UserAvatarSync bool `yaml:"user_avatar_sync"` BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"` @@ -102,6 +103,7 @@ func (bc *BridgeConfig) setDefaults() { bc.CallNotices.End = true bc.HistorySync.CreatePortals = true + bc.HistorySync.MaxAge = 604800 bc.UserAvatarSync = true bc.BridgeMatrixLeave = true diff --git a/example-config.yaml b/example-config.yaml index e2d84f4..24a7719 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -98,6 +98,8 @@ bridge: history_sync: # Whether to create portals from history sync payloads from WhatsApp. create_portals: true + # Maximum age of chats in seconds to create portals for. Set to 0 to create portals for all chats in sync payload. + max_age: 604800 # Whether to enable backfilling history sync payloads from WhatsApp using batch sending # This requires a server with MSC2716 support, which is currently an experimental feature in synapse. # It can be enabled by setting experimental_features -> enable_msc2716 to true in homeserver.yaml. diff --git a/user.go b/user.go index 7778eab..795962a 100644 --- a/user.go +++ b/user.go @@ -347,6 +347,9 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) { return } user.log.Infofln("Handling history sync with type %s, chunk order %d, progress %d%%", evt.GetSyncType(), evt.GetChunkOrder(), evt.GetProgress()) + maxAge := user.bridge.Config.Bridge.HistorySync.MaxAge + minLastMsgToCreate := time.Now().Add(-time.Duration(maxAge) * time.Second) + createRooms := user.bridge.Config.Bridge.HistorySync.CreatePortals for _, conv := range evt.GetConversations() { jid, err := types.ParseJID(conv.GetId()) if err != nil { @@ -365,8 +368,13 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) { _ = user.Client.Store.ChatSettings.PutPinned(jid, true) } + lastMsg := time.Unix(int64(conv.GetConversationTimestamp()), 0) portal := user.GetPortalByJID(jid) - if user.bridge.Config.Bridge.HistorySync.CreatePortals && len(portal.MXID) == 0 { + if createRooms && len(portal.MXID) == 0 { + if maxAge > 0 && !lastMsg.After(minLastMsgToCreate) { + user.log.Debugfln("Not creating portal for %s: last message older than limit (%s)", portal.Key.JID, lastMsg) + continue + } user.log.Debugln("Creating portal for", portal.Key.JID, "as part of history sync handling") err = portal.CreateMatrixRoom(user) if err != nil {