fix(core)!: never return access_hash=0 for bots

This commit is contained in:
alina sireneva
2025-03-08 12:00:06 +03:00
parent a5bc0979e5
commit 9fa4ea8e0e
2 changed files with 32 additions and 24 deletions

View File

@@ -140,7 +140,7 @@ describe('resolvePeer', () => {
})
})
it('should return with zero hash for bots if not in storage', async () => {
it.skip('should return with zero hash for bots if not in storage', async () => {
const client = StubTelegramClient.offline()
await client.storage.self.storeFrom(createStub('user', {
@@ -227,7 +227,7 @@ describe('resolvePeer', () => {
})
})
it('should return with zero hash for bots if not in storage', async () => {
it.skip('should return with zero hash for bots if not in storage', async () => {
const client = StubTelegramClient.offline()
await client.storage.self.storeFrom(createStub('user', {

View File

@@ -157,30 +157,38 @@ export async function resolvePeer(
throw new MtPeerNotFoundError(`Could not find a peer by ${peerId}`)
}
// in some cases, the server allows bots to use access_hash=0.
// if it's not the case, we'll get an `PEER_ID_INVALID` error anyways
const [peerType, bareId] = parseMarkedPeerId(peerId)
if (peerType === 'chat' || client.storage.self.getCached(true)?.isBot) {
// bots can use access_hash=0 in most of the cases
switch (peerType) {
case 'user':
return {
_: 'inputPeerUser',
userId: bareId,
accessHash: Long.ZERO,
}
case 'chat':
return {
_: 'inputPeerChat',
chatId: bareId,
}
case 'channel':
return {
_: 'inputPeerChannel',
channelId: bareId,
accessHash: Long.ZERO,
}
// in some cases, the server allows bots to use access_hash=0.
// however in some cases it fails with PEER_ID_INVALID/CHANNEL_INVALID,
// and currently we don't have a way to gracefully handle those, so just resolve them right away
// (todo: handle those errors)
// if (peerType === 'chat' || client.storage.self.getCached(true)?.isBot) {
// // bots can use access_hash=0 in most of the cases
// switch (peerType) {
// case 'user':
// return {
// _: 'inputPeerUser',
// userId: bareId,
// accessHash: Long.ZERO,
// }
// case 'chat':
// return {
// _: 'inputPeerChat',
// chatId: bareId,
// }
// case 'channel':
// return {
// _: 'inputPeerChannel',
// channelId: bareId,
// accessHash: Long.ZERO,
// }
// }
// }
if (peerType === 'chat') {
return {
_: 'inputPeerChat',
chatId: bareId,
}
}