import type { ApiUser, ApiUserCommonChats, ApiUserFullInfo, ApiUserStatus, } from '../../api/types'; import type { BotAppPermissions } from '../../types'; import type { GlobalState } from '../types'; import { isUserBot } from '../helpers'; export function selectUser(global: T, userId: string): ApiUser | undefined { return global.users.byId[userId]; } export function selectUserStatus(global: T, userId: string): ApiUserStatus | undefined { return global.users.statusesById[userId]; } export function selectUserFullInfo(global: T, userId: string): ApiUserFullInfo | undefined { return global.users.fullInfoById[userId]; } export function selectUserCommonChats( global: T, userId: string, ): ApiUserCommonChats | undefined { return global.users.commonChatsById[userId]; } export function selectIsUserBlocked(global: T, userId: string) { return selectUserFullInfo(global, userId)?.isBlocked; } export function selectIsUserChatProtected(global: T, userId: string) { const fullInfo = selectUserFullInfo(global, userId); if (!fullInfo) return undefined; return Boolean(fullInfo.noForwardsMyEnabled || fullInfo.noForwardsPeerEnabled); } export function selectIsCurrentUserPremium(global: T) { if (!global.currentUserId) return false; return Boolean(global.users.byId[global.currentUserId].isPremium); } export function selectIsCurrentUserFrozen(global: T) { return Boolean(global.appConfig.freezeUntilDate); } export function selectIsPremiumPurchaseBlocked(global: T) { return global.appConfig.isPremiumPurchaseBlocked ?? true; } export function selectIsGiveawayGiftsPurchaseAvailable(global: T) { return global.appConfig.isGiveawayGiftsPurchaseAvailable ?? true; } /** * Slow, not to be used in `withGlobal` */ export function selectUserByPhoneNumber(global: T, phoneNumber: string) { const phoneNumberCleaned = phoneNumber.replace(/[^0-9]/g, ''); return Object.values(global.users.byId).find((user) => user?.phoneNumber === phoneNumberCleaned); } export function selectBot(global: T, userId: string): ApiUser | undefined { const user = selectUser(global, userId); if (!user || !isUserBot(user)) { return undefined; } return user; } export function selectBotAppPermissions( global: T, userId: string, ): BotAppPermissions | undefined { return global.users.botAppPermissionsById[userId]; }