From c46f113f1fa337f28f8d978caf0a1dca42668edc Mon Sep 17 00:00:00 2001 From: teidesu Date: Wed, 9 Jun 2021 01:47:22 +0300 Subject: [PATCH] build: preparing for publish x5 at this point i'm way too tired to describe what happened. stuff added, stuff removed, stuff fixed, i don't even remember anymore lol --- packages/client/src/index.ts | 1 + packages/client/src/methods/updates.ts | 2 + .../client/src/methods/users/resolve-peer.ts | 10 +++- .../src/types/peers/chat-permissions.ts | 32 +++++----- packages/client/src/types/peers/index.ts | 1 + packages/client/src/types/peers/user.ts | 58 +++++++++---------- packages/client/src/types/utils.ts | 4 +- packages/dispatcher/src/filters.ts | 8 +++ 8 files changed, 66 insertions(+), 50 deletions(-) diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index cb3aca90..5bc0a2a3 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -9,3 +9,4 @@ export * from '@mtcute/tl/errors' export * from './parser' export * from './types' export * from './client' +export * from './utils/peer-utils' diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index b040a8e8..ab085bad 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -897,6 +897,8 @@ export function catchUp(this: TelegramClient): Promise { // we also use a lock here so new updates are not processed // while we are catching up with older ones + this._catchUpChannels = true + return this._updLock .acquire() .then(() => _loadDifference.call(this)) diff --git a/packages/client/src/methods/users/resolve-peer.ts b/packages/client/src/methods/users/resolve-peer.ts index b8f39550..f24bae11 100644 --- a/packages/client/src/methods/users/resolve-peer.ts +++ b/packages/client/src/methods/users/resolve-peer.ts @@ -1,7 +1,7 @@ import { tl } from '@mtcute/tl' import { TelegramClient } from '../../client' import { InputPeerLike, MtCuteNotFoundError } from '../../types' -import { getBasicPeerType, MAX_CHANNEL_ID } from '@mtcute/core' +import { getBasicPeerType, getMarkedPeerId, MAX_CHANNEL_ID } from '@mtcute/core' import bigInt from 'big-integer' import { normalizeToInputPeer } from '../../utils/peer-utils' import { assertTypeIs } from '../../utils/type-assertion' @@ -18,7 +18,13 @@ export async function resolvePeer( peerId: InputPeerLike ): Promise { // for convenience we also accept tl objects directly - if (typeof peerId === 'object') return normalizeToInputPeer(peerId) + if (typeof peerId === 'object') { + if (tl.isAnyPeer(peerId)) { + peerId = getMarkedPeerId(peerId) + } else { + return normalizeToInputPeer(peerId) + } + } if (typeof peerId === 'number') { const fromStorage = await this.storage.getPeerById(peerId) diff --git a/packages/client/src/types/peers/chat-permissions.ts b/packages/client/src/types/peers/chat-permissions.ts index 7ae0f61f..ce04bae0 100644 --- a/packages/client/src/types/peers/chat-permissions.ts +++ b/packages/client/src/types/peers/chat-permissions.ts @@ -5,17 +5,17 @@ import { makeInspectable } from '../utils' * Represents the permissions of a user in a {@link Chat}. */ export class ChatPermissions { - readonly _bannedRights: tl.RawChatBannedRights + readonly raw: tl.RawChatBannedRights constructor(bannedRights: tl.RawChatBannedRights) { - this._bannedRights = bannedRights + this.raw = bannedRights } /** * Whether users can view messages */ get canViewMessages(): boolean { - return !this._bannedRights.viewMessages + return !this.raw.viewMessages } /** @@ -23,7 +23,7 @@ export class ChatPermissions { * contacts, locations and venues */ get canSendMessages(): boolean { - return !this._bannedRights.sendMessages + return !this.raw.sendMessages } /** @@ -33,7 +33,7 @@ export class ChatPermissions { * Implies {@link canSendMessages} */ get canSendMedia(): boolean { - return !this._bannedRights.sendMedia + return !this.raw.sendMedia } /** @@ -42,7 +42,7 @@ export class ChatPermissions { * Implies {@link canSendMedia} */ get canSendStickers(): boolean { - return !this._bannedRights.sendStickers + return !this.raw.sendStickers } /** @@ -51,7 +51,7 @@ export class ChatPermissions { * Implies {@link canSendMedia} */ get canSendGifs(): boolean { - return !this._bannedRights.sendGifs + return !this.raw.sendGifs } /** @@ -60,7 +60,7 @@ export class ChatPermissions { * Implies {@link canSendMedia} */ get canSendGames(): boolean { - return !this._bannedRights.sendGames + return !this.raw.sendGames } /** @@ -69,7 +69,7 @@ export class ChatPermissions { * Implies {@link canSendMedia} */ get canUseInline(): boolean { - return !this._bannedRights.sendInline + return !this.raw.sendInline } /** @@ -78,7 +78,7 @@ export class ChatPermissions { * Implies {@link canSendMedia} */ get canAddWebPreviews(): boolean { - return !this._bannedRights.embedLinks + return !this.raw.embedLinks } /** @@ -87,7 +87,7 @@ export class ChatPermissions { * Implies {@link canSendMessages} */ get canSendPolls(): boolean { - return !this._bannedRights.sendPolls + return !this.raw.sendPolls } /** @@ -95,21 +95,21 @@ export class ChatPermissions { * photo and other settings. */ get canChangeInfo(): boolean { - return !this._bannedRights.changeInfo + return !this.raw.changeInfo } /** * Whether users can invite other users to the chat */ get canInviteUsers(): boolean { - return !this._bannedRights.inviteUsers + return !this.raw.inviteUsers } /** * Whether users can pin messages */ get canPinMessages(): boolean { - return !this._bannedRights.pinMessages + return !this.raw.pinMessages } /** @@ -120,9 +120,9 @@ export class ChatPermissions { * will be lifted from a {@link ChatMember} */ get untilDate(): Date | null { - return this._bannedRights.untilDate === 0 + return this.raw.untilDate === 0 ? null - : new Date(this._bannedRights.untilDate * 1000) + : new Date(this.raw.untilDate * 1000) } } diff --git a/packages/client/src/types/peers/index.ts b/packages/client/src/types/peers/index.ts index 2d8e316c..945c7454 100644 --- a/packages/client/src/types/peers/index.ts +++ b/packages/client/src/types/peers/index.ts @@ -32,6 +32,7 @@ export type PeerType = 'user' | 'bot' | 'group' | 'channel' | 'supergroup' export type InputPeerLike = | string | number + | tl.TypePeer | tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel diff --git a/packages/client/src/types/peers/user.ts b/packages/client/src/types/peers/user.ts index 9a491dfd..352a7755 100644 --- a/packages/client/src/types/peers/user.ts +++ b/packages/client/src/types/peers/user.ts @@ -40,48 +40,48 @@ export class User { /** * Underlying raw TL object */ - private _user: tl.RawUser + readonly raw: tl.RawUser constructor(client: TelegramClient, user: tl.TypeUser) { assertTypeIs('User#init', user, 'user') this.client = client - this._user = user + this.raw = user } /** Unique identifier for this user or bot */ get id(): number { - return this._user.id + return this.raw.id } /** Whether this user is you yourself */ get isSelf(): boolean { - return this._user.self! + return this.raw.self! } /** Whether this user is in your contacts */ get isContact(): boolean { - return this._user.contact! + return this.raw.contact! } /** Whether you both have each other's contact */ get isMutualContact(): boolean { - return this._user.mutualContact! + return this.raw.mutualContact! } /** Whether this user is deleted */ get isDeleted(): boolean { - return this._user.deleted! + return this.raw.deleted! } /** Whether this user is a bot */ get isBot(): boolean { - return this._user.bot! + return this.raw.bot! } /** Whether this user has been verified by Telegram */ get isVerified(): boolean { - return this._user.verified! + return this.raw.verified! } /** @@ -89,32 +89,32 @@ export class User { * See {@link restrictionReason} for details */ get isRestricted(): boolean { - return this._user.restricted! + return this.raw.restricted! } /** Whether this user has been flagged for scam */ get isScam(): boolean { - return this._user.scam! + return this.raw.scam! } /** Whether this user has been flagged for impersonation */ get isFake(): boolean { - return this._user.fake! + return this.raw.fake! } /** Whether this user is part of the Telegram support team */ get isSupport(): boolean { - return this._user.support! + return this.raw.support! } /** User's or bot's first name */ get firstName(): string { - return this._user.firstName ?? 'Deleted Account' + return this.raw.firstName ?? 'Deleted Account' } /** User's or bot's last name */ get lastName(): string | null { - return this._user.lastName ?? null + return this.raw.lastName ?? null } static parseStatus( @@ -163,8 +163,8 @@ export class User { private _parsedStatus?: User.ParsedStatus private _parseStatus() { this._parsedStatus = User.parseStatus( - this._user.status!, - this._user.bot + this.raw.status!, + this.raw.bot ) } @@ -194,12 +194,12 @@ export class User { /** User's or bot's username */ get username(): string | null { - return this._user.username ?? null + return this.raw.username ?? null } /** IETF language tag of the user's language */ get language(): string | null { - return this._user.langCode ?? null + return this.raw.langCode ?? null } /** @@ -213,27 +213,27 @@ export class User { * More info at [Pyrogram FAQ](https://docs.pyrogram.org/faq#what-are-the-ip-addresses-of-telegram-data-centers). */ get dcId(): number | null { - return (this._user.photo as any)?.dcId ?? null + return (this.raw.photo as any)?.dcId ?? null } /** User's phone number */ get phoneNumber(): string | null { - return this._user.phone ?? null + return this.raw.phone ?? null } /** * Get this user's input peer for advanced use-cases. */ get inputPeer(): tl.TypeInputPeer { - if (!this._user.accessHash) + if (!this.raw.accessHash) throw new MtCuteArgumentError( "user's access hash is not available!" ) return { _: 'inputPeerUser', - userId: this._user.id, - accessHash: this._user.accessHash, + userId: this.raw.id, + accessHash: this.raw.accessHash, } } @@ -243,13 +243,13 @@ export class User { * Suitable for downloads only */ get photo(): ChatPhoto | null { - if (this._user.photo?._ !== 'userProfilePhoto') return null + if (this.raw.photo?._ !== 'userProfilePhoto') return null if (!this._photo) { this._photo = new ChatPhoto( this.client, this.inputPeer, - this._user.photo + this.raw.photo ) } @@ -261,7 +261,7 @@ export class User { * This field is available only in case *isRestricted* is `true` */ get restrictions(): ReadonlyArray | null { - return this._user.restrictionReason ?? null + return this.raw.restrictionReason ?? null } /** @@ -334,7 +334,7 @@ export class User { * @param parseMode Parse mode to use when creating mention */ permanentMention(text?: string | null, parseMode?: string | null): string { - if (!this._user.accessHash) + if (!this.raw.accessHash) throw new MtCuteArgumentError( "user's access hash is not available!" ) @@ -352,7 +352,7 @@ export class User { length: text.length, url: `tg://user?id=${ this.id - }&hash=${this._user.accessHash.toString(16)}`, + }&hash=${this.raw.accessHash.toString(16)}`, }, ]) } diff --git a/packages/client/src/types/utils.ts b/packages/client/src/types/utils.ts index e1deb123..5996472d 100644 --- a/packages/client/src/types/utils.ts +++ b/packages/client/src/types/utils.ts @@ -34,8 +34,6 @@ function getAllGettersNames(obj: object): string[] { * > **Note**: This means that all getters must be pure! * > (getter that caches after its first invocation is also * > considered pure in this case) - * - * @internal */ export function makeInspectable( obj: Function, @@ -66,7 +64,7 @@ export function makeInspectable( } ret[it] = val } catch (e) { - ret[it] = e.message + ret[it] = "Error: " + e.message } }) return ret diff --git a/packages/dispatcher/src/filters.ts b/packages/dispatcher/src/filters.ts index 197b191a..f012eb63 100644 --- a/packages/dispatcher/src/filters.ts +++ b/packages/dispatcher/src/filters.ts @@ -306,6 +306,14 @@ export namespace filters { }> => (msg) => msg.chat.type === type + /** + * Filter messages by chat ID + */ + export const chatId = ( + id: number + ): UpdateFilter => (msg) => + msg.chat.id === id + /** * Filter incoming messages. *