fix: various improvements

This commit is contained in:
alina 🌸 2023-10-26 22:23:25 +03:00
parent 022481966b
commit 4ac6e439a9
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 55 additions and 22 deletions

View file

@ -210,7 +210,12 @@ import { sendStory } from './methods/stories/send-story.js'
import { sendStoryReaction } from './methods/stories/send-story-reaction.js' import { sendStoryReaction } from './methods/stories/send-story-reaction.js'
import { togglePeerStoriesArchived } from './methods/stories/toggle-peer-stories-archived.js' import { togglePeerStoriesArchived } from './methods/stories/toggle-peer-stories-archived.js'
import { toggleStoriesPinned } from './methods/stories/toggle-stories-pinned.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 { import {
catchUp, catchUp,
enableRps, enableRps,
@ -322,7 +327,7 @@ interface TelegramClientOptions extends BaseTelegramClientOptions {
/** /**
* Parameters for updates manager. * Parameters for updates manager.
*/ */
updates?: Omit<ParsedUpdateHandlerParams, 'onUpdate' | 'onRawUpdate'> updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
} }
export interface TelegramClient extends BaseTelegramClient { export interface TelegramClient extends BaseTelegramClient {
@ -1409,14 +1414,14 @@ export interface TelegramClient extends BaseTelegramClient {
* *
* @param chatId Chat ID or username * @param chatId Chat ID or username
* @param userId User ID, username, phone number, `"me"` or `"self"` * @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: { getChatMember(params: {
/** Chat ID or username */ /** Chat ID or username */
chatId: InputPeerLike chatId: InputPeerLike
/** User ID, username, phone number, `"me"` or `"self"` */ /** User ID, username, phone number, `"me"` or `"self"` */
userId: InputPeerLike userId: InputPeerLike
}): Promise<ChatMember> }): Promise<ChatMember | null>
/** /**
* Get a chunk of members of some chat. * Get a chunk of members of some chat.
* *
@ -5088,9 +5093,12 @@ export class TelegramClient extends BaseTelegramClient {
super(opts) super(opts)
if (!opts.disableUpdates) { if (!opts.disableUpdates) {
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
enableUpdatesProcessing(this, { enableUpdatesProcessing(this, {
...managerParams,
onUpdate: makeParsedUpdateHandler({ onUpdate: makeParsedUpdateHandler({
...opts.updates, messageGroupingInterval,
onUpdate: (update) => { onUpdate: (update) => {
Conversation.handleUpdate(this, update) Conversation.handleUpdate(this, update)
this.emit('update', update) this.emit('update', update)

View file

@ -8,23 +8,31 @@ import { Conversation } from '../types/conversation.js'
// @copy // @copy
import { start } from './auth/start.js' import { start } from './auth/start.js'
// @copy // @copy
import { enableUpdatesProcessing, makeParsedUpdateHandler, ParsedUpdateHandlerParams } from './updates/index.js' import {
enableUpdatesProcessing,
makeParsedUpdateHandler,
ParsedUpdateHandlerParams,
UpdatesManagerParams,
} from './updates/index.js'
// @copy // @copy
interface TelegramClientOptions extends BaseTelegramClientOptions { interface TelegramClientOptions extends BaseTelegramClientOptions {
/** /**
* Parameters for updates manager. * Parameters for updates manager.
*/ */
updates?: Omit<ParsedUpdateHandlerParams, 'onUpdate' | 'onRawUpdate'> updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
} }
// @initialize // @initialize
/** @internal */ /** @internal */
function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) { function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
if (!opts.disableUpdates) { if (!opts.disableUpdates) {
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
enableUpdatesProcessing(this, { enableUpdatesProcessing(this, {
...managerParams,
onUpdate: makeParsedUpdateHandler({ onUpdate: makeParsedUpdateHandler({
...opts.updates, messageGroupingInterval,
onUpdate: (update) => { onUpdate: (update) => {
Conversation.handleUpdate(this, update) Conversation.handleUpdate(this, update)
this.emit('update', update) this.emit('update', update)

View file

@ -2,7 +2,12 @@ import { BaseTelegramClient, tl } from '@mtcute/core'
import { assertTypeIs } from '@mtcute/core/utils.js' import { assertTypeIs } from '@mtcute/core/utils.js'
import { ChatMember, InputPeerLike, MtInvalidPeerTypeError, PeersIndex } from '../../types/index.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' 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 chatId Chat ID or username
* @param userId User ID, username, phone number, `"me"` or `"self"` * @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( export async function getChatMember(
client: BaseTelegramClient, client: BaseTelegramClient,
@ -20,7 +25,7 @@ export async function getChatMember(
/** User ID, username, phone number, `"me"` or `"self"` */ /** User ID, username, phone number, `"me"` or `"self"` */
userId: InputPeerLike userId: InputPeerLike
}, },
): Promise<ChatMember> { ): Promise<ChatMember | null> {
const { chatId, userId } = params const { chatId, userId } = params
const user = await resolvePeer(client, userId) 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)) { } else if (isInputPeerChannel(chat)) {
const res = await client.call({ try {
_: 'channels.getParticipant', const res = await client.call({
channel: normalizeToInputChannel(chat), _: 'channels.getParticipant',
participant: user, 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') } else throw new MtInvalidPeerTypeError(chatId, 'chat or channel')
} }

View file

@ -154,7 +154,7 @@ export class MessageEntity {
*/ */
is<const T extends MessageEntityKind>( is<const T extends MessageEntityKind>(
kind: T, kind: T,
): this is MessageEntity & { content: Extract<MessageEntityParams, { kind: T }>; kind: T } { ): this is MessageEntity & { params: Extract<MessageEntityParams, { kind: T }>; kind: T } {
return this.params.kind === kind return this.params.kind === kind
} }
} }

View file

@ -215,8 +215,12 @@ export class TlBinaryReader {
vector(reader = this.object, bare = false): unknown[] { vector(reader = this.object, bare = false): unknown[] {
if (!bare) { if (!bare) {
if (this.uint() !== 0x1cb5c415) { const uint = this.uint()
throw new Error('Invalid object code, expected 0x1cb5c415 (vector)')
if (uint !== 0x1cb5c415) {
throw new Error(
`Invalid object code, expected 0x1cb5c415 (vector), got 0x${uint.toString(16)} at ${this.pos - 4}`,
)
} }
} }