From 8b6d58739941f42eaffb5e71202fb3271e780c30 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Mon, 5 Jul 2021 17:26:30 +0300 Subject: [PATCH] fix(client): improved own username handling --- packages/client/src/client.ts | 17 +++++++++++++---- .../client/src/methods/auth/_initialize.ts | 5 +++-- packages/client/src/methods/auth/log-out.ts | 19 ++++++++----------- .../client/src/methods/auth/sign-in-bot.ts | 2 +- packages/client/src/methods/auth/sign-in.ts | 1 + packages/client/src/methods/updates.ts | 2 ++ packages/client/src/methods/users/get-me.ts | 18 +++++++++++------- .../src/methods/users/get-my-username.ts | 15 +++++++++++++++ .../src/methods/users/update-username.ts | 2 ++ packages/dispatcher/src/filters.ts | 11 +---------- 10 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 packages/client/src/methods/users/get-my-username.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 350baa7e..ff8319fd 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -147,6 +147,7 @@ import { blockUser } from './methods/users/block-user' import { deleteProfilePhotos } from './methods/users/delete-profile-photos' import { getCommonChats } from './methods/users/get-common-chats' import { getMe } from './methods/users/get-me' +import { getMyUsername } from './methods/users/get-my-username' import { getProfilePhotos } from './methods/users/get-profile-photos' import { getUsers } from './methods/users/get-users' import { iterProfilePhotos } from './methods/users/iter-profile-photos' @@ -230,10 +231,9 @@ export interface TelegramClient extends BaseTelegramClient { * When you log out, you can immediately log back in using * the same {@link TelegramClient} instance. * - * @param resetSession (default: `false`) Whether to reset the session * @returns On success, `true` is returned */ - logOut(resetSession?: boolean): Promise + logOut(): Promise /** * Recover your password with a recovery code and log in. * @@ -3005,6 +3005,14 @@ export interface TelegramClient extends BaseTelegramClient { * */ getMe(): Promise + /** + * Get currently authorized user's username. + * + * This method uses locally available information and + * does not call any API methods. + * + */ + getMyUsername(): string | null /** * Get a list of profile pictures of a user * @@ -3177,7 +3185,7 @@ export interface TelegramClient extends BaseTelegramClient { export class TelegramClient extends BaseTelegramClient { protected _userId: number | null protected _isBot: boolean - protected _botUsername: string | null + protected _selfUsername: string | null protected _downloadConnections: Record protected _connectionsForInline: Record protected _parseModes: Record @@ -3197,7 +3205,7 @@ export class TelegramClient extends BaseTelegramClient { super(opts) this._userId = null this._isBot = false - this._botUsername = null + this._selfUsername = null this._downloadConnections = {} this._connectionsForInline = {} this._parseModes = {} @@ -3357,6 +3365,7 @@ export class TelegramClient extends BaseTelegramClient { deleteProfilePhotos = deleteProfilePhotos getCommonChats = getCommonChats getMe = getMe + getMyUsername = getMyUsername getProfilePhotos = getProfilePhotos getUsers = getUsers iterProfilePhotos = iterProfilePhotos diff --git a/packages/client/src/methods/auth/_initialize.ts b/packages/client/src/methods/auth/_initialize.ts index 54aec93b..52b092e8 100644 --- a/packages/client/src/methods/auth/_initialize.ts +++ b/packages/client/src/methods/auth/_initialize.ts @@ -8,12 +8,13 @@ interface AuthState { // (see methods/updates) _userId: number | null _isBot: boolean - _botUsername: string | null + + _selfUsername: string | null } // @initialize function _initializeAuthState(this: TelegramClient) { this._userId = null this._isBot = false - this._botUsername = null + this._selfUsername = null } diff --git a/packages/client/src/methods/auth/log-out.ts b/packages/client/src/methods/auth/log-out.ts index 2464648c..1e14772a 100644 --- a/packages/client/src/methods/auth/log-out.ts +++ b/packages/client/src/methods/auth/log-out.ts @@ -6,24 +6,21 @@ import { TelegramClient } from '../../client' * When you log out, you can immediately log back in using * the same {@link TelegramClient} instance. * - * @param resetSession Whether to reset the session * @returns On success, `true` is returned * @internal */ export async function logOut( - this: TelegramClient, - resetSession = false + this: TelegramClient ): Promise { await this.call({ _: 'auth.logOut' }) - if (resetSession) { - this._userId = null - this._isBot = false - this._pts = this._seq = this._date = undefined as any - this._selfChanged = true - this.storage.reset() - await this._saveStorage() - } + this._userId = null + this._isBot = false + this._pts = this._seq = this._date = undefined as any + this._selfUsername = null + this._selfChanged = true + this.storage.reset() + await this._saveStorage() return true } diff --git a/packages/client/src/methods/auth/sign-in-bot.ts b/packages/client/src/methods/auth/sign-in-bot.ts index 3a55787b..57b03a22 100644 --- a/packages/client/src/methods/auth/sign-in-bot.ts +++ b/packages/client/src/methods/auth/sign-in-bot.ts @@ -35,7 +35,7 @@ export async function signInBot( this._userId = res.user.id this._isBot = true - this._botUsername = res.user.username! + this._selfUsername = res.user.username! this._selfChanged = true await this._fetchUpdatesState() await this._saveStorage() diff --git a/packages/client/src/methods/auth/sign-in.ts b/packages/client/src/methods/auth/sign-in.ts index fcd881f5..e74510ec 100644 --- a/packages/client/src/methods/auth/sign-in.ts +++ b/packages/client/src/methods/auth/sign-in.ts @@ -44,6 +44,7 @@ export async function signIn( this._userId = res.user.id this._isBot = false this._selfChanged = true + this._selfUsername = res.user.username ?? null await this._fetchUpdatesState() await this._saveStorage() diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index 1c70130a..81096ff3 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -755,6 +755,8 @@ export function _handleUpdate( upd.dcOptions } else if (upd._ === 'updateConfig') { this._config = await this.call({ _: 'help.getConfig' }) + } else if (upd._ === 'updateUserName' && upd.userId === this._userId) { + this._selfUsername = upd.username || null } else { if (!noDispatch) { const peers = await _fetchPeersForShort.call( diff --git a/packages/client/src/methods/users/get-me.ts b/packages/client/src/methods/users/get-me.ts index 7ebd3971..297e72dd 100644 --- a/packages/client/src/methods/users/get-me.ts +++ b/packages/client/src/methods/users/get-me.ts @@ -9,13 +9,17 @@ import { assertTypeIs } from '../../utils/type-assertion' */ export function getMe(this: TelegramClient): Promise { return this.call({ - _: 'users.getFullUser', - id: { - _: 'inputUserSelf', - }, - }).then((res) => { - assertTypeIs('getMe (@ users.getFullUser -> user)', res.user, 'user') + _: 'users.getUsers', + id: [ + { + _: 'inputUserSelf', + }, + ], + }).then(([user]) => { + assertTypeIs('getMe (@ users.getUsers)', user, 'user') - return new User(this, res.user) + this._selfUsername = user.username ?? null + + return new User(this, user) }) } diff --git a/packages/client/src/methods/users/get-my-username.ts b/packages/client/src/methods/users/get-my-username.ts new file mode 100644 index 00000000..bd69a945 --- /dev/null +++ b/packages/client/src/methods/users/get-my-username.ts @@ -0,0 +1,15 @@ +import { TelegramClient } from '../../client' +import { User } from '../../types' +import { assertTypeIs } from '../../utils/type-assertion' + +/** + * Get currently authorized user's username. + * + * This method uses locally available information and + * does not call any API methods. + * + * @internal + */ +export function getMyUsername(this: TelegramClient): string | null { + return this._selfUsername +} diff --git a/packages/client/src/methods/users/update-username.ts b/packages/client/src/methods/users/update-username.ts index e347aa89..3440308d 100644 --- a/packages/client/src/methods/users/update-username.ts +++ b/packages/client/src/methods/users/update-username.ts @@ -21,5 +21,7 @@ export async function updateUsername( username, }) + this._selfUsername = username || null + return new User(this, res) } diff --git a/packages/dispatcher/src/filters.ts b/packages/dispatcher/src/filters.ts index 03406669..5a40bad0 100644 --- a/packages/dispatcher/src/filters.ts +++ b/packages/dispatcher/src/filters.ts @@ -859,16 +859,7 @@ export namespace filters { const lastGroup = m[m.length - 1] if (lastGroup && msg.client['_isBot']) { // check bot username - if (!msg.client['_botUsername']) { - // need to fetch it first - - return msg.client.getUsers('self').then((self) => { - msg.client['_botUsername'] = self.username! - return check(msg) - }) - } - - if (lastGroup !== msg.client['_botUsername']) return false + if (lastGroup !== msg.client['_selfUsername']) return false } const match = m.slice(1, -1)