From 886858d9de2d2541a2078cc0874bba02b9acd1cf Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Sat, 16 Dec 2023 19:10:59 +0300 Subject: [PATCH] refactor(client): isSelfPeer method --- packages/client/src/client.ts | 12 +++++++++- packages/client/src/methods/auth/_state.ts | 24 +++++++++++++++++++ .../src/methods/chats/reorder-usernames.ts | 4 ++-- .../src/methods/chats/set-chat-color.ts | 6 ++--- .../methods/chats/toggle-fragment-username.ts | 4 ++-- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index afc6fb0c..10adcedd 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -15,7 +15,7 @@ import { import { MemoryStorage } from '@mtcute/core/src/storage/memory.js' import { tdFileId } from '@mtcute/file-id' -import { AuthState, getAuthState } from './methods/auth/_state.js' +import { AuthState, getAuthState, isSelfPeer } from './methods/auth/_state.js' import { checkPassword } from './methods/auth/check-password.js' import { getPasswordHint } from './methods/auth/get-password-hint.js' import { logOut } from './methods/auth/log-out.js' @@ -508,6 +508,12 @@ export interface TelegramClient extends BaseTelegramClient { on(name: string, handler: (...args: any[]) => void): this getAuthState(): AuthState + /** + * Check if the given peer/input peer is referring to the current user + * **Available**: ✅ both users and bots + * + */ + isSelfPeer(peer: tl.TypeInputPeer | tl.TypePeer | tl.TypeInputUser): boolean /** * Check your Two-Step verification password and log in * @@ -5237,6 +5243,10 @@ TelegramClient.prototype.getAuthState = function (...args) { return getAuthState(this, ...args) } +TelegramClient.prototype.isSelfPeer = function (...args) { + return isSelfPeer(this, ...args) +} + TelegramClient.prototype.checkPassword = function (...args) { return checkPassword(this, ...args) } diff --git a/packages/client/src/methods/auth/_state.ts b/packages/client/src/methods/auth/_state.ts index 8ff07e53..5472aeda 100644 --- a/packages/client/src/methods/auth/_state.ts +++ b/packages/client/src/methods/auth/_state.ts @@ -98,3 +98,27 @@ export async function _onAuthorization( return new User(auth.user) } + +/** + * Check if the given peer/input peer is referring to the current user + */ +export function isSelfPeer( + client: BaseTelegramClient, + peer: tl.TypeInputPeer | tl.TypePeer | tl.TypeInputUser, +): boolean { + const state = getAuthState(client) + + switch (peer._) { + case 'inputPeerSelf': + case 'inputUserSelf': + return true + case 'inputPeerUser': + case 'inputPeerUserFromMessage': + case 'inputUser': + case 'inputUserFromMessage': + case 'peerUser': + return peer.userId === state.userId + default: + return false + } +} diff --git a/packages/client/src/methods/chats/reorder-usernames.ts b/packages/client/src/methods/chats/reorder-usernames.ts index 1d16c175..2c2ff167 100644 --- a/packages/client/src/methods/chats/reorder-usernames.ts +++ b/packages/client/src/methods/chats/reorder-usernames.ts @@ -2,7 +2,7 @@ import { BaseTelegramClient } from '@mtcute/core' import { InputPeerLike } from '../../types/index.js' import { assertTrue, isInputPeerChannel, isInputPeerUser, toInputChannel, toInputUser } from '../../utils/index.js' -import { getAuthState } from '../auth/_state.js' +import { isSelfPeer } from '../auth/_state.js' import { resolvePeer } from '../users/resolve-peer.js' /** @@ -20,7 +20,7 @@ export async function reorderUsernames( if (isInputPeerUser(peer)) { // either a bot or self - if (peer._ === 'inputPeerSelf' || peer.userId === getAuthState(client).userId) { + if (isSelfPeer(client, peer)) { // self const r = await client.call({ _: 'account.reorderUsernames', diff --git a/packages/client/src/methods/chats/set-chat-color.ts b/packages/client/src/methods/chats/set-chat-color.ts index c4c45973..e16cc050 100644 --- a/packages/client/src/methods/chats/set-chat-color.ts +++ b/packages/client/src/methods/chats/set-chat-color.ts @@ -2,7 +2,7 @@ import { BaseTelegramClient, MtTypeAssertionError, tl } from '@mtcute/core' import { InputPeerLike, MtInvalidPeerTypeError } from '../../types/index.js' import { assertTrue, isInputPeerChannel, isInputPeerUser, toInputChannel } from '../../utils/index.js' -import { getAuthState } from '../auth/_state.js' +import { isSelfPeer } from '../auth/_state.js' import { resolvePeer } from '../users/resolve-peer.js' // @available=user @@ -64,8 +64,8 @@ export async function setChatColor( } if (isInputPeerUser(peer)) { - if (peer._ !== 'inputPeerSelf' && peer.userId !== getAuthState(client).userId) { - throw new MtTypeAssertionError('setChatColor', 'inputPeerSelf | inputPeerUser', peer._) + if (!isSelfPeer(client, peer)) { + throw new MtTypeAssertionError('setChatColor', 'self', peer._) } const r = await client.call({ diff --git a/packages/client/src/methods/chats/toggle-fragment-username.ts b/packages/client/src/methods/chats/toggle-fragment-username.ts index 8fdf635b..934b2f1f 100644 --- a/packages/client/src/methods/chats/toggle-fragment-username.ts +++ b/packages/client/src/methods/chats/toggle-fragment-username.ts @@ -2,7 +2,7 @@ import { BaseTelegramClient } from '@mtcute/core' import { InputPeerLike } from '../../types/index.js' import { assertTrue, isInputPeerChannel, isInputPeerUser, toInputChannel, toInputUser } from '../../utils/index.js' -import { getAuthState } from '../auth/_state.js' +import { isSelfPeer } from '../auth/_state.js' import { resolvePeer } from '../users/resolve-peer.js' /** @@ -35,7 +35,7 @@ export async function toggleFragmentUsername( if (isInputPeerUser(peer)) { // either a bot or self - if (peer._ === 'inputPeerSelf' || peer.userId === getAuthState(client).userId) { + if (isSelfPeer(client, peer)) { // self const r = await client.call({ _: 'account.toggleUsername',