Skip to content

Commit

Permalink
login: only sync storage after chats
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 20, 2025
1 parent 121945a commit 0b4d63a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
27 changes: 23 additions & 4 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
}
}

Expand Down
11 changes: 1 addition & 10 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0b4d63a

Please sign in to comment.