Add option to limit age of chats to create portals for

This commit is contained in:
Tulir Asokan 2021-10-30 21:54:35 +03:00
parent 4f11d41a60
commit e3aed76f7a
4 changed files with 19 additions and 7 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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.

10
user.go
View file

@ -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 {