From b4a26c6f5b77610881bde6b8768156b4960800dd Mon Sep 17 00:00:00 2001 From: teidesu Date: Mon, 26 Apr 2021 22:41:19 +0300 Subject: [PATCH] fix(client): disable catch up by default, also save storage after catching up --- packages/client/src/methods/auth/start.ts | 33 ++++++++++++++--------- packages/client/src/methods/updates.ts | 1 + 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/client/src/methods/auth/start.ts b/packages/client/src/methods/auth/start.ts index e22f0cb0..ade47db7 100644 --- a/packages/client/src/methods/auth/start.ts +++ b/packages/client/src/methods/auth/start.ts @@ -111,29 +111,40 @@ export async function start( /** * Whether to "catch up" (load missed updates). - * Note: you should register your handlers - * before calling `start()` + * Only applicable if the saved session already + * contained authorization and updates state. * - * Defaults to true. + * Note: you should register your handlers + * before calling `start()`, otherwise they will + * not be called. + * + * Note: In case the storage was not properly + * closed the last time, "catching up" might + * result in duplicate updates. + * + * Defaults to `false`. */ catchUp?: boolean } ): Promise { - if (!params.phone && !params.botToken) - throw new MtCuteArgumentError( - 'Neither phone nor bot token were provided' - ) - try { const me = await this.getMe() + // user is already authorized - if (params.catchUp !== false && !this._disableUpdates) + + if (!this._disableUpdates && params.catchUp) await this.catchUp() + return me } catch (e) { if (!(e instanceof AuthKeyUnregisteredError)) throw e } + if (!params.phone && !params.botToken) + throw new MtCuteArgumentError( + 'Neither phone nor bot token were provided' + ) + let phone = params.phone ? await resolveMaybeDynamic(params.phone) : null if (phone) { phone = normalizePhoneNumber(phone) @@ -149,9 +160,7 @@ export async function start( 'Either bot token or phone number must be provided' ) - if (params.catchUp !== false) await this.catchUp() - - return this.signInBot(botToken) + return await this.signInBot(botToken) } let sentCode = await this.sendCode(phone) diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index 17ee686e..708aafff 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -767,4 +767,5 @@ export function catchUp(this: TelegramClient): Promise { .then(() => _loadDifference.call(this)) .catch((err) => this._emitError(err)) .then(() => this._updLock.release()) + .then(() => this._saveStorage()) }