mtcute/packages/client/src/methods/users/get-me.ts

42 lines
1.3 KiB
TypeScript
Raw Normal View History

import { BaseTelegramClient } from '@mtcute/core'
import { assertTypeIs } from '@mtcute/core/utils.js'
2023-09-22 15:32:28 +03:00
import { User } from '../../types/index.js'
import { getAuthState } from '../auth/_state.js'
2021-04-08 12:19:38 +03:00
/**
* Get currently authorized user's full information
*/
export function getMe(client: BaseTelegramClient): Promise<User> {
return client
.call({
_: 'users.getUsers',
id: [
{
_: 'inputUserSelf',
},
],
})
.then(async ([user]) => {
assertTypeIs('getMe (@ users.getUsers)', user, 'user')
2021-04-08 12:19:38 +03:00
const authState = getAuthState(client)
if (authState.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
authState.userId = user.id
authState.isBot = Boolean(user.bot)
authState.selfChanged = true
await client.saveStorage()
}
authState.selfUsername = user.username ?? null
return new User(user)
})
2021-04-08 12:19:38 +03:00
}