From e6e28399b0c0bbf28d9a7cbbbbf2e83a26d96e5a Mon Sep 17 00:00:00 2001 From: teidesu Date: Sun, 16 May 2021 14:32:36 +0300 Subject: [PATCH] refactor: prefer switches over ifs when possible --- packages/client/src/methods/auth/sign-in.ts | 2 +- .../src/methods/chats/create-channel.ts | 9 +- .../client/src/methods/chats/create-group.ts | 9 +- .../src/methods/chats/create-supergroup.ts | 9 +- .../src/methods/chats/get-nearby-chats.ts | 9 +- .../src/methods/chats/iter-chat-members.ts | 3 +- .../client/src/methods/chats/join-chat.ts | 18 +- .../chats/set-chat-default-permissions.ts | 9 +- .../src/methods/contacts/add-contact.ts | 8 +- .../src/methods/contacts/delete-contacts.ts | 8 +- .../client/src/methods/messages/close-poll.ts | 8 +- .../src/methods/messages/find-in-update.ts | 8 +- .../src/methods/messages/forward-messages.ts | 20 +- .../client/src/methods/messages/send-vote.ts | 8 +- packages/client/src/methods/updates.ts | 67 ++--- .../types/bots/input/input-inline-message.ts | 148 ++++++----- packages/client/src/types/bots/keyboards.ts | 57 ++--- .../client/src/types/media/document-utils.ts | 40 ++- packages/client/src/types/media/thumbnail.ts | 15 +- packages/client/src/types/messages/dialog.ts | 16 +- packages/client/src/types/messages/message.ts | 33 ++- .../client/src/types/peers/chat-member.ts | 128 +++++----- packages/client/src/types/peers/chat.ts | 141 ++++++----- packages/client/src/utils/updates-utils.ts | 11 + packages/core/src/utils/peer-utils.ts | 229 ++++++++++-------- packages/core/src/utils/tl-json.ts | 18 +- .../src/updates/chat-member-update.ts | 47 ++-- packages/file-id/src/serialize-unique.ts | 42 ++-- 28 files changed, 546 insertions(+), 574 deletions(-) diff --git a/packages/client/src/methods/auth/sign-in.ts b/packages/client/src/methods/auth/sign-in.ts index 7193c51b..68c44205 100644 --- a/packages/client/src/methods/auth/sign-in.ts +++ b/packages/client/src/methods/auth/sign-in.ts @@ -1,4 +1,4 @@ -import { MtCuteError, User, TermsOfService } from '../../types' +import { User, TermsOfService } from '../../types' import { TelegramClient } from '../../client' import { assertTypeIs } from '../../utils/type-assertion' import { normalizePhoneNumber } from '../../utils/misc-utils' diff --git a/packages/client/src/methods/chats/create-channel.ts b/packages/client/src/methods/chats/create-channel.ts index 9b75c1b1..890aac66 100644 --- a/packages/client/src/methods/chats/create-channel.ts +++ b/packages/client/src/methods/chats/create-channel.ts @@ -1,5 +1,6 @@ import { TelegramClient } from '../../client' import { Chat, MtCuteTypeAssertionError } from '../../types' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Create a new broadcast channel @@ -21,13 +22,7 @@ export async function createChannel( broadcast: true, }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'channels.createChannel', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('channels.createChannel', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/chats/create-group.ts b/packages/client/src/methods/chats/create-group.ts index 3e69423c..287d51ae 100644 --- a/packages/client/src/methods/chats/create-group.ts +++ b/packages/client/src/methods/chats/create-group.ts @@ -3,6 +3,7 @@ import { MaybeArray } from '@mtcute/core' import { Chat, InputPeerLike, MtCuteTypeAssertionError } from '../../types' import { normalizeToInputUser } from '../../utils/peer-utils' import { tl } from '@mtcute/tl' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Create a legacy group chat @@ -34,13 +35,7 @@ export async function createGroup( users: peers }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'messages.createChat', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('messages.createChat', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/chats/create-supergroup.ts b/packages/client/src/methods/chats/create-supergroup.ts index f166eab6..3ab88fa1 100644 --- a/packages/client/src/methods/chats/create-supergroup.ts +++ b/packages/client/src/methods/chats/create-supergroup.ts @@ -1,5 +1,6 @@ import { TelegramClient } from '../../client' import { Chat, MtCuteTypeAssertionError } from '../../types' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Create a new supergroup @@ -20,13 +21,7 @@ export async function createSupergroup( megagroup: true }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'channels.createChannel', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('channels.createChannel', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/chats/get-nearby-chats.ts b/packages/client/src/methods/chats/get-nearby-chats.ts index 2586ad1e..ecce0c12 100644 --- a/packages/client/src/methods/chats/get-nearby-chats.ts +++ b/packages/client/src/methods/chats/get-nearby-chats.ts @@ -3,6 +3,7 @@ import { Chat, MtCuteTypeAssertionError } from '../../types' import { assertTypeIs } from '../../utils/type-assertion' import { getMarkedPeerId } from '@mtcute/core' import { tl } from 'packages/tl' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Get nearby chats @@ -25,13 +26,7 @@ export async function getNearbyChats( }, }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'contacts.getLocated', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('contacts.getLocated', res) if (!res.updates.length) return [] diff --git a/packages/client/src/methods/chats/iter-chat-members.ts b/packages/client/src/methods/chats/iter-chat-members.ts index 0d08d22b..a80783c2 100644 --- a/packages/client/src/methods/chats/iter-chat-members.ts +++ b/packages/client/src/methods/chats/iter-chat-members.ts @@ -1,5 +1,6 @@ import { TelegramClient } from '../../client' import { ChatMember, InputPeerLike } from '../../types' +import { isInputPeerChat } from '../../utils/peer-utils' /** * Iterate through chat members @@ -45,7 +46,7 @@ export async function* iterChatMembers( if (!members.length) break - if (chat._ === 'inputPeerChat') { + if (isInputPeerChat(chat)) { total = members.length } diff --git a/packages/client/src/methods/chats/join-chat.ts b/packages/client/src/methods/chats/join-chat.ts index 20332edc..3912f47a 100644 --- a/packages/client/src/methods/chats/join-chat.ts +++ b/packages/client/src/methods/chats/join-chat.ts @@ -9,6 +9,7 @@ import { INVITE_LINK_REGEX, normalizeToInputChannel, } from '../../utils/peer-utils' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Join a channel or supergroup @@ -29,13 +30,7 @@ export async function joinChat( _: 'messages.importChatInvite', hash: m[1], }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'messages.importChatInvite', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('messages.importChatInvite', res) this._handleUpdate(res) @@ -50,13 +45,8 @@ export async function joinChat( _: 'channels.joinChannel', channel: peer, }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'channels.joinChannel', - 'updates | updatesCombined', - res._ - ) - } + + assertIsUpdatesGroup('channels.joinChannel', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/chats/set-chat-default-permissions.ts b/packages/client/src/methods/chats/set-chat-default-permissions.ts index 2a47c38a..8e5a0b84 100644 --- a/packages/client/src/methods/chats/set-chat-default-permissions.ts +++ b/packages/client/src/methods/chats/set-chat-default-permissions.ts @@ -1,6 +1,7 @@ import { TelegramClient } from '../../client' import { Chat, InputPeerLike, MtCuteTypeAssertionError } from '../../types' import { tl } from '@mtcute/tl' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Change default chat permissions for all members. @@ -32,13 +33,7 @@ export async function setChatDefaultPermissions( } }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) { - throw new MtCuteTypeAssertionError( - 'messages.editChatDefaultBannedRights', - 'updates | updatesCombined', - res._ - ) - } + assertIsUpdatesGroup('messages.editChatDefaultBannedRights', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/contacts/add-contact.ts b/packages/client/src/methods/contacts/add-contact.ts index 5e53f8e1..ccc32b5f 100644 --- a/packages/client/src/methods/contacts/add-contact.ts +++ b/packages/client/src/methods/contacts/add-contact.ts @@ -2,6 +2,7 @@ import { TelegramClient } from '../../client' import { InputPeerLike, MtCuteInvalidPeerTypeError, MtCuteTypeAssertionError, User } from '../../types' import { normalizeToInputUser } from '../../utils/peer-utils' import { tl } from '@mtcute/tl' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Add an existing Telegram user as a contact @@ -48,12 +49,7 @@ export async function addContact( addPhonePrivacyException: !!params.sharePhone }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - 'addContact', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('contacts.addContact', res) this._handleUpdate(res) diff --git a/packages/client/src/methods/contacts/delete-contacts.ts b/packages/client/src/methods/contacts/delete-contacts.ts index 44e69914..67ecea16 100644 --- a/packages/client/src/methods/contacts/delete-contacts.ts +++ b/packages/client/src/methods/contacts/delete-contacts.ts @@ -3,6 +3,7 @@ import { MaybeArray } from '@mtcute/core' import { InputPeerLike, MtCuteInvalidPeerTypeError, MtCuteTypeAssertionError, User } from '../../types' import { normalizeToInputUser } from '../../utils/peer-utils' import { tl } from '@mtcute/tl' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Delete a single contact from your Telegram contacts list @@ -56,12 +57,7 @@ export async function deleteContacts( id: inputPeers }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - 'addContact', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('contacts.deleteContacts', res) if (single && !res.updates.length) return null diff --git a/packages/client/src/methods/messages/close-poll.ts b/packages/client/src/methods/messages/close-poll.ts index dc6ea0f4..7b234957 100644 --- a/packages/client/src/methods/messages/close-poll.ts +++ b/packages/client/src/methods/messages/close-poll.ts @@ -3,6 +3,7 @@ import { InputPeerLike, MtCuteTypeAssertionError, Poll } from '../../types' import { createUsersChatsIndex } from '../../utils/peer-utils' import bigInt from 'big-integer' import { assertTypeIs } from '../../utils/type-assertion' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Close a poll sent by you. @@ -35,12 +36,7 @@ export async function closePoll( }, }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - 'messages.editMessage', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('messages.editMessage', res) this._handleUpdate(res, true) diff --git a/packages/client/src/methods/messages/find-in-update.ts b/packages/client/src/methods/messages/find-in-update.ts index 18f3b941..84bf389c 100644 --- a/packages/client/src/methods/messages/find-in-update.ts +++ b/packages/client/src/methods/messages/find-in-update.ts @@ -2,6 +2,7 @@ import { TelegramClient } from '../../client' import { tl } from '@mtcute/tl' import { Message, MtCuteTypeAssertionError } from '../../types' import { createUsersChatsIndex } from '../../utils/peer-utils' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** @internal */ export function _findMessageInUpdate( @@ -9,12 +10,7 @@ export function _findMessageInUpdate( res: tl.TypeUpdates, isEdit = false ): Message { - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - '_findMessageInUpdate', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('_findMessageInUpdate', res) this._handleUpdate(res, true) diff --git a/packages/client/src/methods/messages/forward-messages.ts b/packages/client/src/methods/messages/forward-messages.ts index 7cd0fbb8..0ebf12b7 100644 --- a/packages/client/src/methods/messages/forward-messages.ts +++ b/packages/client/src/methods/messages/forward-messages.ts @@ -12,6 +12,7 @@ import { createUsersChatsIndex, } from '../../utils/peer-utils' import { normalizeDate, randomUlong } from '../../utils/misc-utils' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Forward a single message. @@ -229,12 +230,7 @@ export async function forwardMessages( ), }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - 'messages.forwardMessages', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('messages.forwardMessages', res) this._handleUpdate(res, true) @@ -242,12 +238,12 @@ export async function forwardMessages( const forwarded: Message[] = [] res.updates.forEach((upd) => { - if ( - upd._ === 'updateNewMessage' || - upd._ == 'updateNewChannelMessage' || - upd._ === 'updateNewScheduledMessage' - ) { - forwarded.push(new Message(this, upd.message, users, chats)) + switch (upd._) { + case 'updateNewMessage': + case 'updateNewChannelMessage': + case 'updateNewScheduledMessage': + forwarded.push(new Message(this, upd.message, users, chats)) + break } }) diff --git a/packages/client/src/methods/messages/send-vote.ts b/packages/client/src/methods/messages/send-vote.ts index b2e06162..8b66d63b 100644 --- a/packages/client/src/methods/messages/send-vote.ts +++ b/packages/client/src/methods/messages/send-vote.ts @@ -8,6 +8,7 @@ import { import { MaybeArray } from '@mtcute/core' import { createUsersChatsIndex } from '../../utils/peer-utils' import { assertTypeIs } from '../../utils/type-assertion' +import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** * Send or retract a vote in a poll. @@ -56,12 +57,7 @@ export async function sendVote( options: options as Buffer[], }) - if (!(res._ === 'updates' || res._ === 'updatesCombined')) - throw new MtCuteTypeAssertionError( - 'messages.sendVote', - 'updates | updatesCombined', - res._ - ) + assertIsUpdatesGroup('messages.sendVote', res) this._handleUpdate(res, true) diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index c218ff88..b3a7d5f6 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -160,37 +160,37 @@ function _createNoDispatchIndex( pts: {}, } - if (updates._ === 'updates' || updates._ === 'updatesCombined') { - updates.updates.forEach((upd) => { - const cid = extractChannelIdFromUpdate(upd) ?? 0 - if ( - upd._ === 'updateNewMessage' || - upd._ === 'updateNewChannelMessage' - ) { - if (!ret.msg[cid]) ret.msg[cid] = {} - ret.msg[cid][upd.message.id] = true - } + switch (updates._) { + case 'updates': + case 'updatesCombined': + updates.updates.forEach((upd) => { + const cid = extractChannelIdFromUpdate(upd) ?? 0 + switch (upd._) { + case 'updateNewMessage': + case 'updateNewChannelMessage': + if (!ret.msg[cid]) ret.msg[cid] = {} + ret.msg[cid][upd.message.id] = true + break + } - const pts = 'pts' in upd ? upd.pts : undefined + const pts = 'pts' in upd ? upd.pts : undefined - if (pts) { - if (!ret.msg[cid]) ret.msg[cid] = {} - ret.msg[cid][pts] = true - } - }) - } + if (pts) { + if (!ret.msg[cid]) ret.msg[cid] = {} + ret.msg[cid][pts] = true + } + }) + break + case 'updateShortMessage': + case 'updateShortChatMessage': + case 'updateShortSentMessage': + // these updates are only used for non-channel messages, so we use 0 + if (!ret.msg[0]) ret.msg[0] = {} + if (!ret.pts[0]) ret.pts[0] = {} - if ( - updates._ === 'updateShortMessage' || - updates._ === 'updateShortChatMessage' || - updates._ === 'updateShortSentMessage' - ) { - // these updates are only used for non-channel messages, so we use 0 - if (!ret.msg[0]) ret.msg[0] = {} - if (!ret.pts[0]) ret.pts[0] = {} - - ret.msg[0][updates.id] = true - ret.pts[0][updates.pts] = true + ret.msg[0][updates.id] = true + ret.pts[0][updates.pts] = true + break } return ret @@ -208,11 +208,12 @@ async function _loadDifference( qts: 0, }) - if (diff._ === 'updates.differenceEmpty') return - - if (diff._ === 'updates.differenceTooLong') { - this._pts = diff.pts - return + switch (diff._) { + case 'updates.differenceEmpty': + return + case 'updates.differenceTooLong': + this._pts = diff.pts + return } const state = diff --git a/packages/client/src/types/bots/input/input-inline-message.ts b/packages/client/src/types/bots/input/input-inline-message.ts index c1d26ea4..1871fd2e 100644 --- a/packages/client/src/types/bots/input/input-inline-message.ts +++ b/packages/client/src/types/bots/input/input-inline-message.ts @@ -231,88 +231,80 @@ export namespace BotInlineMessage { obj: InputInlineMessage, parseMode?: string | null ): Promise { - if (obj.type === 'text') { - const [message, entities] = await client['_parseEntities']( - obj.text, - parseMode, - obj.entities - ) + switch (obj.type) { + case 'text': { + const [message, entities] = await client['_parseEntities']( + obj.text, + parseMode, + obj.entities + ) - return { - _: 'inputBotInlineMessageText', - message, - entities, - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + return { + _: 'inputBotInlineMessageText', + message, + entities, + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } } - } + case 'media': { + const [message, entities] = await client['_parseEntities']( + obj.text, + parseMode, + obj.entities + ) - if (obj.type === 'media') { - const [message, entities] = await client['_parseEntities']( - obj.text, - parseMode, - obj.entities - ) - - return { - _: 'inputBotInlineMessageMediaAuto', - message, - entities, - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + return { + _: 'inputBotInlineMessageMediaAuto', + message, + entities, + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } } + case 'geo': + case 'geo_live': + return { + _: 'inputBotInlineMessageMediaGeo', + geoPoint: { + _: 'inputGeoPoint', + lat: obj.latitude, + long: obj.longitude, + }, + // fields will be `undefined` if this is a `geo` + heading: (obj as InputMediaGeoLive).heading, + period: (obj as InputMediaGeoLive).period, + proximityNotificationRadius: (obj as InputMediaGeoLive) + .proximityNotificationRadius, + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } + case 'venue': + return { + _: 'inputBotInlineMessageMediaVenue', + geoPoint: { + _: 'inputGeoPoint', + lat: obj.latitude, + long: obj.longitude, + }, + title: obj.title, + address: obj.address, + provider: obj.source?.provider ?? '', + venueId: obj.source?.id ?? '', + venueType: obj.source?.type ?? '', + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } + case 'game': + return { + _: 'inputBotInlineMessageGame', + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } + case 'contact': + return { + _: 'inputBotInlineMessageMediaContact', + phoneNumber: obj.phone, + firstName: obj.firstName, + lastName: obj.lastName ?? '', + vcard: obj.vcard ?? '', + replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), + } } - - if (obj.type === 'geo' || obj.type === 'geo_live') { - return { - _: 'inputBotInlineMessageMediaGeo', - geoPoint: { - _: 'inputGeoPoint', - lat: obj.latitude, - long: obj.longitude, - }, - // fields will be `undefined` if this is a `geo` - heading: (obj as InputMediaGeoLive).heading, - period: (obj as InputMediaGeoLive).period, - proximityNotificationRadius: (obj as InputMediaGeoLive) - .proximityNotificationRadius, - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), - } - } - - if (obj.type === 'venue') { - return { - _: 'inputBotInlineMessageMediaVenue', - geoPoint: { - _: 'inputGeoPoint', - lat: obj.latitude, - long: obj.longitude, - }, - title: obj.title, - address: obj.address, - provider: obj.source?.provider ?? '', - venueId: obj.source?.id ?? '', - venueType: obj.source?.type ?? '', - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), - } - } - - if (obj.type === 'game') { - return { - _: 'inputBotInlineMessageGame', - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), - } - } - - if (obj.type === 'contact') { - return { - _: 'inputBotInlineMessageMediaContact', - phoneNumber: obj.phone, - firstName: obj.firstName, - lastName: obj.lastName ?? '', - vcard: obj.vcard ?? '', - replyMarkup: BotKeyboard._convertToTl(obj.replyMarkup), - } - } - - return obj as never } } diff --git a/packages/client/src/types/bots/keyboards.ts b/packages/client/src/types/bots/keyboards.ts index 1beb982d..b9ffa61e 100644 --- a/packages/client/src/types/bots/keyboards.ts +++ b/packages/client/src/types/bots/keyboards.ts @@ -332,38 +332,31 @@ export namespace BotKeyboard { ): tl.TypeReplyMarkup | undefined { if (!obj) return obj - if (obj.type === 'reply') { - return { - _: 'replyKeyboardMarkup', - resize: obj.resize, - singleUse: obj.singleUse, - selective: obj.selective, - rows: _2dToRows(obj.buttons), - } + switch (obj.type) { + case 'reply': + return { + _: 'replyKeyboardMarkup', + resize: obj.resize, + singleUse: obj.singleUse, + selective: obj.selective, + rows: _2dToRows(obj.buttons), + } + case 'reply_hide': + return { + _: 'replyKeyboardHide', + selective: obj.selective, + } + case 'force_reply': + return { + _: 'replyKeyboardForceReply', + singleUse: obj.singleUse, + selective: obj.selective, + } + case 'inline': + return { + _: 'replyInlineMarkup', + rows: _2dToRows(obj.buttons), + } } - - if (obj.type === 'reply_hide') { - return { - _: 'replyKeyboardHide', - selective: obj.selective, - } - } - - if (obj.type === 'force_reply') { - return { - _: 'replyKeyboardForceReply', - singleUse: obj.singleUse, - selective: obj.selective, - } - } - - if (obj.type === 'inline') { - return { - _: 'replyInlineMarkup', - rows: _2dToRows(obj.buttons), - } - } - - return undefined } } diff --git a/packages/client/src/types/media/document-utils.ts b/packages/client/src/types/media/document-utils.ts index d3366773..47a630b9 100644 --- a/packages/client/src/types/media/document-utils.ts +++ b/packages/client/src/types/media/document-utils.ts @@ -12,28 +12,26 @@ export function parseDocument( doc: tl.RawDocument ): RawDocument { for (const attr of doc.attributes) { - if (attr._ === 'documentAttributeAudio') { - if (attr.voice) { - return new Voice(client, doc, attr) - } else { - return new Audio(client, doc, attr) + switch (attr._) { + case 'documentAttributeAudio': + if (attr.voice) { + return new Voice(client, doc, attr) + } else { + return new Audio(client, doc, attr) + } + case 'documentAttributeSticker': { + const sz = doc.attributes.find( + (it) => it._ === 'documentAttributeImageSize' + )! as tl.RawDocumentAttributeImageSize + return new Sticker(client, doc, attr, sz) } - } - - if (attr._ === 'documentAttributeSticker') { - const sz = doc.attributes.find( - (it) => it._ === 'documentAttributeImageSize' - )! as tl.RawDocumentAttributeImageSize - return new Sticker(client, doc, attr, sz) - } - - if ( - attr._ === 'documentAttributeVideo' || - // legacy gif - (attr._ === 'documentAttributeImageSize' && - doc.mimeType === 'image/gif') - ) { - return new Video(client, doc, attr) + case 'documentAttributeVideo': + return new Video(client, doc, attr) + case 'documentAttributeImageSize': + // legacy gif + if (doc.mimeType === 'image/gif') { + return new Video(client, doc, attr) + } } } diff --git a/packages/client/src/types/media/thumbnail.ts b/packages/client/src/types/media/thumbnail.ts index 5598ec80..68d9dbe4 100644 --- a/packages/client/src/types/media/thumbnail.ts +++ b/packages/client/src/types/media/thumbnail.ts @@ -62,12 +62,15 @@ export class Thumbnail extends FileLocation { media: tl.RawPhoto | tl.RawDocument, sz: tl.TypePhotoSize ) { - if (sz._ === 'photoSizeEmpty' || sz._ === 'photoCachedSize') - throw new MtCuteTypeAssertionError( - 'sz', - 'not (photoSizeEmpty | photoCachedSize)', - sz._ - ) + switch (sz._) { + case 'photoSizeEmpty': + case 'photoCachedSize': + throw new MtCuteTypeAssertionError( + 'sz', + 'not (photoSizeEmpty | photoCachedSize)', + sz._ + ) + } let location: | tl.TypeInputFileLocation diff --git a/packages/client/src/types/messages/dialog.ts b/packages/client/src/types/messages/dialog.ts index 9159931c..72a41470 100644 --- a/packages/client/src/types/messages/dialog.ts +++ b/packages/client/src/types/messages/dialog.ts @@ -170,12 +170,16 @@ export class Dialog { const peer = this.raw.peer let chat - if (peer._ === 'peerChannel' || peer._ === 'peerChat') { - chat = this._chats[ - peer._ === 'peerChannel' ? peer.channelId : peer.chatId - ] - } else { - chat = this._users[peer.userId] + switch (peer._) { + case 'peerChannel': + case 'peerChat': + chat = this._chats[ + peer._ === 'peerChannel' ? peer.channelId : peer.chatId + ] + break + default: + chat = this._users[peer.userId] + break } this._chat = new Chat(this.client, chat) diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 2f65f14e..2c73e7d3 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -184,20 +184,27 @@ export class Message { if (!from) { // anon admin, return the chat this._sender = this.chat - } else if (from._ === 'peerChannel') { - // forwarded channel post - this._sender = new Chat( - this.client, - this._chats[from.channelId] - ) - } else if (from._ === 'peerUser') { - this._sender = new User(this.client, this._users[from.userId]) } else - throw new MtCuteTypeAssertionError( - 'raw.fromId', - 'peerUser | peerChannel', - from._ - ) + switch (from._) { + case 'peerChannel': // forwarded channel post + this._sender = new Chat( + this.client, + this._chats[from.channelId] + ) + break + case 'peerUser': + this._sender = new User( + this.client, + this._users[from.userId] + ) + break + default: + throw new MtCuteTypeAssertionError( + 'raw.fromId', + 'peerUser | peerChannel', + from._ + ) + } } return this._sender diff --git a/packages/client/src/types/peers/chat-member.ts b/packages/client/src/types/peers/chat-member.ts index 28318216..93c08906 100644 --- a/packages/client/src/types/peers/chat-member.ts +++ b/packages/client/src/types/peers/chat-member.ts @@ -38,7 +38,7 @@ export class ChatMember { constructor( client: TelegramClient, raw: tl.TypeChatParticipant | tl.TypeChannelParticipant, - users: UsersIndex, + users: UsersIndex ) { this.client = client this.raw = raw @@ -51,24 +51,25 @@ export class ChatMember { */ get user(): User { if (this._user === undefined) { - if ( - this.raw._ === 'channelParticipantBanned' || - this.raw._ === 'channelParticipantLeft' - ) { - assertTypeIs( - 'ChatMember#user (raw.peer)', - this.raw.peer, - 'peerUser' - ) - this._user = new User( - this.client, - this._users[this.raw.peer.userId] - ) - } else { - this._user = new User( - this.client, - this._users[this.raw.userId] - ) + switch (this.raw._) { + case 'channelParticipantBanned': + case 'channelParticipantLeft': + assertTypeIs( + 'ChatMember#user (raw.peer)', + this.raw.peer, + 'peerUser' + ) + this._user = new User( + this.client, + this._users[this.raw.peer.userId] + ) + break + default: + this._user = new User( + this.client, + this._users[this.raw.userId] + ) + break } } @@ -79,34 +80,23 @@ export class ChatMember { * Get the chat member status */ get status(): ChatMember.Status { - if ( - this.raw._ === 'channelParticipant' || - this.raw._ === 'channelParticipantSelf' || - this.raw._ === 'chatParticipant' - ) { - return 'member' - } - - if ( - this.raw._ === 'channelParticipantCreator' || - this.raw._ === 'chatParticipantCreator' - ) { - return 'creator' - } - - if ( - this.raw._ === 'channelParticipantAdmin' || - this.raw._ === 'chatParticipantAdmin' - ) { - return 'admin' - } - - if (this.raw._ === 'channelParticipantLeft') { - return 'left' - } - - if (this.raw._ === 'channelParticipantBanned') { - return this.raw.bannedRights.viewMessages ? 'banned' : 'restricted' + switch (this.raw._) { + case 'channelParticipant': + case 'channelParticipantSelf': + case 'chatParticipant': + return 'member' + case 'channelParticipantCreator': + case 'chatParticipantCreator': + return 'creator' + case 'channelParticipantAdmin': + case 'chatParticipantAdmin': + return 'admin' + case 'channelParticipantLeft': + return 'left' + case 'channelParticipantBanned': + return this.raw.bannedRights.viewMessages + ? 'banned' + : 'restricted' } // fallback @@ -119,10 +109,13 @@ export class ChatMember { * `null` for non-admins and in case custom title is not set. */ get title(): string | null { - return this.raw._ === 'channelParticipantCreator' || - this.raw._ === 'channelParticipantAdmin' - ? this.raw.rank ?? null - : null + switch (this.raw._) { + case 'channelParticipantCreator': + case 'channelParticipantAdmin': + return this.raw.rank ?? null + default: + return null + } } /** @@ -131,11 +124,14 @@ export class ChatMember { * Not available for creators and left members */ get joinedDate(): Date | null { - return this.raw._ === 'channelParticipantCreator' || - this.raw._ === 'chatParticipantCreator' || - this.raw._ === 'channelParticipantLeft' - ? null - : new Date(this.raw.date * 1000) + switch (this.raw._) { + case 'channelParticipantCreator': + case 'chatParticipantCreator': + case 'channelParticipantLeft': + return null + default: + return new Date(this.raw.date * 1000) + } } private _invitedBy?: User | null @@ -149,14 +145,7 @@ export class ChatMember { */ get invitedBy(): User | null { if (this._invitedBy === undefined) { - if ( - this.raw._ !== 'chatParticipantCreator' && - this.raw._ !== 'channelParticipantCreator' && - this.raw._ !== 'channelParticipant' && - this.raw._ !== 'channelParticipantBanned' && - this.raw._ !== 'channelParticipantLeft' && - this.raw.inviterId - ) { + if ('inviterId' in this.raw && this.raw.inviterId) { this._invitedBy = new User( this.client, this._users[this.raw.inviterId] @@ -244,10 +233,13 @@ export class ChatMember { * Also contains whether this admin is anonymous. */ get permissions(): tl.RawChatAdminRights | null { - return this.raw._ === 'channelParticipantAdmin' || - this.raw._ === 'channelParticipantCreator' - ? this.raw.adminRights - : null + switch (this.raw._) { + case 'channelParticipantAdmin': + case 'channelParticipantCreator': + return this.raw.adminRights + default: + return null + } } } diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index f83a7934..15bb2e72 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -56,20 +56,20 @@ export class Chat { ) { if (!peer) throw new MtCuteArgumentError('peer is not available') - if ( - !( - peer._ === 'user' || - peer._ === 'chat' || - peer._ === 'channel' || - peer._ === 'chatForbidden' || - peer._ === 'channelForbidden' - ) - ) - throw new MtCuteTypeAssertionError( - 'peer', - 'user | chat | channel', - peer._ - ) + switch (peer._) { + case 'user': + case 'chat': + case 'channel': + case 'chatForbidden': + case 'channelForbidden': + break + default: + throw new MtCuteTypeAssertionError( + 'peer', + 'user | chat | channel', + peer._ + ) + } this.client = client this.peer = peer @@ -87,41 +87,41 @@ export class Chat { */ get inputPeer(): tl.TypeInputPeer { if (!this._inputPeer) { - if (this.peer._ === 'user') { - if (!this.peer.accessHash) { - throw new MtCuteArgumentError( - "Peer's access hash is not available!" - ) - } + switch (this.peer._) { + case 'user': + if (!this.peer.accessHash) { + throw new MtCuteArgumentError( + "Peer's access hash is not available!" + ) + } - this._inputPeer = { - _: 'inputPeerUser', - userId: this.peer.id, - accessHash: this.peer.accessHash, - } - } else if ( - this.peer._ === 'chat' || - this.peer._ === 'chatForbidden' - ) { - this._inputPeer = { - _: 'inputPeerChat', - chatId: this.peer.id, - } - } else if ( - this.peer._ === 'channel' || - this.peer._ === 'channelForbidden' - ) { - if (!this.peer.accessHash) { - throw new MtCuteArgumentError( - "Peer's access hash is not available!" - ) - } + this._inputPeer = { + _: 'inputPeerUser', + userId: this.peer.id, + accessHash: this.peer.accessHash, + } + break + case 'chat': + case 'chatForbidden': + this._inputPeer = { + _: 'inputPeerChat', + chatId: this.peer.id, + } + break + case 'channel': + case 'channelForbidden': + if (!this.peer.accessHash) { + throw new MtCuteArgumentError( + "Peer's access hash is not available!" + ) + } - this._inputPeer = { - _: 'inputPeerChannel', - channelId: this.peer.id, - accessHash: this.peer.accessHash, - } + this._inputPeer = { + _: 'inputPeerChannel', + channelId: this.peer.id, + accessHash: this.peer.accessHash, + } + break } } @@ -132,23 +132,23 @@ export class Chat { /** Type of chat */ get type(): Chat.Type { if (!this._type) { - if (this.peer._ === 'user') { - this._type = this.peer.bot ? 'bot' : 'private' - } else if ( - this.peer._ === 'chat' || - this.peer._ === 'chatForbidden' - ) { - this._type = 'group' - } else if ( - this.peer._ === 'channel' || - this.peer._ === 'channelForbidden' - ) { - this._type = - this.peer._ === 'channel' && this.peer.gigagroup - ? 'gigagroup' - : this.peer.broadcast - ? 'channel' - : 'supergroup' + switch (this.peer._) { + case 'user': + this._type = this.peer.bot ? 'bot' : 'private' + break + case 'chat': + case 'chatForbidden': + this._type = 'group' + break + case 'channel': + case 'channelForbidden': + this._type = + this.peer._ === 'channel' && this.peer.gigagroup + ? 'gigagroup' + : this.peer.broadcast + ? 'channel' + : 'supergroup' + break } } @@ -467,12 +467,11 @@ export class Chat { users: UsersIndex, chats: ChatsIndex ): Chat { - if (peer._ === 'peerUser') { - return new Chat(client, users[peer.userId]) - } - - if (peer._ === 'peerChat') { - return new Chat(client, chats[peer.chatId]) + switch (peer._) { + case 'peerUser': + return new Chat(client, users[peer.userId]) + case 'peerChat': + return new Chat(client, chats[peer.chatId]) } return new Chat(client, chats[peer.channelId]) @@ -508,8 +507,6 @@ export class Chat { } } - // todo: bound methods https://github.com/pyrogram/pyrogram/blob/a86656aefcc93cc3d2f5c98227d5da28fcddb136/pyrogram/types/user_and_chats/chat.py#L319 - /** * Join this chat. */ diff --git a/packages/client/src/utils/updates-utils.ts b/packages/client/src/utils/updates-utils.ts index c437a6ea..4a1c664a 100644 --- a/packages/client/src/utils/updates-utils.ts +++ b/packages/client/src/utils/updates-utils.ts @@ -1,4 +1,5 @@ import { tl } from '@mtcute/tl' +import { MtCuteTypeAssertionError } from '../types' // dummy updates which are used for methods that return messages.affectedHistory. // that is not an update, but it carries info about pts, and we need to handle it @@ -34,3 +35,13 @@ export function isDummyUpdate(upd: tl.TypeUpdate): boolean { export function isDummyUpdates(upd: tl.TypeUpdates): boolean { return upd._ === 'updates' && upd.updates.length === 1 && isDummyUpdate(upd.updates[0]) } + +/** @internal */ +export function assertIsUpdatesGroup(ctx: string, upd: tl.TypeUpdates): asserts upd is tl.RawUpdates | tl.RawUpdatesCombined { + switch (upd._) { + case 'updates': + case 'updatesCombined': + return + } + throw new MtCuteTypeAssertionError(ctx, 'updates | updatesCombined', upd._) +} diff --git a/packages/core/src/utils/peer-utils.ts b/packages/core/src/utils/peer-utils.ts index 96af196b..ab1f5018 100644 --- a/packages/core/src/utils/peer-utils.ts +++ b/packages/core/src/utils/peer-utils.ts @@ -10,11 +10,14 @@ export const MAX_USER_ID = 2147483647 * Get the bare (non-marked) ID from a {@link tl.TypePeer} */ export function getBarePeerId(peer: tl.TypePeer): number { - if (peer._ === 'peerUser') return peer.userId - if (peer._ === 'peerChat') return peer.chatId - if (peer._ === 'peerChannel') return peer.channelId - - throw new Error('Invalid peer') + switch (peer._) { + case 'peerUser': + return peer.userId + case 'peerChat': + return peer.chatId + case 'peerChannel': + return peer.channelId + } } /** @@ -35,17 +38,31 @@ export function getMarkedPeerId( peerType?: BasicPeerType | PeerType ): number { if (typeof peer === 'number') { - if (peerType === 'user' || peerType === 'bot') return peer - if (peerType === 'chat' || peerType === 'group') return -peer - if (peerType === 'channel' || peerType === 'supergroup') - return MAX_CHANNEL_ID - peer + switch (peerType) { + case 'user': + case 'bot': + return peer + case 'chat': + case 'group': + return -peer + case 'channel': + case 'supergroup': + return MAX_CHANNEL_ID - peer + } throw new Error('Invalid peer type') } - if (peer._ === 'peerUser' || peer._ === 'inputPeerUser') return peer.userId - if (peer._ === 'peerChat' || peer._ === 'inputPeerChat') return -peer.chatId - if (peer._ === 'peerChannel' || peer._ === 'inputPeerChannel') - return MAX_CHANNEL_ID - peer.channelId + switch (peer._) { + case 'peerUser': + case 'inputPeerUser': + return peer.userId + case 'peerChat': + case 'inputPeerChat': + return -peer.chatId + case 'peerChannel': + case 'inputPeerChannel': + return MAX_CHANNEL_ID - peer.channelId + } throw new Error('Invalid peer') } @@ -55,11 +72,14 @@ export function getMarkedPeerId( */ export function getBasicPeerType(peer: tl.TypePeer | number): BasicPeerType { if (typeof peer !== 'number') { - if (peer._ === 'peerUser') return 'user' - if (peer._ === 'peerChat') return 'chat' - if (peer._ === 'peerChannel') return 'channel' - - throw new Error('Invalid peer') + switch (peer._) { + case 'peerUser': + return 'user' + case 'peerChat': + return 'chat' + case 'peerChannel': + return 'channel' + } } if (peer < 0) { @@ -90,11 +110,16 @@ export function markedPeerIdToBare(peerId: number): number { * Convert {@link PeerType} to {@link BasicPeerType} */ export function peerTypeToBasic(type: PeerType): BasicPeerType { - if (type === 'bot' || type === 'user') return 'user' - if (type === 'group') return 'chat' - if (type === 'channel' || type === 'supergroup') return 'channel' - - throw new Error('Invalid peer type') + switch (type) { + case 'bot': + case 'user': + return 'user' + case 'group': + return 'chat' + case 'channel': + case 'supergroup': + return 'channel' + } } function comparePeers( @@ -105,22 +130,28 @@ function comparePeers( if ('userId' in first) { if ('userId' in second) return first.userId === second.userId - if (second._ === 'user' || second._ === 'userEmpty') - return first.userId === second.id + switch (second._) { + case 'user': + case 'userEmpty': + return first.userId === second.id + } } if ('chatId' in first) { if ('chatId' in second) return first.chatId === second.chatId - if ( - second._ === 'chat' || - second._ === 'chatForbidden' || - second._ === 'chatEmpty' - ) - return first.chatId === second.id + switch (second._) { + case 'chat': + case 'chatForbidden': + case 'chatEmpty': + return first.chatId === second.id + } } if ('channelId' in first) { if ('channelId' in second) return first.channelId === second.channelId - if (second._ === 'channel' || second._ === 'channelForbidden') - return first.channelId === second.id + switch (second._) { + case 'channel': + case 'channelForbidden': + return first.channelId === second.id + } } return false } @@ -141,31 +172,32 @@ function isRefMessage(msg: tl.TypeMessage, peer: any): boolean | undefined { function findContext(obj: any, peer: any): [number, number] | undefined { if (!peer.min) return undefined - if ( - obj._ === 'updates' || - obj._ === 'updatesCombined' || - obj._ === 'updates.difference' || - obj._ === 'updates.differenceSlice' - ) { - for (const upd of (obj.updates || obj.otherUpdates) as tl.TypeUpdate[]) { - if ( - (upd._ === 'updateNewMessage' || - upd._ === 'updateNewChannelMessage' || - upd._ === 'updateEditMessage' || - upd._ === 'updateEditChannelMessage') && - isRefMessage(upd.message, peer) - ) { - return [getMarkedPeerId(upd.message.peerId!), upd.message.id] + switch (obj._) { + case 'updates': + case 'updatesCombined': + case 'updates.difference': + case 'updates.differenceSlice': + for (const upd of (obj.updates || + obj.otherUpdates) as tl.TypeUpdate[]) { + switch (upd._) { + case 'updateNewMessage': + case 'updateNewChannelMessage': + case 'updateEditMessage': + case 'updateEditChannelMessage': + if (isRefMessage(upd.message, peer)) { + return [ + getMarkedPeerId(upd.message.peerId!), + upd.message.id, + ] + } + break + } } - } - } - - if (obj._ === 'updateShortMessage') { - return [obj.userId, obj.id] - } - - if (obj._ === 'updateShortChatMessage') { - return [-obj.chatId, obj.id] + break + case 'updateShortMessage': + return [obj.userId, obj.id] + case 'updateShortChatMessage': + return [-obj.chatId, obj.id] } if ('messages' in obj || 'newMessages' in obj) { @@ -193,19 +225,17 @@ export function* getAllPeersFrom( > { if (typeof obj !== 'object') return - if ( - obj._ === 'user' || - obj._ === 'chat' || - obj._ === 'channel' || - obj._ === 'chatForbidden' || - obj._ === 'channelForbidden' - ) { - yield obj - return - } - if (obj._ === 'userFull') { - yield obj.user - return + switch (obj._) { + case 'user': + case 'chat': + case 'channel': + case 'chatForbidden': + case 'channelForbidden': + yield obj + return + case 'userFull': + yield obj.user + return } if ( @@ -216,26 +246,26 @@ export function* getAllPeersFrom( yield obj.user } - if ( - 'chat' in obj && - typeof obj.chat === 'object' && - (obj.chat._ === 'chat' || - obj.chat._ === 'channel' || - obj.chat._ === 'chatForbidden' || - obj.chat._ === 'channelForbidden') - ) { - yield obj.chat + if ('chat' in obj && typeof obj.chat === 'object') { + switch (obj.chat._) { + case 'chat': + case 'channel': + case 'chatForbidden': + case 'channelForbidden': + yield obj.chat + break + } } - if ( - 'channel' in obj && - typeof obj.channel === 'object' && - (obj.channel._ === 'chat' || - obj.channel._ === 'channel' || - obj.channel._ === 'chatForbidden' || - obj.channel._ === 'channelForbidden') - ) { - yield obj.channel + if ('channel' in obj && typeof obj.channel === 'object') { + switch (obj.channel._) { + case 'chat': + case 'channel': + case 'chatForbidden': + case 'channelForbidden': + yield obj.channel + break + } } if ('users' in obj && Array.isArray(obj.users) && obj.users.length) { @@ -257,18 +287,19 @@ export function* getAllPeersFrom( if ('chats' in obj && Array.isArray(obj.chats) && obj.chats.length) { for (const chat of obj.chats) { // .chats is sometimes number[] - if ( - typeof chat === 'object' && - (chat._ === 'chat' || - chat._ === 'channel' || - chat._ === 'chatForbidden' || - chat._ === 'channelForbidden') - ) { - if (chat.min) { - chat.fromMessage = findContext(obj, chat) - } + if (typeof chat === 'object') { + switch (chat._) { + case 'chat': + case 'channel': + case 'chatForbidden': + case 'channelForbidden': + if (chat.min) { + chat.fromMessage = findContext(obj, chat) + } - yield chat + yield chat + break + } } } } diff --git a/packages/core/src/utils/tl-json.ts b/packages/core/src/utils/tl-json.ts index 0c4efdd7..0ccb333d 100644 --- a/packages/core/src/utils/tl-json.ts +++ b/packages/core/src/utils/tl-json.ts @@ -38,14 +38,16 @@ export function jsonToTlJson(obj: any): tl.TypeJSONValue { * @param obj TL JSON object to convert */ export function tlJsonToJson(obj: tl.TypeJSONValue): any { - if (obj._ === 'jsonNull') return null - if ( - obj._ === 'jsonBool' || - obj._ === 'jsonNumber' || - obj._ === 'jsonString' - ) - return obj.value - if (obj._ === 'jsonArray') return obj.value.map(tlJsonToJson) + switch (obj._) { + case 'jsonNull': + return null + case 'jsonBool': + case 'jsonNumber': + case 'jsonString': + return obj.value + case 'jsonArray': + return obj.value.map(tlJsonToJson) + } const ret: any = {} diff --git a/packages/dispatcher/src/updates/chat-member-update.ts b/packages/dispatcher/src/updates/chat-member-update.ts index b103e5fa..9d3b5535 100644 --- a/packages/dispatcher/src/updates/chat-member-update.ts +++ b/packages/dispatcher/src/updates/chat-member-update.ts @@ -127,20 +127,29 @@ export class ChatMemberUpdate { // in this case OR is the same as AND, but AND doesn't work well with typescript :shrug: if (!old || !cur) return (this._type = 'other') - if (old._ === 'chatParticipant' || old._ === 'channelParticipant') { - if ( - cur._ === 'chatParticipantAdmin' || - cur._ === 'channelParticipantAdmin' - ) { - return (this._type = 'promoted') - } + switch (old._) { + case 'chatParticipant': + case 'channelParticipant': + switch (cur._) { + case 'chatParticipantAdmin': + case 'channelParticipantAdmin': + return (this._type = 'promoted') + case 'channelParticipantBanned': + // kicked or restricted + if (cur.left) return (this._type = 'kicked') - if (cur._ === 'channelParticipantBanned') { - // kicked or restricted - if (cur.left) return (this._type = 'kicked') + return (this._type = 'restricted') + } + break + case 'chatParticipantCreator': + case 'channelParticipantCreator': + return (this._type = 'old_owner') + } - return (this._type = 'restricted') - } + switch (cur._) { + case 'chatParticipantCreator': + case 'channelParticipantCreator': + return (this._type = 'new_owner') } if ( @@ -157,20 +166,6 @@ export class ChatMemberUpdate { return (this._type = 'demoted') } - if ( - old._ === 'chatParticipantCreator' || - old._ === 'channelParticipantCreator' - ) { - return (this._type = 'old_owner') - } - - if ( - cur._ === 'chatParticipantCreator' || - cur._ === 'channelParticipantCreator' - ) { - return (this._type = 'new_owner') - } - return (this._type = 'other') } diff --git a/packages/file-id/src/serialize-unique.ts b/packages/file-id/src/serialize-unique.ts index 03b38585..9c503a04 100644 --- a/packages/file-id/src/serialize-unique.ts +++ b/packages/file-id/src/serialize-unique.ts @@ -75,25 +75,29 @@ export function toUniqueFileId( } let writer: BinaryWriter - if (inputLocation._ === 'photo') { - writer = BinaryWriter.alloc(16) - writer.int32(type) - writer.long(inputLocation.volumeId) - writer.int32(inputLocation.localId) - } else if (inputLocation._ === 'web') { - writer = BinaryWriter.alloc( - Buffer.byteLength(inputLocation.url, 'utf-8') + 8 - ) - writer.int32(type) - writer.string(inputLocation.url) - } else if (inputLocation._ === 'common') { - writer = BinaryWriter.alloc(12) - writer.int32(type) - writer.long(inputLocation.id) - } else { - throw new td.UnsupportedError( - `Unique IDs are not supported for ${(inputLocation as any)._}` - ) + switch (inputLocation._) { + case 'photo': + writer = BinaryWriter.alloc(16) + writer.int32(type) + writer.long(inputLocation.volumeId) + writer.int32(inputLocation.localId) + break + case 'web': + writer = BinaryWriter.alloc( + Buffer.byteLength(inputLocation.url, 'utf-8') + 8 + ) + writer.int32(type) + writer.string(inputLocation.url) + break + case 'common': + writer = BinaryWriter.alloc(12) + writer.int32(type) + writer.long(inputLocation.id) + break + default: + throw new td.UnsupportedError( + `Unique IDs are not supported for ${(inputLocation as any)._}` + ) } return encodeUrlSafeBase64(telegramRleEncode(writer.result()))