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 { deleteProfilePhotos } from './methods/users/delete-profile-photos'
|
||||||
import { getCommonChats } from './methods/users/get-common-chats'
|
import { getCommonChats } from './methods/users/get-common-chats'
|
||||||
import { getMe } from './methods/users/get-me'
|
import { getMe } from './methods/users/get-me'
|
||||||
|
import { getMyUsername } from './methods/users/get-my-username'
|
||||||
import { getProfilePhotos } from './methods/users/get-profile-photos'
|
import { getProfilePhotos } from './methods/users/get-profile-photos'
|
||||||
import { getUsers } from './methods/users/get-users'
|
import { getUsers } from './methods/users/get-users'
|
||||||
import { iterProfilePhotos } from './methods/users/iter-profile-photos'
|
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
|
* When you log out, you can immediately log back in using
|
||||||
* the same {@link TelegramClient} instance.
|
* the same {@link TelegramClient} instance.
|
||||||
*
|
*
|
||||||
* @param resetSession (default: `false`) Whether to reset the session
|
|
||||||
* @returns On success, `true` is returned
|
* @returns On success, `true` is returned
|
||||||
*/
|
*/
|
||||||
logOut(resetSession?: boolean): Promise<true>
|
logOut(): Promise<true>
|
||||||
/**
|
/**
|
||||||
* Recover your password with a recovery code and log in.
|
* Recover your password with a recovery code and log in.
|
||||||
*
|
*
|
||||||
|
@ -3005,6 +3005,14 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
getMe(): Promise<User>
|
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
|
* Get a list of profile pictures of a user
|
||||||
*
|
*
|
||||||
|
@ -3177,7 +3185,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
export class TelegramClient extends BaseTelegramClient {
|
export class TelegramClient extends BaseTelegramClient {
|
||||||
protected _userId: number | null
|
protected _userId: number | null
|
||||||
protected _isBot: boolean
|
protected _isBot: boolean
|
||||||
protected _botUsername: string | null
|
protected _selfUsername: string | null
|
||||||
protected _downloadConnections: Record<number, TelegramConnection>
|
protected _downloadConnections: Record<number, TelegramConnection>
|
||||||
protected _connectionsForInline: Record<number, TelegramConnection>
|
protected _connectionsForInline: Record<number, TelegramConnection>
|
||||||
protected _parseModes: Record<string, IMessageEntityParser>
|
protected _parseModes: Record<string, IMessageEntityParser>
|
||||||
|
@ -3197,7 +3205,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
super(opts)
|
super(opts)
|
||||||
this._userId = null
|
this._userId = null
|
||||||
this._isBot = false
|
this._isBot = false
|
||||||
this._botUsername = null
|
this._selfUsername = null
|
||||||
this._downloadConnections = {}
|
this._downloadConnections = {}
|
||||||
this._connectionsForInline = {}
|
this._connectionsForInline = {}
|
||||||
this._parseModes = {}
|
this._parseModes = {}
|
||||||
|
@ -3357,6 +3365,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
deleteProfilePhotos = deleteProfilePhotos
|
deleteProfilePhotos = deleteProfilePhotos
|
||||||
getCommonChats = getCommonChats
|
getCommonChats = getCommonChats
|
||||||
getMe = getMe
|
getMe = getMe
|
||||||
|
getMyUsername = getMyUsername
|
||||||
getProfilePhotos = getProfilePhotos
|
getProfilePhotos = getProfilePhotos
|
||||||
getUsers = getUsers
|
getUsers = getUsers
|
||||||
iterProfilePhotos = iterProfilePhotos
|
iterProfilePhotos = iterProfilePhotos
|
||||||
|
|
|
@ -8,12 +8,13 @@ interface AuthState {
|
||||||
// (see methods/updates)
|
// (see methods/updates)
|
||||||
_userId: number | null
|
_userId: number | null
|
||||||
_isBot: boolean
|
_isBot: boolean
|
||||||
_botUsername: string | null
|
|
||||||
|
_selfUsername: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// @initialize
|
// @initialize
|
||||||
function _initializeAuthState(this: TelegramClient) {
|
function _initializeAuthState(this: TelegramClient) {
|
||||||
this._userId = null
|
this._userId = null
|
||||||
this._isBot = false
|
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
|
* When you log out, you can immediately log back in using
|
||||||
* the same {@link TelegramClient} instance.
|
* the same {@link TelegramClient} instance.
|
||||||
*
|
*
|
||||||
* @param resetSession Whether to reset the session
|
|
||||||
* @returns On success, `true` is returned
|
* @returns On success, `true` is returned
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export async function logOut(
|
export async function logOut(
|
||||||
this: TelegramClient,
|
this: TelegramClient
|
||||||
resetSession = false
|
|
||||||
): Promise<true> {
|
): Promise<true> {
|
||||||
await this.call({ _: 'auth.logOut' })
|
await this.call({ _: 'auth.logOut' })
|
||||||
|
|
||||||
if (resetSession) {
|
|
||||||
this._userId = null
|
this._userId = null
|
||||||
this._isBot = false
|
this._isBot = false
|
||||||
this._pts = this._seq = this._date = undefined as any
|
this._pts = this._seq = this._date = undefined as any
|
||||||
|
this._selfUsername = null
|
||||||
this._selfChanged = true
|
this._selfChanged = true
|
||||||
this.storage.reset()
|
this.storage.reset()
|
||||||
await this._saveStorage()
|
await this._saveStorage()
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ export async function signInBot(
|
||||||
|
|
||||||
this._userId = res.user.id
|
this._userId = res.user.id
|
||||||
this._isBot = true
|
this._isBot = true
|
||||||
this._botUsername = res.user.username!
|
this._selfUsername = res.user.username!
|
||||||
this._selfChanged = true
|
this._selfChanged = true
|
||||||
await this._fetchUpdatesState()
|
await this._fetchUpdatesState()
|
||||||
await this._saveStorage()
|
await this._saveStorage()
|
||||||
|
|
|
@ -44,6 +44,7 @@ export async function signIn(
|
||||||
this._userId = res.user.id
|
this._userId = res.user.id
|
||||||
this._isBot = false
|
this._isBot = false
|
||||||
this._selfChanged = true
|
this._selfChanged = true
|
||||||
|
this._selfUsername = res.user.username ?? null
|
||||||
await this._fetchUpdatesState()
|
await this._fetchUpdatesState()
|
||||||
await this._saveStorage()
|
await this._saveStorage()
|
||||||
|
|
||||||
|
|
|
@ -755,6 +755,8 @@ export function _handleUpdate(
|
||||||
upd.dcOptions
|
upd.dcOptions
|
||||||
} else if (upd._ === 'updateConfig') {
|
} else if (upd._ === 'updateConfig') {
|
||||||
this._config = await this.call({ _: 'help.getConfig' })
|
this._config = await this.call({ _: 'help.getConfig' })
|
||||||
|
} else if (upd._ === 'updateUserName' && upd.userId === this._userId) {
|
||||||
|
this._selfUsername = upd.username || null
|
||||||
} else {
|
} else {
|
||||||
if (!noDispatch) {
|
if (!noDispatch) {
|
||||||
const peers = await _fetchPeersForShort.call(
|
const peers = await _fetchPeersForShort.call(
|
||||||
|
|
|
@ -9,13 +9,17 @@ import { assertTypeIs } from '../../utils/type-assertion'
|
||||||
*/
|
*/
|
||||||
export function getMe(this: TelegramClient): Promise<User> {
|
export function getMe(this: TelegramClient): Promise<User> {
|
||||||
return this.call({
|
return this.call({
|
||||||
_: 'users.getFullUser',
|
_: 'users.getUsers',
|
||||||
id: {
|
id: [
|
||||||
|
{
|
||||||
_: 'inputUserSelf',
|
_: 'inputUserSelf',
|
||||||
},
|
},
|
||||||
}).then((res) => {
|
],
|
||||||
assertTypeIs('getMe (@ users.getFullUser -> user)', res.user, 'user')
|
}).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,
|
username,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this._selfUsername = username || null
|
||||||
|
|
||||||
return new User(this, res)
|
return new User(this, res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -859,16 +859,7 @@ export namespace filters {
|
||||||
const lastGroup = m[m.length - 1]
|
const lastGroup = m[m.length - 1]
|
||||||
if (lastGroup && msg.client['_isBot']) {
|
if (lastGroup && msg.client['_isBot']) {
|
||||||
// check bot username
|
// check bot username
|
||||||
if (!msg.client['_botUsername']) {
|
if (lastGroup !== msg.client['_selfUsername']) return false
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const match = m.slice(1, -1)
|
const match = m.slice(1, -1)
|
||||||
|
|
Loading…
Reference in a new issue