From b4d6361f1332e77e170af4e9ba43b8ef89cb2e04 Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Mon, 7 Apr 2025 04:56:01 +0300 Subject: [PATCH] fix(core): properly handle star gift owner when the server doesnt return peer --- .../core/src/highlevel/types/peers/peers-index.ts | 11 +++++++++++ .../highlevel/types/premium/stars-gift-unique.ts | 13 ++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/core/src/highlevel/types/peers/peers-index.ts b/packages/core/src/highlevel/types/peers/peers-index.ts index 8bf9e32f..559a1d73 100644 --- a/packages/core/src/highlevel/types/peers/peers-index.ts +++ b/packages/core/src/highlevel/types/peers/peers-index.ts @@ -74,4 +74,15 @@ export class PeersIndex { return this.chat(peer.channelId) } } + + has(peer: tl.TypePeer): boolean { + switch (peer._) { + case 'peerUser': + return this.users.has(peer.userId) + case 'peerChat': + return this.chats.has(peer.chatId) + case 'peerChannel': + return this.chats.has(peer.channelId) + } + } } diff --git a/packages/core/src/highlevel/types/premium/stars-gift-unique.ts b/packages/core/src/highlevel/types/premium/stars-gift-unique.ts index 876377df..6cd76085 100644 --- a/packages/core/src/highlevel/types/premium/stars-gift-unique.ts +++ b/packages/core/src/highlevel/types/premium/stars-gift-unique.ts @@ -4,6 +4,7 @@ import type { TextWithEntities } from '../misc/entities.js' import type { Peer } from '../peers/peer.js' import type { PeersIndex } from '../peers/peers-index.js' import { assert } from '@fuman/utils' +import { getMarkedPeerId } from '../../../utils/peer-utils.js' import { makeInspectable } from '../../utils/inspectable.js' import { memoizeGetters } from '../../utils/memoize.js' import { parseDocument } from '../media/document-utils.js' @@ -145,12 +146,22 @@ export class StarGiftUnique { return this.raw.slug } - /** ID of the peer who owns this gift, if available */ + /** + * ID of the peer who owns this gift, if available + * > Note: in some cases, only {@link ownerId} is available, and not {@link owner}, + * > in which cases please use `tg.getPeer(ownerId)` manually + */ get owner(): Peer | null { if (!this.raw.ownerId) return null + if (!this._peers.has(this.raw.ownerId)) return null return parsePeer(this.raw.ownerId, this._peers) } + /** ID of the peer who owns this gift, if available */ + get ownerId(): number | null { + return this.raw.ownerId ? getMarkedPeerId(this.raw.ownerId) : null + } + /** Name of the user who owns this gift, if available */ get ownerName(): string | null { return this.raw.ownerName ?? null