fix(core)!: throw an error if peer not found for non-bot accounts in resolvePeer
This commit is contained in:
parent
c39be7dcd3
commit
5528ec3afb
4 changed files with 33 additions and 60 deletions
|
@ -1,5 +1,5 @@
|
|||
import Long from 'long'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { createStub, StubTelegramClient } from '@mtcute/test'
|
||||
|
||||
|
@ -20,6 +20,8 @@ describe('sendText', () => {
|
|||
it('should correctly handle updateNewMessage', async () => {
|
||||
const client = new StubTelegramClient()
|
||||
|
||||
await client.registerPeers(stubUser)
|
||||
|
||||
client.respondWith('messages.sendMessage', (req) =>
|
||||
createStub('updates', {
|
||||
users: [stubUser],
|
||||
|
@ -60,6 +62,8 @@ describe('sendText', () => {
|
|||
it('should correctly handle updateNewChannelMessage', async () => {
|
||||
const client = new StubTelegramClient()
|
||||
|
||||
await client.registerPeers(stubChannel, stubUser)
|
||||
|
||||
client.respondWith('messages.sendMessage', (req) =>
|
||||
createStub('updates', {
|
||||
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()
|
||||
|
||||
await client.storage.self.store({
|
||||
|
@ -128,42 +132,4 @@ describe('sendText', () => {
|
|||
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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Long from 'long'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { beforeAll, describe, expect, it } from 'vitest'
|
||||
|
||||
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 () => {
|
||||
expect(await getUsers(client, [123, 456])).toEqual([
|
||||
new User(createStub('user', { id: 123, accessHash: Long.ZERO })),
|
||||
|
|
|
@ -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 resolved = await resolvePeer(client, 123)
|
||||
|
||||
expect(resolved).toEqual({
|
||||
_: 'inputPeerUser',
|
||||
userId: 123,
|
||||
accessHash: Long.ZERO,
|
||||
})
|
||||
await expect(resolvePeer(client, 123)).rejects.toThrow(MtPeerNotFoundError)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -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 resolved = await resolvePeer(client, -1000000000123)
|
||||
|
||||
expect(resolved).toEqual({
|
||||
_: 'inputPeerChannel',
|
||||
channelId: 123,
|
||||
accessHash: Long.ZERO,
|
||||
})
|
||||
await expect(resolvePeer(client, -1000000000123)).rejects.toThrow(MtPeerNotFoundError)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -133,12 +133,14 @@ export async function resolvePeer(
|
|||
throw new MtPeerNotFoundError(`Could not find a peer by ${peerId}`)
|
||||
}
|
||||
|
||||
// in some cases, the server allows us to use access_hash=0.
|
||||
// particularly, when we're a bot or we're referencing a user
|
||||
// who we have "seen" recently
|
||||
// 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) {
|
||||
throw new MtPeerNotFoundError(`Peer ${peerId} is not found in local cache`)
|
||||
}
|
||||
|
||||
switch (peerType) {
|
||||
case 'user':
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue