From 0f1edcfcf773ca51333710331af3e6b6c44ae284 Mon Sep 17 00:00:00 2001 From: teidesu Date: Tue, 11 May 2021 21:46:59 +0300 Subject: [PATCH] refactor: use UsersIndex and ChatsIndex types instead of bare record types --- packages/client/src/client.ts | 14 ++++++++-- packages/client/src/methods/_imports.ts | 4 ++- packages/client/src/methods/updates.ts | 5 ++-- .../client/src/types/bots/callback-query.ts | 6 ++-- .../client/src/types/bots/inline-query.ts | 6 ++-- packages/client/src/types/media/poll.ts | 5 ++-- packages/client/src/types/messages/dialog.ts | 10 +++---- packages/client/src/types/messages/message.ts | 10 +++---- packages/client/src/types/peers/chat-event.ts | 9 +++--- .../src/types/peers/chat-invite-link.ts | 5 ++-- .../client/src/types/peers/chat-member.ts | 5 ++-- packages/client/src/types/peers/chat.ts | 10 +++---- packages/client/src/types/peers/index.ts | 3 ++ packages/client/src/utils/peer-utils.ts | 9 +++--- packages/dispatcher/src/dispatcher.ts | 16 ++++++----- packages/dispatcher/src/handler.ts | 10 ++++--- .../src/updates/chat-member-update.ts | 10 ++++--- .../src/updates/chosen-inline-result.ts | 6 ++-- .../dispatcher/src/updates/poll-update.ts | 28 +++++++++++++------ packages/dispatcher/src/updates/poll-vote.ts | 15 ++++++++-- 20 files changed, 116 insertions(+), 70 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 834d50e2..27b74dbc 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -143,6 +143,7 @@ import { ChatInviteLink, ChatMember, ChatPreview, + ChatsIndex, Dialog, FileDownloadParameters, InputChatPermissions, @@ -166,6 +167,7 @@ import { UploadFileLike, UploadedFile, User, + UsersIndex, } from './types' import { MaybeArray, MaybeAsync, TelegramConnection } from '@mtcute/core' import { Lock } from './utils/lock' @@ -490,6 +492,7 @@ export interface TelegramClient extends BaseTelegramClient { * @param queryId Inline query ID * @param results Results of the query * @param params Additional parameters + */ answerInlineQuery( queryId: tl.Long, @@ -691,6 +694,7 @@ export interface TelegramClient extends BaseTelegramClient { * * @param chatId Chat ID * @param params + */ getChatEventLog( chatId: InputPeerLike, @@ -843,6 +847,7 @@ export interface TelegramClient extends BaseTelegramClient { * * @param latitude Latitude of the location * @param longitude Longitude of the location + */ getNearbyChats(latitude: number, longitude: number): Promise /** @@ -1054,6 +1059,7 @@ export interface TelegramClient extends BaseTelegramClient { * * @param folder Parameters for the folder * @returns Newly created folder + */ createFolder( folder: PartialExcept @@ -1102,6 +1108,7 @@ export interface TelegramClient extends BaseTelegramClient { * is not considered when sorting. * * @param params Fetch parameters + */ getDialogs(params?: { /** @@ -1740,6 +1747,7 @@ export interface TelegramClient extends BaseTelegramClient { * * @param chatId Chat ID * @param message ID of one of the messages in the group + */ getMessageGroup(chatId: InputPeerLike, message: number): Promise /** @@ -1956,6 +1964,7 @@ export interface TelegramClient extends BaseTelegramClient { * @param fromChatId Target chat ID * @param message Message ID to forward * @param params + */ sendCopy( toChatId: InputPeerLike, @@ -2147,6 +2156,7 @@ export interface TelegramClient extends BaseTelegramClient { * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` * @param text Text of the message * @param params Additional sending parameters + */ sendText( chatId: InputPeerLike, @@ -2528,8 +2538,8 @@ export interface TelegramClient extends BaseTelegramClient { */ dispatchUpdate( update: tl.TypeUpdate | tl.TypeMessage, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): void /** * Catch up with the server by loading missed updates. diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index a1606aca..4dce8e3b 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -35,7 +35,9 @@ import { TypingStatus, Photo, ChatEvent, - ChatInviteLink + ChatInviteLink, + UsersIndex, + ChatsIndex } from '../types' // @copy diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index 98181595..3b8c6b93 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -11,6 +11,7 @@ import { Lock } from '../utils/lock' import bigInt from 'big-integer' import { MAX_CHANNEL_ID } from '@mtcute/core' import { isDummyUpdate, isDummyUpdates } from '../utils/updates-utils' +import { ChatsIndex, UsersIndex } from '../types' const debug = require('debug')('mtcute:upds') @@ -135,8 +136,8 @@ export async function _saveStorage(this: TelegramClient): Promise { export function dispatchUpdate( this: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): void { // no-op // } diff --git a/packages/client/src/types/bots/callback-query.ts b/packages/client/src/types/bots/callback-query.ts index 44be9866..51e4c675 100644 --- a/packages/client/src/types/bots/callback-query.ts +++ b/packages/client/src/types/bots/callback-query.ts @@ -5,7 +5,7 @@ import { Message } from '../messages' import { MtCuteArgumentError } from '../errors' import { getMarkedPeerId } from '@mtcute/core' import { encodeInlineMessageId } from '../../utils/inline-utils' -import { User } from '../peers' +import { User, UsersIndex } from '../peers' /** * An incoming callback query, originated from a callback button @@ -17,12 +17,12 @@ export class CallbackQuery { | tl.RawUpdateBotCallbackQuery | tl.RawUpdateInlineBotCallbackQuery - readonly _users: Record + readonly _users: UsersIndex constructor( client: TelegramClient, raw: tl.RawUpdateBotCallbackQuery | tl.RawUpdateInlineBotCallbackQuery, - users: Record + users: UsersIndex ) { this.client = client this.raw = raw diff --git a/packages/client/src/types/bots/inline-query.ts b/packages/client/src/types/bots/inline-query.ts index 5ca91609..16c1a111 100644 --- a/packages/client/src/types/bots/inline-query.ts +++ b/packages/client/src/types/bots/inline-query.ts @@ -1,6 +1,6 @@ import { makeInspectable } from '@mtcute/client/src/types/utils' import { tl } from '@mtcute/tl' -import { PeerType, User } from '../peers' +import { PeerType, User, UsersIndex } from '../peers' import { TelegramClient } from '../../client' import { Location } from '../media' import { InputInlineResult } from './input' @@ -18,12 +18,12 @@ export class InlineQuery { readonly raw: tl.RawUpdateBotInlineQuery /** Map of users in this message. Mainly for internal use */ - readonly _users: Record + readonly _users: UsersIndex constructor( client: TelegramClient, raw: tl.RawUpdateBotInlineQuery, - users: Record + users: UsersIndex ) { this.client = client this.raw = raw diff --git a/packages/client/src/types/media/poll.ts b/packages/client/src/types/media/poll.ts index 2ead6446..07f5fa35 100644 --- a/packages/client/src/types/media/poll.ts +++ b/packages/client/src/types/media/poll.ts @@ -3,6 +3,7 @@ import { tl } from '@mtcute/tl' import { TelegramClient } from '../../client' import { MessageEntity } from '../messages' import bigInt from 'big-integer' +import { UsersIndex } from '../peers' export namespace Poll { export interface PollAnswer { @@ -41,12 +42,12 @@ export class Poll { readonly raw: tl.TypePoll readonly results?: tl.TypePollResults - readonly _users: Record + readonly _users: UsersIndex constructor( client: TelegramClient, raw: tl.TypePoll, - users: Record, + users: UsersIndex, results?: tl.TypePollResults ) { this.client = client diff --git a/packages/client/src/types/messages/dialog.ts b/packages/client/src/types/messages/dialog.ts index e553b5df..b2472212 100644 --- a/packages/client/src/types/messages/dialog.ts +++ b/packages/client/src/types/messages/dialog.ts @@ -1,6 +1,6 @@ import { TelegramClient } from '../../client' import { tl } from '@mtcute/tl' -import { Chat } from '../peers' +import { Chat, ChatsIndex, UsersIndex } from '../peers' import { Message } from './message' import { DraftMessage } from './draft-message' import { makeInspectable } from '../utils' @@ -18,10 +18,10 @@ export class Dialog { readonly raw: tl.RawDialog /** Map of users in this object. Mainly for internal use */ - readonly _users: Record + readonly _users: UsersIndex /** Map of chats in this object. Mainly for internal use */ - readonly _chats: Record + readonly _chats: ChatsIndex /** Map of messages in this object. Mainly for internal use */ readonly _messages: Record @@ -29,8 +29,8 @@ export class Dialog { constructor( client: TelegramClient, raw: tl.RawDialog, - users: Record, - chats: Record, + users: UsersIndex, + chats: ChatsIndex, messages: Record ) { this.client = client diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index f1537025..f8f6a47e 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -1,4 +1,4 @@ -import { User, Chat, InputPeerLike } from '../peers' +import { User, Chat, InputPeerLike, UsersIndex, ChatsIndex } from '../peers' import { tl } from '@mtcute/tl' import { BotKeyboard, ReplyMarkup } from '../bots' import { MAX_CHANNEL_ID } from '@mtcute/core' @@ -90,17 +90,17 @@ export class Message { readonly raw: tl.RawMessage | tl.RawMessageService /** Map of users in this message. Mainly for internal use */ - readonly _users: Record + readonly _users: UsersIndex /** Map of chats in this message. Mainly for internal use */ - readonly _chats: Record + readonly _chats: ChatsIndex private _emptyError?: MtCuteEmptyError constructor( client: TelegramClient, raw: tl.TypeMessage, - users: Record, - chats: Record, + users: UsersIndex, + chats: ChatsIndex, isScheduled = false ) { this.client = client diff --git a/packages/client/src/types/peers/chat-event.ts b/packages/client/src/types/peers/chat-event.ts index 61997010..96b526d6 100644 --- a/packages/client/src/types/peers/chat-event.ts +++ b/packages/client/src/types/peers/chat-event.ts @@ -8,6 +8,7 @@ import { Message } from '../messages' import { ChatPermissions } from './chat-permissions' import { ChatLocation } from './chat-location' import { ChatInviteLink } from './chat-invite-link' +import { ChatsIndex, UsersIndex } from './index' export namespace ChatEvent { /** A user has joined the group (in the case of big groups, info of the user that has joined isn't shown) */ @@ -496,14 +497,14 @@ export class ChatEvent { readonly client: TelegramClient readonly raw: tl.TypeChannelAdminLogEvent - readonly _users: Record - readonly _chats: Record + readonly _users: UsersIndex + readonly _chats: ChatsIndex constructor( client: TelegramClient, raw: tl.TypeChannelAdminLogEvent, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ) { this.client = client this.raw = raw diff --git a/packages/client/src/types/peers/chat-invite-link.ts b/packages/client/src/types/peers/chat-invite-link.ts index 7c0bd2cb..391c9413 100644 --- a/packages/client/src/types/peers/chat-invite-link.ts +++ b/packages/client/src/types/peers/chat-invite-link.ts @@ -2,6 +2,7 @@ import { makeInspectable } from '../utils' import { TelegramClient } from '../../client' import { tl } from '@mtcute/tl' import { User } from './user' +import { UsersIndex } from './index' export namespace ChatInviteLink { export interface JoinedMember { @@ -17,9 +18,9 @@ export class ChatInviteLink { readonly client: TelegramClient readonly raw: tl.RawChatInviteExported - readonly _users?: Record + readonly _users?: UsersIndex - constructor (client: TelegramClient, raw: tl.RawChatInviteExported, users?: Record) { + constructor (client: TelegramClient, raw: tl.RawChatInviteExported, users?: UsersIndex) { this.client = client this.raw = raw this._users = users diff --git a/packages/client/src/types/peers/chat-member.ts b/packages/client/src/types/peers/chat-member.ts index 64e5412e..28318216 100644 --- a/packages/client/src/types/peers/chat-member.ts +++ b/packages/client/src/types/peers/chat-member.ts @@ -4,6 +4,7 @@ import { tl } from '@mtcute/tl' import { User } from './user' import { assertTypeIs } from '../../utils/type-assertion' import { ChatPermissions } from './chat-permissions' +import { UsersIndex } from './index' export namespace ChatMember { /** @@ -32,12 +33,12 @@ export class ChatMember { readonly raw: tl.TypeChatParticipant | tl.TypeChannelParticipant /** Map of users in this object. Mainly for internal use */ - readonly _users: Record + readonly _users: UsersIndex constructor( client: TelegramClient, raw: tl.TypeChatParticipant | tl.TypeChannelParticipant, - users: Record, + users: UsersIndex, ) { this.client = client this.raw = raw diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 467ec9db..7d486244 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -5,7 +5,7 @@ import { TelegramClient } from '../../client' import { getMarkedPeerId, MaybeArray } from '@mtcute/core' import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors' import { makeInspectable } from '../utils' -import { InputPeerLike, User } from './index' +import { ChatsIndex, InputPeerLike, User, UsersIndex } from './index' import { ChatLocation } from './chat-location' export namespace Chat { @@ -454,8 +454,8 @@ export class Chat { static _parseFromMessage( client: TelegramClient, message: tl.RawMessage | tl.RawMessageService, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): Chat { return Chat._parseFromPeer(client, message.peerId, users, chats) } @@ -464,8 +464,8 @@ export class Chat { static _parseFromPeer( client: TelegramClient, peer: tl.TypePeer, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): Chat { if (peer._ === 'peerUser') { return new Chat(client, users[peer.userId]) diff --git a/packages/client/src/types/peers/index.ts b/packages/client/src/types/peers/index.ts index 9ca0d076..2d8e316c 100644 --- a/packages/client/src/types/peers/index.ts +++ b/packages/client/src/types/peers/index.ts @@ -35,3 +35,6 @@ export type InputPeerLike = | tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel + +export type UsersIndex = Record +export type ChatsIndex = Record diff --git a/packages/client/src/utils/peer-utils.ts b/packages/client/src/utils/peer-utils.ts index 84a2caf3..38968773 100644 --- a/packages/client/src/utils/peer-utils.ts +++ b/packages/client/src/utils/peer-utils.ts @@ -1,5 +1,6 @@ import { tl } from '@mtcute/tl' import bigInt from 'big-integer' +import { ChatsIndex, UsersIndex } from '../types' export const INVITE_LINK_REGEX = /^(?:https?:\/\/)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)\/joinchat\/)([\w-]+)$/i @@ -103,11 +104,11 @@ export function createUsersChatsIndex( chats?: tl.TypeChat[] } ): { - users: Record - chats: Record + users: UsersIndex + chats: ChatsIndex } { - const users: Record = {} - const chats: Record = {} + const users: UsersIndex = {} + const chats: ChatsIndex = {} obj.users?.forEach((e) => (users[e.id] = e)) obj.chats?.forEach((e) => (chats[e.id] = e)) diff --git a/packages/dispatcher/src/dispatcher.ts b/packages/dispatcher/src/dispatcher.ts index 4cacad61..ff9ab167 100644 --- a/packages/dispatcher/src/dispatcher.ts +++ b/packages/dispatcher/src/dispatcher.ts @@ -1,9 +1,11 @@ import { CallbackQuery, + ChatsIndex, InlineQuery, Message, MtCuteArgumentError, TelegramClient, + UsersIndex, } from '@mtcute/client' import { tl } from '@mtcute/tl' import { @@ -42,8 +44,8 @@ const noop = () => {} type ParserFunction = ( client: TelegramClient, upd: tl.TypeUpdate | tl.TypeMessage, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ) => any type UpdateParser = [Exclude, ParserFunction] @@ -73,7 +75,7 @@ const callbackQueryParser: UpdateParser = [ ] const userTypingParser: UpdateParser = [ 'user_typing', - (client, upd) => new UserTypingUpdate(client, upd as any) + (client, upd) => new UserTypingUpdate(client, upd as any), ] const PARSERS: Partial< @@ -182,8 +184,8 @@ export class Dispatcher { */ dispatchUpdate( update: tl.TypeUpdate | tl.TypeMessage, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): void { if (!this._client) return @@ -208,8 +210,8 @@ export class Dispatcher { */ async dispatchUpdateNow( update: tl.TypeUpdate | tl.TypeMessage, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ): Promise { if (!this._client) return diff --git a/packages/dispatcher/src/handler.ts b/packages/dispatcher/src/handler.ts index 17f7df90..71bb5c2d 100644 --- a/packages/dispatcher/src/handler.ts +++ b/packages/dispatcher/src/handler.ts @@ -4,6 +4,8 @@ import { TelegramClient, InlineQuery, CallbackQuery, + UsersIndex, + ChatsIndex, } from '@mtcute/client' import { tl } from '@mtcute/tl' import { PropagationSymbol } from './propagation' @@ -35,14 +37,14 @@ export type RawUpdateHandler = BaseUpdateHandler< ( client: TelegramClient, update: tl.TypeUpdate, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ) => MaybeAsync, ( client: TelegramClient, update: tl.TypeUpdate, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ) => MaybeAsync > diff --git a/packages/dispatcher/src/updates/chat-member-update.ts b/packages/dispatcher/src/updates/chat-member-update.ts index 4ac69080..b103e5fa 100644 --- a/packages/dispatcher/src/updates/chat-member-update.ts +++ b/packages/dispatcher/src/updates/chat-member-update.ts @@ -3,8 +3,10 @@ import { Chat, ChatInviteLink, ChatMember, + ChatsIndex, TelegramClient, User, + UsersIndex, } from '@mtcute/client' import { makeInspectable } from '@mtcute/client/src/types/utils' @@ -49,15 +51,15 @@ export class ChatMemberUpdate { readonly raw: tl.RawUpdateChatParticipant | tl.RawUpdateChannelParticipant /** Map of users in this message. Mainly for internal use */ - readonly _users: Record + readonly _users: UsersIndex /** Map of chats in this message. Mainly for internal use */ - readonly _chats: Record + readonly _chats: ChatsIndex constructor( client: TelegramClient, raw: tl.RawUpdateChatParticipant | tl.RawUpdateChannelParticipant, - users: Record, - chats: Record + users: UsersIndex, + chats: ChatsIndex ) { this.client = client this.raw = raw diff --git a/packages/dispatcher/src/updates/chosen-inline-result.ts b/packages/dispatcher/src/updates/chosen-inline-result.ts index 2e5fa393..d1c83953 100644 --- a/packages/dispatcher/src/updates/chosen-inline-result.ts +++ b/packages/dispatcher/src/updates/chosen-inline-result.ts @@ -4,7 +4,7 @@ import { TelegramClient, User, Location, - MtCuteArgumentError, + MtCuteArgumentError, UsersIndex, } from '@mtcute/client' import { encodeInlineMessageId } from '@mtcute/client/src/utils/inline-utils' @@ -18,12 +18,12 @@ export class ChosenInlineResult { readonly client: TelegramClient readonly raw: tl.RawUpdateBotInlineSend - readonly _users: Record + readonly _users: UsersIndex constructor( client: TelegramClient, raw: tl.RawUpdateBotInlineSend, - users: Record + users: UsersIndex ) { this.client = client this.raw = raw diff --git a/packages/dispatcher/src/updates/poll-update.ts b/packages/dispatcher/src/updates/poll-update.ts index 62c8a481..645d4de4 100644 --- a/packages/dispatcher/src/updates/poll-update.ts +++ b/packages/dispatcher/src/updates/poll-update.ts @@ -1,5 +1,5 @@ import { makeInspectable } from '@mtcute/client/src/types/utils' -import { TelegramClient, Poll } from '@mtcute/client' +import { TelegramClient, Poll, UsersIndex } from '@mtcute/client' import { tl } from '@mtcute/tl' /** @@ -13,9 +13,13 @@ export class PollUpdate { readonly client: TelegramClient readonly raw: tl.RawUpdateMessagePoll - readonly _users: Record + readonly _users: UsersIndex - constructor (client: TelegramClient, raw: tl.RawUpdateMessagePoll, users: Record) { + constructor( + client: TelegramClient, + raw: tl.RawUpdateMessagePoll, + users: UsersIndex + ) { this.client = client this.raw = raw this._users = users @@ -55,15 +59,21 @@ export class PollUpdate { _: 'poll', id: this.raw.pollId, question: '', - answers: this.raw.results.results?.map((res) => ({ - _: 'pollAnswer', - text: '', - option: res.option - })) ?? [] + answers: + this.raw.results.results?.map((res) => ({ + _: 'pollAnswer', + text: '', + option: res.option, + })) ?? [], } } - this._poll = new Poll(this.client, poll, this._users, this.raw.results) + this._poll = new Poll( + this.client, + poll, + this._users, + this.raw.results + ) } return this._poll diff --git a/packages/dispatcher/src/updates/poll-vote.ts b/packages/dispatcher/src/updates/poll-vote.ts index 95c3b75f..065ababb 100644 --- a/packages/dispatcher/src/updates/poll-vote.ts +++ b/packages/dispatcher/src/updates/poll-vote.ts @@ -1,4 +1,9 @@ -import { MtCuteUnsupportedError, TelegramClient, User } from '@mtcute/client' +import { + MtCuteUnsupportedError, + TelegramClient, + User, + UsersIndex, +} from '@mtcute/client' import { tl } from '@mtcute/tl' import { makeInspectable } from '@mtcute/client/src/types/utils' @@ -12,9 +17,13 @@ export class PollVoteUpdate { readonly client: TelegramClient readonly raw: tl.RawUpdateMessagePollVote - readonly _users: Record + readonly _users: UsersIndex - constructor(client: TelegramClient, raw: tl.RawUpdateMessagePollVote, users: Record) { + constructor( + client: TelegramClient, + raw: tl.RawUpdateMessagePollVote, + users: UsersIndex + ) { this.client = client this.raw = raw this._users = users