2025-09-30 16:52:04 +02:00

77 lines
2.7 KiB
TypeScript

import type { ApiPeer, ApiSavedGifts, ApiStarGiftCollection } from '../../api/types';
import type { GlobalState, TabArgs } from '../types';
import { SERVICE_NOTIFICATIONS_USER_ID } from '../../config';
import { isUserId } from '../../util/entities/ids';
import { getCurrentTabId } from '../../util/establishMultitabRole';
import { isChatAdmin, isDeletedUser } from '../helpers';
import { selectChat, selectChatFullInfo } from './chats';
import { type ProfileCollectionKey } from './payments';
import { selectTabState } from './tabs';
import { selectBot, selectUser, selectUserFullInfo } from './users';
export function selectPeer<T extends GlobalState>(global: T, peerId: string): ApiPeer | undefined {
return selectUser(global, peerId) || selectChat(global, peerId);
}
export function selectPeerPhotos<T extends GlobalState>(global: T, peerId: string) {
return global.peers.profilePhotosById[peerId];
}
export function selectCanGift<T extends GlobalState>(global: T, peerId: string) {
const bot = selectBot(global, peerId);
const user = selectUser(global, peerId);
if (user) {
return !bot && peerId !== SERVICE_NOTIFICATIONS_USER_ID && !isDeletedUser(user);
}
return selectChatFullInfo(global, peerId)?.areStarGiftsAvailable;
}
export function selectPeerCollectionSavedGifts<T extends GlobalState>(
global: T,
peerId: string,
collectionId: ProfileCollectionKey,
...[tabId = getCurrentTabId()]: TabArgs<T>
): ApiSavedGifts | undefined {
const tabState = selectTabState(global, tabId);
return tabState.savedGifts.collectionsByPeerId[peerId]?.[collectionId];
}
export function selectPeerSavedGifts<T extends GlobalState>(
global: T,
peerId: string,
...[tabId = getCurrentTabId()]: TabArgs<T>
): ApiSavedGifts | undefined {
return selectPeerCollectionSavedGifts(global, peerId, 'all', tabId);
}
export function selectPeerStarGiftCollections<T extends GlobalState>(
global: T,
peerId: string,
): ApiStarGiftCollection[] | undefined {
return global.starGiftCollections?.byPeerId[peerId];
}
export function selectPeerPaidMessagesStars<T extends GlobalState>(
global: T,
peerId: string,
) {
const isChatWithUser = isUserId(peerId);
if (isChatWithUser) {
const userFullInfo = isChatWithUser ? selectUserFullInfo(global, peerId) : undefined;
return userFullInfo?.paidMessagesStars;
}
const chat = selectChat(global, peerId);
if (!chat) return undefined;
if (isChatAdmin(chat)) return undefined;
return chat.paidMessagesStars;
}
export function selectPeerHasProfileBackground<T extends GlobalState>(global: T, peerId: string) {
const peer = selectPeer(global, peerId);
return Boolean(peer?.profileColor || peer?.emojiStatus?.type === 'collectible');
}