refactor(client): isSelfPeer method

This commit is contained in:
alina 🌸 2023-12-16 19:10:59 +03:00
parent 712c1e8348
commit 886858d9de
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 42 additions and 8 deletions

View file

@ -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)
}

View file

@ -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
}
}

View file

@ -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',

View file

@ -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({

View file

@ -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',