From 0b4d63a62b4d08597753da9609ad82a88f3083fe Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 20 Jan 2025 20:09:06 +0200 Subject: [PATCH] login: only sync storage after chats --- pkg/connector/client.go | 27 +++++++++++++++++++++++---- pkg/connector/login.go | 11 +---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 7bf68841..89901eb6 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -216,7 +216,7 @@ func (s *SignalClient) Connect(ctx context.Context) { return } s.updateRemoteProfile(ctx, false) - s.tryConnect(ctx, 0) + s.tryConnect(ctx, 0, true) } func (s *SignalClient) ConnectBackground(ctx context.Context) error { @@ -266,7 +266,24 @@ func (s *SignalClient) Disconnect() { } } -func (s *SignalClient) tryConnect(ctx context.Context, retryCount int) { +func (s *SignalClient) postLoginConnect() { + ctx := s.UserLogin.Log.WithContext(context.Background()) + // TODO it would be more proper to only connect after syncing, + // but currently syncing will fetch group info online, so it has to be connected. + s.tryConnect(ctx, 0, false) + if s.Client.Store.EphemeralBackupKey != nil { + go func() { + s.syncChats(ctx) + if s.Client.Store.MasterKey != nil { + s.Client.SyncStorage(ctx) + } + }() + } else if s.Client.Store.MasterKey != nil { + go s.Client.SyncStorage(ctx) + } +} + +func (s *SignalClient) tryConnect(ctx context.Context, retryCount int, doSync bool) { err := s.Client.RegisterCapabilities(ctx) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to register capabilities") @@ -281,13 +298,15 @@ func (s *SignalClient) tryConnect(ctx context.Context, retryCount int) { retryInSeconds := 2 << retryCount zerolog.Ctx(ctx).Debug().Int("retry_in_seconds", retryInSeconds).Msg("Sleeping and retrying connection") time.Sleep(time.Duration(retryInSeconds) * time.Second) - s.tryConnect(ctx, retryCount+1) + s.tryConnect(ctx, retryCount+1, doSync) } else { s.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: "unknown-websocket-error", Message: err.Error()}) } } else { go s.bridgeStateLoop(ch) - go s.syncChats(ctx) + if doSync { + go s.syncChats(ctx) + } } } diff --git a/pkg/connector/login.go b/pkg/connector/login.go index e6549717..8d30da95 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -21,7 +21,6 @@ import ( "fmt" "github.com/google/uuid" - "github.com/rs/zerolog" "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" @@ -174,15 +173,7 @@ func (qr *QRLogin) processingWait(ctx context.Context) (*bridgev2.LoginStep, err if err != nil { return nil, fmt.Errorf("failed to create user login: %w", err) } - backgroundCtx := ul.Log.WithContext(context.Background()) - signalClient := ul.Client.(*SignalClient).Client - // TODO it would be more proper to only connect after syncing, - // but currently syncing will fetch group info online, so it has to be connected. - ul.Client.Connect(backgroundCtx) - if signalClient.Store.MasterKey != nil { - zerolog.Ctx(ctx).Info().Msg("Received master key in login, syncing storage immediately") - go signalClient.SyncStorage(backgroundCtx) - } + ul.Client.(*SignalClient).postLoginConnect() return &bridgev2.LoginStep{ Type: bridgev2.LoginStepTypeComplete, StepID: LoginStepComplete,