login: only sync storage after chats

This commit is contained in:
Tulir Asokan 2025-01-20 20:09:06 +02:00
parent 121945a445
commit 0b4d63a62b
2 changed files with 24 additions and 14 deletions

View file

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

View file

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