fix(client): improved own username handling
This commit is contained in:
parent
be506f5ed7
commit
8b6d587399
10 changed files with 57 additions and 35 deletions
|
@ -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<true>
|
||||
logOut(): Promise<true>
|
||||
/**
|
||||
* Recover your password with a recovery code and log in.
|
||||
*
|
||||
|
@ -3005,6 +3005,14 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
*
|
||||
*/
|
||||
getMe(): Promise<User>
|
||||
/**
|
||||
* 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<number, TelegramConnection>
|
||||
protected _connectionsForInline: Record<number, TelegramConnection>
|
||||
protected _parseModes: Record<string, IMessageEntityParser>
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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<true> {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -9,13 +9,17 @@ import { assertTypeIs } from '../../utils/type-assertion'
|
|||
*/
|
||||
export function getMe(this: TelegramClient): Promise<User> {
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
|
15
packages/client/src/methods/users/get-my-username.ts
Normal file
15
packages/client/src/methods/users/get-my-username.ts
Normal file
|
@ -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
|
||||
}
|
|
@ -21,5 +21,7 @@ export async function updateUsername(
|
|||
username,
|
||||
})
|
||||
|
||||
this._selfUsername = username || null
|
||||
|
||||
return new User(this, res)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue