fix(core)!: throw an error if peer not found for non-bot accounts in resolvePeer

This commit is contained in:
alina 🌸 2024-05-30 18:07:46 +03:00
parent c39be7dcd3
commit 5528ec3afb
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
4 changed files with 33 additions and 60 deletions

View file

@ -1,5 +1,5 @@
import Long from 'long' import Long from 'long'
import { describe, expect, it, vi } from 'vitest' import { describe, expect, it } from 'vitest'
import { createStub, StubTelegramClient } from '@mtcute/test' import { createStub, StubTelegramClient } from '@mtcute/test'
@ -20,6 +20,8 @@ describe('sendText', () => {
it('should correctly handle updateNewMessage', async () => { it('should correctly handle updateNewMessage', async () => {
const client = new StubTelegramClient() const client = new StubTelegramClient()
await client.registerPeers(stubUser)
client.respondWith('messages.sendMessage', (req) => client.respondWith('messages.sendMessage', (req) =>
createStub('updates', { createStub('updates', {
users: [stubUser], users: [stubUser],
@ -60,6 +62,8 @@ describe('sendText', () => {
it('should correctly handle updateNewChannelMessage', async () => { it('should correctly handle updateNewChannelMessage', async () => {
const client = new StubTelegramClient() const client = new StubTelegramClient()
await client.registerPeers(stubChannel, stubUser)
client.respondWith('messages.sendMessage', (req) => client.respondWith('messages.sendMessage', (req) =>
createStub('updates', { createStub('updates', {
users: [stubUser], users: [stubUser],
@ -100,7 +104,7 @@ describe('sendText', () => {
}) })
}) })
it('should correctly handle updateShortSentMessage with cached peer', async () => { it('should correctly handle updateShortSentMessage', async () => {
const client = new StubTelegramClient() const client = new StubTelegramClient()
await client.storage.self.store({ await client.storage.self.store({
@ -128,42 +132,4 @@ describe('sendText', () => {
expect(msg.text).toEqual('test') expect(msg.text).toEqual('test')
}) })
}) })
it('should correctly handle updateShortSentMessage without cached peer', async () => {
const client = new StubTelegramClient()
await client.storage.self.store({
userId: stubUser.id,
isBot: false,
isPremium: false,
usernames: [],
})
const getUsersFn = client.respondWith(
'users.getUsers',
vi.fn(() => [stubUser]),
)
client.respondWith('messages.sendMessage', () =>
createStub('updateShortSentMessage', {
id: 123,
out: true,
}),
)
await client.with(async () => {
const msg = await sendText(client, stubUser.id, 'test')
expect(getUsersFn).toHaveBeenCalledWith({
_: 'users.getUsers',
id: [{ _: 'inputUser', userId: stubUser.id, accessHash: Long.ZERO }],
})
expect(msg).toBeDefined()
expect(msg.id).toEqual(123)
expect(msg.chat.chatType).toEqual('private')
expect(msg.chat.id).toEqual(stubUser.id)
expect(msg.text).toEqual('test')
})
})
}) })

View file

@ -1,5 +1,5 @@
import Long from 'long' import Long from 'long'
import { describe, expect, it } from 'vitest' import { beforeAll, describe, expect, it } from 'vitest'
import { createStub, StubTelegramClient } from '@mtcute/test' import { createStub, StubTelegramClient } from '@mtcute/test'
@ -20,6 +20,23 @@ describe('getUsers', () => {
}), }),
) )
beforeAll(async () => {
await client.registerPeers(
createStub('user', {
id: 123,
accessHash: Long.fromBits(123, 456),
}),
createStub('user', {
id: 456,
accessHash: Long.fromBits(123, 456),
}),
createStub('user', {
id: 1,
accessHash: Long.fromBits(123, 456),
}),
)
})
it('should return users returned by users.getUsers', async () => { it('should return users returned by users.getUsers', async () => {
expect(await getUsers(client, [123, 456])).toEqual([ expect(await getUsers(client, [123, 456])).toEqual([
new User(createStub('user', { id: 123, accessHash: Long.ZERO })), new User(createStub('user', { id: 123, accessHash: Long.ZERO })),

View file

@ -140,16 +140,10 @@ describe('resolvePeer', () => {
}) })
}) })
it('should return user with zero hash if not in storage', async () => { it('should throw if not in storage', async () => {
const client = new StubTelegramClient() const client = new StubTelegramClient()
const resolved = await resolvePeer(client, 123) await expect(resolvePeer(client, 123)).rejects.toThrow(MtPeerNotFoundError)
expect(resolved).toEqual({
_: 'inputPeerUser',
userId: 123,
accessHash: Long.ZERO,
})
}) })
}) })
@ -198,16 +192,10 @@ describe('resolvePeer', () => {
}) })
}) })
it('should return channel with zero hash if not in storage', async () => { it('should throw if not in storage', async () => {
const client = new StubTelegramClient() const client = new StubTelegramClient()
const resolved = await resolvePeer(client, -1000000000123) await expect(resolvePeer(client, -1000000000123)).rejects.toThrow(MtPeerNotFoundError)
expect(resolved).toEqual({
_: 'inputPeerChannel',
channelId: 123,
accessHash: Long.ZERO,
})
}) })
}) })

View file

@ -133,12 +133,14 @@ export async function resolvePeer(
throw new MtPeerNotFoundError(`Could not find a peer by ${peerId}`) throw new MtPeerNotFoundError(`Could not find a peer by ${peerId}`)
} }
// in some cases, the server allows us to use access_hash=0. // in some cases, the server allows bots to use access_hash=0.
// particularly, when we're a bot or we're referencing a user
// who we have "seen" recently
// if it's not the case, we'll get an `PEER_ID_INVALID` error anyways // if it's not the case, we'll get an `PEER_ID_INVALID` error anyways
const [peerType, bareId] = parseMarkedPeerId(peerId) const [peerType, bareId] = parseMarkedPeerId(peerId)
if (peerType !== 'chat' && !client.storage.self.getCached(true)?.isBot) {
throw new MtPeerNotFoundError(`Peer ${peerId} is not found in local cache`)
}
switch (peerType) { switch (peerType) {
case 'user': case 'user':
return { return {