2021-04-08 12:19:38 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import { User } from '../../types'
|
|
|
|
import { assertTypeIs } from '../../utils/type-assertion'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get currently authorized user's full information
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function getMe(this: TelegramClient): Promise<User> {
|
|
|
|
return this.call({
|
2021-07-05 17:26:30 +03:00
|
|
|
_: 'users.getUsers',
|
|
|
|
id: [
|
|
|
|
{
|
|
|
|
_: 'inputUserSelf',
|
|
|
|
},
|
|
|
|
],
|
2021-11-23 00:03:59 +03:00
|
|
|
}).then(async ([user]) => {
|
2021-07-05 17:26:30 +03:00
|
|
|
assertTypeIs('getMe (@ users.getUsers)', user, 'user')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (this._userId !== user.id) {
|
|
|
|
// there is such possibility, e.g. when
|
|
|
|
// using a string session without `self`,
|
|
|
|
// or logging out and re-logging in
|
|
|
|
// we need to update the fields accordingly,
|
|
|
|
// and force-save the session
|
|
|
|
this._userId = user.id
|
2023-06-05 03:30:48 +03:00
|
|
|
this._isBot = Boolean(user.bot)
|
2021-11-23 00:03:59 +03:00
|
|
|
await this._saveStorage()
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:26:30 +03:00
|
|
|
this._selfUsername = user.username ?? null
|
|
|
|
|
|
|
|
return new User(this, user)
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
}
|