diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 8b4e3f27..797e3b42 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -111,6 +111,7 @@ type TelegramClient struct { stopTakeoutTimer *time.Timer takeoutDialogsOnce sync.Once syncChatsLock sync.Mutex + isNewLogin bool prevReactionPoll map[networkid.PortalKey]time.Time prevReactionPollLock sync.Mutex @@ -511,6 +512,15 @@ func (t *TelegramClient) runInBackground(ctx context.Context) { log := zerolog.Ctx(ctx) err := t.client.Run(ctx, func(ctx context.Context) error { t.clientInitialized.Set() + // If takeout dialog sync is enabled, we assume it'll resume from a getTakeoutID call. + // If not, resume dialog sync manually here. + if !t.isNewLogin && !t.main.Config.Takeout.DialogSync { + go func() { + if err := t.syncChats(log.WithContext(t.clientCtx), 0, false, false); err != nil { + log.Err(err).Msg("Failed to resume chat sync") + } + }() + } log.Info().Msg("Client running, starting updates") err := t.updatesManager.Run(ctx, t.client.API(), t.telegramUserID, updates.AuthOptions{ IsBot: t.metadata.IsBot, diff --git a/pkg/connector/login.go b/pkg/connector/login.go index 4bee7e28..68b27a1e 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -229,8 +229,9 @@ func (bl *baseLogin) finalizeLogin( if err != nil { return nil, fmt.Errorf("failed to save new login: %w", err) } - ul.Client.Connect(ul.Log.WithContext(bl.main.Bridge.BackgroundCtx)) client := ul.Client.(*TelegramClient) + client.isNewLogin = true + client.Connect(ul.Log.WithContext(bl.main.Bridge.BackgroundCtx)) bgCtx := ul.Log.WithContext(bl.main.Bridge.BackgroundCtx) go func() { @@ -241,6 +242,8 @@ func (bl *baseLogin) finalizeLogin( err := client.clientInitialized.Wait(bgCtx) if err != nil { log.Err(err).Msg("Failed to wait for client init to sync chats after login") + } else if client.clientDone.IsSet() { + log.Warn().Msg("Client is already done after login, skipping chat sync") } else if err = client.syncChats(log.WithContext(client.clientCtx), 0, true, false); err != nil { log.Err(err).Msg("Failed to sync chats") } @@ -259,6 +262,8 @@ func (bl *baseLogin) finalizeLogin( err := client.clientInitialized.Wait(bgCtx) if err != nil { log.Err(err).Msg("Failed to wait for client init to start takeout") + } else if client.clientDone.IsSet() { + log.Warn().Msg("Client is already done after login, skipping takeout") } else if _, err = client.getTakeoutID(bgCtx); err != nil { log.Err(err).Msg("Failed to get takeout") } else if client.stopTakeoutTimer == nil {