mtcute/packages/client/src/methods/users/resolve-peer.ts
teidesu ec736f8590 some changes
i've been wanting to name a commit like this for my entire life, lol. seriously though, a lot has changed:
 - extracted TL-related stuff to `@mtcute/tl-utils` and `@mtcute/tl-runtime`, rewrote codegen in TS
 - updated to layer 134, moved to int64 identifiers
 - rewritten networking (mtproto), rewritten updates handling
 - *lots* of refactoring

 still a very early version though, there are a lot of improvements to be made, but at least it runs, lol

 also tl-reference will not be updated anytime soon because i want to rewrite it
2021-11-23 00:03:59 +03:00

195 lines
6.2 KiB
TypeScript

import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
import { InputPeerLike, MtNotFoundError } from '../../types'
import { getBasicPeerType, getMarkedPeerId, toggleChannelIdMark } from '@mtcute/core'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import Long from 'long'
import { assertTypeIs } from '../../utils/type-assertion'
/**
* Get the `InputPeer` of a known peer id.
* Useful when an `InputPeer` is needed.
*
* @param peerId The peer identifier that you want to extract the `InputPeer` from.
* @param force Whether to force re-fetch the peer from the server
* @internal
*/
export async function resolvePeer(
this: TelegramClient,
peerId: InputPeerLike,
force = false
): Promise<tl.TypeInputPeer> {
// for convenience we also accept tl objects directly
if (typeof peerId === 'object') {
if (tl.isAnyPeer(peerId)) {
peerId = getMarkedPeerId(peerId)
} else {
return normalizeToInputPeer(peerId)
}
}
if (typeof peerId === 'number' && !force) {
const fromStorage = await this.storage.getPeerById(peerId)
if (fromStorage) return fromStorage
}
if (typeof peerId === 'string') {
if (peerId === 'self' || peerId === 'me') return { _: 'inputPeerSelf' }
peerId = peerId.replace(/[@+\s()]/g, '')
if (peerId.match(/^\d+$/)) {
// phone number
const fromStorage = await this.storage.getPeerByPhone(peerId)
if (fromStorage) return fromStorage
const res = await this.call({
_: 'contacts.getContacts',
hash: Long.ZERO,
})
assertTypeIs('contacts.getContacts', res, 'contacts.contacts')
const found = res.users.find(
(it) => (it as tl.RawUser).phone === peerId
)
if (found && found._ === 'user')
return {
_: 'inputPeerUser',
userId: found.id,
accessHash: found.accessHash!,
}
throw new MtNotFoundError(
`Could not find a peer by phone ${peerId}`
)
} else {
// username
if (!force) {
const fromStorage = await this.storage.getPeerByUsername(peerId)
if (fromStorage) return fromStorage
}
const res = await this.call({
_: 'contacts.resolveUsername',
username: peerId,
})
if (res.peer._ === 'peerUser') {
const id = res.peer.userId
const found = res.users.find((it) => it.id === id)
if (found && found._ === 'user')
return {
_: 'inputPeerUser',
userId: found.id,
accessHash: found.accessHash!,
}
} else {
const id =
res.peer._ === 'peerChannel'
? res.peer.channelId
: res.peer.chatId
const found = res.chats.find((it) => it.id === id)
if (found)
switch (found._) {
case 'channel':
case 'channelForbidden':
return {
_: 'inputPeerChannel',
channelId: found.id,
accessHash: found.accessHash!,
}
case 'chat':
case 'chatForbidden':
return {
_: 'inputPeerChat',
chatId: found.id,
}
}
}
throw new MtNotFoundError(
`Could not find a peer by username ${peerId}`
)
}
}
const peerType = getBasicPeerType(peerId)
// try fetching by id, with access_hash set to 0
switch (peerType) {
case 'user': {
const res = await this.call({
_: 'users.getUsers',
id: [
{
_: 'inputUser',
userId: peerId,
accessHash: Long.ZERO,
},
],
})
const found = res.find((it) => it.id === peerId)
if (found && found._ === 'user')
return {
_: 'inputPeerUser',
userId: found.id,
accessHash: found.accessHash!,
}
break
}
case 'chat': {
// do we really need to make a call?
// const id = -peerId
// const res = await this.call({
// _: 'messages.getChats',
// id: [id],
// })
//
// const found = res.chats.find((it) => it.id === id)
// if (found && (found._ === 'chat' || found._ === 'chatForbidden'))
// return {
// _: 'inputPeerChat',
// chatId: found.id
// }
return {
_: 'inputPeerChat',
chatId: -peerId,
}
// break
}
case 'channel': {
const id = toggleChannelIdMark(peerId as number)
const res = await this.call({
_: 'channels.getChannels',
id: [
{
_: 'inputChannel',
channelId: id,
accessHash: Long.ZERO,
},
],
})
const found = res.chats.find((it) => it.id === id)
if (
found &&
(found._ === 'channel' || found._ === 'channelForbidden')
)
return {
_: 'inputPeerChannel',
channelId: found.id,
accessHash: found.accessHash!,
}
break
}
}
throw new MtNotFoundError(`Could not find a peer by ID ${peerId}`)
}