From 37cd34e4bfe471d22ccbaf46d34fb10e3cecb41d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 30 May 2019 20:25:04 +0300 Subject: [PATCH] Make contact wait delay configurable and fix nil pointer usage --- config/bridge.go | 2 ++ example-config.yaml | 3 +++ portal.go | 2 +- user.go | 7 ++++--- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index 3dd679f..01c6112 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -37,6 +37,7 @@ type BridgeConfig struct { MaxConnectionAttempts int `yaml:"max_connection_attempts"` ConnectionRetryDelay int `yaml:"connection_retry_delay"` ReportConnectionRetry bool `yaml:"report_connection_retry"` + ContactWaitDelay int `yaml:"contact_wait_delay"` InitialChatSync int `yaml:"initial_chat_sync_count"` InitialHistoryFill int `yaml:"initial_history_fill_count"` @@ -59,6 +60,7 @@ func (bc *BridgeConfig) setDefaults() { bc.MaxConnectionAttempts = 3 bc.ConnectionRetryDelay = -1 bc.ReportConnectionRetry = true + bc.ContactWaitDelay = 1 bc.InitialChatSync = 10 bc.InitialHistoryFill = 20 diff --git a/example-config.yaml b/example-config.yaml index e1b3f97..6f01b15 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -68,6 +68,9 @@ bridge: # Whether or not the bridge should send a notice to the user's management room when it retries connecting. # If false, it will only report when it stops retrying. report_connection_retry: true + # Number of seconds to wait for contacts and chats to be sent at startup before syncing. + # If you have lots of chats, it might take more than a second. + contact_wait_delay: 1 # Number of chats to sync for new users. initial_chat_sync_count: 10 diff --git a/portal.go b/portal.go index 191a544..c692226 100644 --- a/portal.go +++ b/portal.go @@ -410,7 +410,7 @@ func (portal *Portal) ensureUserInvited(user *User) { portal.log.Warnfln("Failed to ensure %s is invited to %s: %v", user.MXID, portal.MXID, err) } customPuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID) - if customPuppet.CustomIntent() != nil { + if customPuppet != nil && customPuppet.CustomIntent() != nil { _ = customPuppet.CustomIntent().EnsureJoined(portal.MXID) } } diff --git a/user.go b/user.go index e3f988c..e564fcb 100644 --- a/user.go +++ b/user.go @@ -296,10 +296,11 @@ func (user *User) PostLogin() { } func (user *User) intPostLogin() { - user.log.Debugln("Waiting a second for contacts to arrive") + dur := time.Duration(user.bridge.Config.Bridge.ContactWaitDelay) * time.Second + user.log.Debugfln("Waiting %s for contacts to arrive", dur) // Hacky way to wait for chats and contacts to arrive automatically - time.Sleep(1 * time.Second) - user.log.Debugln("Waited a second, have", len(user.Conn.Store.Chats), "chats and", len(user.Conn.Store.Contacts), "contacts") + time.Sleep(dur) + user.log.Debugfln("Waited %s, have %d chats and %d contacts", dur, len(user.Conn.Store.Chats), len(user.Conn.Store.Contacts)) go user.syncPuppets() user.syncPortals(false)