Various fixes

This commit is contained in:
Ilya Laktyushin
2025-05-13 13:41:41 +04:00
parent 0ae2f9b53a
commit 0b448bf68f
10 changed files with 243 additions and 67 deletions

View File

@@ -854,6 +854,7 @@ public enum TransferStarGiftError {
public enum BuyStarGiftError {
case generic
case priceChanged(Int64)
}
public enum UpdateStarGiftPriceError {
@@ -865,7 +866,7 @@ public enum UpgradeStarGiftError {
case generic
}
func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
let source: BotPaymentInvoiceSource = .starGiftResale(slug: slug, toPeerId: peerId)
return _internal_fetchBotPaymentForm(accountPeerId: account.peerId, postbox: account.postbox, network: account.network, source: source, themeParams: nil)
|> map(Optional.init)
@@ -874,6 +875,9 @@ func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id
}
|> mapToSignal { paymentForm in
if let paymentForm {
if let paymentPrice = paymentForm.invoice.prices.first?.amount, let price, paymentPrice > price {
return .fail(.priceChanged(paymentPrice))
}
return _internal_sendStarsPaymentForm(account: account, formId: paymentForm.id, source: source)
|> mapError { _ -> BuyStarGiftError in
return .generic
@@ -1473,26 +1477,53 @@ private final class ProfileGiftsContextImpl {
return _internal_transferStarGift(account: self.account, prepaid: prepaid, reference: reference, peerId: peerId)
}
func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
if let count = self.count {
self.count = max(0, count - 1)
func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
var listingPrice: Int64?
if let gift = self.gifts.first(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
}), case let .unique(uniqueGift) = gift.gift {
listingPrice = uniqueGift.resellStars
}
self.gifts.removeAll(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
})
self.filteredGifts.removeAll(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
})
self.pushState()
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
if listingPrice == nil {
if let gift = self.filteredGifts.first(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
}), case let .unique(uniqueGift) = gift.gift {
listingPrice = uniqueGift.resellStars
}
}
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price ?? listingPrice)
|> afterCompleted { [weak self] in
guard let self else {
return
}
self.queue.async {
if let count = self.count {
self.count = max(0, count - 1)
}
self.gifts.removeAll(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
})
self.filteredGifts.removeAll(where: { gift in
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
return true
}
return false
})
self.pushState()
}
}
}
func removeStarGift(gift: TelegramCore.StarGift) {
@@ -1975,11 +2006,11 @@ public final class ProfileGiftsContext {
}
}
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64? = nil) -> Signal<Never, BuyStarGiftError> {
return Signal { subscriber in
let disposable = MetaDisposable()
self.impl.with { impl in
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId).start(error: { error in
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId, price: price).start(error: { error in
subscriber.putError(error)
}, completed: {
subscriber.putCompletion()
@@ -2606,8 +2637,18 @@ private final class ResaleGiftsContextImpl {
self.loadMore()
}
func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
var listingPrice: Int64?
if let gift = self.gifts.first(where: { gift in
if case let .unique(uniqueGift) = gift, uniqueGift.slug == slug {
return true
}
return false
}), case let .unique(uniqueGift) = gift {
listingPrice = uniqueGift.resellStars
}
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price ?? listingPrice)
|> afterCompleted { [weak self] in
guard let self else {
return
@@ -2754,11 +2795,11 @@ public final class ResaleGiftsContext {
}
}
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64? = nil) -> Signal<Never, BuyStarGiftError> {
return Signal { subscriber in
let disposable = MetaDisposable()
self.impl.with { impl in
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId).start(error: { error in
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId, price: price).start(error: { error in
subscriber.putError(error)
}, completed: {
subscriber.putCompletion()

View File

@@ -125,8 +125,8 @@ public extension TelegramEngine {
return _internal_transferStarGift(account: self.account, prepaid: prepaid, reference: reference, peerId: peerId)
}
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price)
}
public func upgradeStarGift(formId: Int64?, reference: StarGiftReference, keepOriginalInfo: Bool) -> Signal<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError> {