From 4ac6e439a9d08c7a52731a837a761b89624f27ef Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Thu, 26 Oct 2023 22:23:25 +0300 Subject: [PATCH] fix: various improvements --- packages/client/src/client.ts | 18 +++++++--- packages/client/src/methods/_init.ts | 14 ++++++-- .../src/methods/chats/get-chat-member.ts | 35 +++++++++++++------ .../src/types/messages/message-entity.ts | 2 +- packages/tl-runtime/src/reader.ts | 8 +++-- 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 3133b0a3..2a9b4565 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -210,7 +210,12 @@ import { sendStory } from './methods/stories/send-story.js' import { sendStoryReaction } from './methods/stories/send-story-reaction.js' import { togglePeerStoriesArchived } from './methods/stories/toggle-peer-stories-archived.js' import { toggleStoriesPinned } from './methods/stories/toggle-stories-pinned.js' -import { enableUpdatesProcessing, makeParsedUpdateHandler, ParsedUpdateHandlerParams } from './methods/updates/index.js' +import { + enableUpdatesProcessing, + makeParsedUpdateHandler, + ParsedUpdateHandlerParams, + UpdatesManagerParams, +} from './methods/updates/index.js' import { catchUp, enableRps, @@ -322,7 +327,7 @@ interface TelegramClientOptions extends BaseTelegramClientOptions { /** * Parameters for updates manager. */ - updates?: Omit + updates?: Omit } export interface TelegramClient extends BaseTelegramClient { @@ -1409,14 +1414,14 @@ export interface TelegramClient extends BaseTelegramClient { * * @param chatId Chat ID or username * @param userId User ID, username, phone number, `"me"` or `"self"` - * @throws UserNotParticipantError In case given user is not a participant of a given chat + * @returns Chat member, or `null` if user is not a member of the chat */ getChatMember(params: { /** Chat ID or username */ chatId: InputPeerLike /** User ID, username, phone number, `"me"` or `"self"` */ userId: InputPeerLike - }): Promise + }): Promise /** * Get a chunk of members of some chat. * @@ -5088,9 +5093,12 @@ export class TelegramClient extends BaseTelegramClient { super(opts) if (!opts.disableUpdates) { + const { messageGroupingInterval, ...managerParams } = opts.updates ?? {} + enableUpdatesProcessing(this, { + ...managerParams, onUpdate: makeParsedUpdateHandler({ - ...opts.updates, + messageGroupingInterval, onUpdate: (update) => { Conversation.handleUpdate(this, update) this.emit('update', update) diff --git a/packages/client/src/methods/_init.ts b/packages/client/src/methods/_init.ts index 34d3d4bc..6e1fa01b 100644 --- a/packages/client/src/methods/_init.ts +++ b/packages/client/src/methods/_init.ts @@ -8,23 +8,31 @@ import { Conversation } from '../types/conversation.js' // @copy import { start } from './auth/start.js' // @copy -import { enableUpdatesProcessing, makeParsedUpdateHandler, ParsedUpdateHandlerParams } from './updates/index.js' +import { + enableUpdatesProcessing, + makeParsedUpdateHandler, + ParsedUpdateHandlerParams, + UpdatesManagerParams, +} from './updates/index.js' // @copy interface TelegramClientOptions extends BaseTelegramClientOptions { /** * Parameters for updates manager. */ - updates?: Omit + updates?: Omit } // @initialize /** @internal */ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) { if (!opts.disableUpdates) { + const { messageGroupingInterval, ...managerParams } = opts.updates ?? {} + enableUpdatesProcessing(this, { + ...managerParams, onUpdate: makeParsedUpdateHandler({ - ...opts.updates, + messageGroupingInterval, onUpdate: (update) => { Conversation.handleUpdate(this, update) this.emit('update', update) diff --git a/packages/client/src/methods/chats/get-chat-member.ts b/packages/client/src/methods/chats/get-chat-member.ts index 940dce32..31c19216 100644 --- a/packages/client/src/methods/chats/get-chat-member.ts +++ b/packages/client/src/methods/chats/get-chat-member.ts @@ -2,7 +2,12 @@ import { BaseTelegramClient, tl } from '@mtcute/core' import { assertTypeIs } from '@mtcute/core/utils.js' import { ChatMember, InputPeerLike, MtInvalidPeerTypeError, PeersIndex } from '../../types/index.js' -import { isInputPeerChannel, isInputPeerChat, isInputPeerUser, normalizeToInputChannel } from '../../utils/peer-utils.js' +import { + isInputPeerChannel, + isInputPeerChat, + isInputPeerUser, + normalizeToInputChannel, +} from '../../utils/peer-utils.js' import { resolvePeer } from '../users/resolve-peer.js' /** @@ -10,7 +15,7 @@ import { resolvePeer } from '../users/resolve-peer.js' * * @param chatId Chat ID or username * @param userId User ID, username, phone number, `"me"` or `"self"` - * @throws UserNotParticipantError In case given user is not a participant of a given chat + * @returns Chat member, or `null` if user is not a member of the chat */ export async function getChatMember( client: BaseTelegramClient, @@ -20,7 +25,7 @@ export async function getChatMember( /** User ID, username, phone number, `"me"` or `"self"` */ userId: InputPeerLike }, -): Promise { +): Promise { const { chatId, userId } = params const user = await resolvePeer(client, userId) @@ -52,16 +57,24 @@ export async function getChatMember( } } - throw new tl.RpcError(404, 'USER_NOT_PARTICIPANT') + return null } else if (isInputPeerChannel(chat)) { - const res = await client.call({ - _: 'channels.getParticipant', - channel: normalizeToInputChannel(chat), - participant: user, - }) + try { + const res = await client.call({ + _: 'channels.getParticipant', + channel: normalizeToInputChannel(chat), + participant: user, + }) - const peers = PeersIndex.from(res) + const peers = PeersIndex.from(res) - return new ChatMember(res.participant, peers) + return new ChatMember(res.participant, peers) + } catch (e) { + if (tl.RpcError.is(e, 'USER_NOT_PARTICIPANT')) { + return null + } + + throw e + } } else throw new MtInvalidPeerTypeError(chatId, 'chat or channel') } diff --git a/packages/client/src/types/messages/message-entity.ts b/packages/client/src/types/messages/message-entity.ts index 209e06aa..7135e2dc 100644 --- a/packages/client/src/types/messages/message-entity.ts +++ b/packages/client/src/types/messages/message-entity.ts @@ -154,7 +154,7 @@ export class MessageEntity { */ is( kind: T, - ): this is MessageEntity & { content: Extract; kind: T } { + ): this is MessageEntity & { params: Extract; kind: T } { return this.params.kind === kind } } diff --git a/packages/tl-runtime/src/reader.ts b/packages/tl-runtime/src/reader.ts index 99abefdc..a2f2ca0e 100644 --- a/packages/tl-runtime/src/reader.ts +++ b/packages/tl-runtime/src/reader.ts @@ -215,8 +215,12 @@ export class TlBinaryReader { vector(reader = this.object, bare = false): unknown[] { if (!bare) { - if (this.uint() !== 0x1cb5c415) { - throw new Error('Invalid object code, expected 0x1cb5c415 (vector)') + const uint = this.uint() + + if (uint !== 0x1cb5c415) { + throw new Error( + `Invalid object code, expected 0x1cb5c415 (vector), got 0x${uint.toString(16)} at ${this.pos - 4}`, + ) } }