fix: various improvements
This commit is contained in:
parent
022481966b
commit
4ac6e439a9
5 changed files with 55 additions and 22 deletions
|
@ -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<ParsedUpdateHandlerParams, 'onUpdate' | 'onRawUpdate'>
|
||||
updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
|
||||
}
|
||||
|
||||
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<ChatMember>
|
||||
}): Promise<ChatMember | null>
|
||||
/**
|
||||
* 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)
|
||||
|
|
|
@ -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<ParsedUpdateHandlerParams, 'onUpdate' | 'onRawUpdate'>
|
||||
updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
|
||||
}
|
||||
|
||||
// @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)
|
||||
|
|
|
@ -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<ChatMember> {
|
||||
): Promise<ChatMember | null> {
|
||||
const { chatId, userId } = params
|
||||
|
||||
const user = await resolvePeer(client, userId)
|
||||
|
@ -52,8 +57,9 @@ export async function getChatMember(
|
|||
}
|
||||
}
|
||||
|
||||
throw new tl.RpcError(404, 'USER_NOT_PARTICIPANT')
|
||||
return null
|
||||
} else if (isInputPeerChannel(chat)) {
|
||||
try {
|
||||
const res = await client.call({
|
||||
_: 'channels.getParticipant',
|
||||
channel: normalizeToInputChannel(chat),
|
||||
|
@ -63,5 +69,12 @@ export async function getChatMember(
|
|||
const peers = PeersIndex.from(res)
|
||||
|
||||
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')
|
||||
}
|
||||
|
|
|
@ -154,7 +154,7 @@ export class MessageEntity {
|
|||
*/
|
||||
is<const T extends MessageEntityKind>(
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue