2023-10-09 05:59:48 +03:00
|
|
|
import { BaseTelegramClient } from '@mtcute/core'
|
2023-10-16 19:23:53 +03:00
|
|
|
import { assertTypeIs } from '@mtcute/core/utils.js'
|
2023-09-22 15:32:28 +03:00
|
|
|
|
2023-10-16 19:23:53 +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
|
|
|
|
*/
|
2023-10-09 05:59:48 +03:00
|
|
|
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
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
const authState = getAuthState(client)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
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()
|
|
|
|
}
|
2021-07-05 17:26:30 +03:00
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
authState.selfUsername = user.username ?? null
|
|
|
|
|
|
|
|
return new User(user)
|
|
|
|
})
|
2021-04-08 12:19:38 +03:00
|
|
|
}
|