Skip to content

Commit e39a3db

Browse files
laktyushinIsaac
authored andcommitted
Various fixes
(cherry picked from commit 0b448bf)
1 parent 25a3ae7 commit e39a3db

File tree

10 files changed

+239
-63
lines changed

10 files changed

+239
-63
lines changed

Telegram/Telegram-iOS/en.lproj/Localizable.strings

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14320,3 +14320,16 @@ Sorry for the inconvenience.";
1432014320

1432114321
"Story.Editor.TooltipSelection_1" = "Tap here to view your %@ story";
1432214322
"Story.Editor.TooltipSelection_any" = "Tap here to view your %@ stories";
14323+
14324+
"Gift.Buy.ErrorUnknown" = "An error occurred. Please try again.";
14325+
"Gift.Buy.ErrorPriceChanged.Title" = "Warning";
14326+
"Gift.Buy.ErrorPriceChanged.Text" = "The price has increased from **%1$@** to **%2$@**. Buy the gift anyway?";
14327+
"Gift.Buy.ErrorPriceChanged.Text.Stars_1" = "%@ Star";
14328+
"Gift.Buy.ErrorPriceChanged.Text.Stars_any" = "%@ Stars";
14329+
14330+
"ChatbotSetup.Gift.Warning.Title" = "Warning";
14331+
"ChatbotSetup.Gift.Warning.CombinedText" = "The bot **%@** will be able to **manage your gifts and stars**, including giving them away to other users.";
14332+
"ChatbotSetup.Gift.Warning.GiftsText" = "The bot **%@** will be able to **manage your gifts**, including giving them away to other users.";
14333+
"ChatbotSetup.Gift.Warning.StarsText" = "The bot **%@** will be able to **transfer your stars**.";
14334+
"ChatbotSetup.Gift.Warning.UsernameText" = "The bot **%@** will be able to **edit your username**, which will make your current username available for others to claim.";
14335+
"ChatbotSetup.Gift.Warning.Proceed" = "Proceed";

submodules/ContactListUI/Sources/ContactListNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ public final class ContactListNode: ASDisplayNode {
18411841
}
18421842

18431843
var isEmpty = false
1844-
if (authorizationStatus == .notDetermined || authorizationStatus == .denied) && peers.isEmpty {
1844+
if (authorizationStatus == .notDetermined || authorizationStatus == .denied) && peers.isEmpty && topPeers.isEmpty {
18451845
isEmpty = true
18461846
}
18471847

submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGifts.swift

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ public enum TransferStarGiftError {
854854

855855
public enum BuyStarGiftError {
856856
case generic
857+
case priceChanged(Int64)
857858
}
858859

859860
public enum UpdateStarGiftPriceError {
@@ -865,7 +866,7 @@ public enum UpgradeStarGiftError {
865866
case generic
866867
}
867868

868-
func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
869+
func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
869870
let source: BotPaymentInvoiceSource = .starGiftResale(slug: slug, toPeerId: peerId)
870871
return _internal_fetchBotPaymentForm(accountPeerId: account.peerId, postbox: account.postbox, network: account.network, source: source, themeParams: nil)
871872
|> map(Optional.init)
@@ -874,6 +875,9 @@ func _internal_buyStarGift(account: Account, slug: String, peerId: EnginePeer.Id
874875
}
875876
|> mapToSignal { paymentForm in
876877
if let paymentForm {
878+
if let paymentPrice = paymentForm.invoice.prices.first?.amount, let price, paymentPrice > price {
879+
return .fail(.priceChanged(paymentPrice))
880+
}
877881
return _internal_sendStarsPaymentForm(account: account, formId: paymentForm.id, source: source)
878882
|> mapError { _ -> BuyStarGiftError in
879883
return .generic
@@ -1473,26 +1477,53 @@ private final class ProfileGiftsContextImpl {
14731477
return _internal_transferStarGift(account: self.account, prepaid: prepaid, reference: reference, peerId: peerId)
14741478
}
14751479

1476-
func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
1477-
if let count = self.count {
1478-
self.count = max(0, count - 1)
1479-
}
1480-
self.gifts.removeAll(where: { gift in
1480+
func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
1481+
var listingPrice: Int64?
1482+
if let gift = self.gifts.first(where: { gift in
14811483
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
14821484
return true
14831485
}
14841486
return false
1485-
})
1486-
self.filteredGifts.removeAll(where: { gift in
1487-
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
1488-
return true
1487+
}), case let .unique(uniqueGift) = gift.gift {
1488+
listingPrice = uniqueGift.resellStars
1489+
}
1490+
1491+
if listingPrice == nil {
1492+
if let gift = self.filteredGifts.first(where: { gift in
1493+
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
1494+
return true
1495+
}
1496+
return false
1497+
}), case let .unique(uniqueGift) = gift.gift {
1498+
listingPrice = uniqueGift.resellStars
14891499
}
1490-
return false
1491-
})
1500+
}
1501+
1502+
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price ?? listingPrice)
1503+
|> afterCompleted { [weak self] in
1504+
guard let self else {
1505+
return
1506+
}
1507+
self.queue.async {
1508+
if let count = self.count {
1509+
self.count = max(0, count - 1)
1510+
}
1511+
self.gifts.removeAll(where: { gift in
1512+
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
1513+
return true
1514+
}
1515+
return false
1516+
})
1517+
self.filteredGifts.removeAll(where: { gift in
1518+
if case let .unique(uniqueGift) = gift.gift, uniqueGift.slug == slug {
1519+
return true
1520+
}
1521+
return false
1522+
})
14921523

1493-
self.pushState()
1494-
1495-
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
1524+
self.pushState()
1525+
}
1526+
}
14961527
}
14971528

14981529
func removeStarGift(gift: TelegramCore.StarGift) {
@@ -1975,11 +2006,11 @@ public final class ProfileGiftsContext {
19752006
}
19762007
}
19772008

1978-
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
2009+
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64? = nil) -> Signal<Never, BuyStarGiftError> {
19792010
return Signal { subscriber in
19802011
let disposable = MetaDisposable()
19812012
self.impl.with { impl in
1982-
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId).start(error: { error in
2013+
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId, price: price).start(error: { error in
19832014
subscriber.putError(error)
19842015
}, completed: {
19852016
subscriber.putCompletion()
@@ -2606,8 +2637,18 @@ private final class ResaleGiftsContextImpl {
26062637
self.loadMore()
26072638
}
26082639

2609-
func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
2610-
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
2640+
func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
2641+
var listingPrice: Int64?
2642+
if let gift = self.gifts.first(where: { gift in
2643+
if case let .unique(uniqueGift) = gift, uniqueGift.slug == slug {
2644+
return true
2645+
}
2646+
return false
2647+
}), case let .unique(uniqueGift) = gift {
2648+
listingPrice = uniqueGift.resellStars
2649+
}
2650+
2651+
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price ?? listingPrice)
26112652
|> afterCompleted { [weak self] in
26122653
guard let self else {
26132654
return
@@ -2754,11 +2795,11 @@ public final class ResaleGiftsContext {
27542795
}
27552796
}
27562797

2757-
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
2798+
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64? = nil) -> Signal<Never, BuyStarGiftError> {
27582799
return Signal { subscriber in
27592800
let disposable = MetaDisposable()
27602801
self.impl.with { impl in
2761-
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId).start(error: { error in
2802+
disposable.set(impl.buyStarGift(slug: slug, peerId: peerId, price: price).start(error: { error in
27622803
subscriber.putError(error)
27632804
}, completed: {
27642805
subscriber.putCompletion()

submodules/TelegramCore/Sources/TelegramEngine/Payments/TelegramEnginePayments.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public extension TelegramEngine {
125125
return _internal_transferStarGift(account: self.account, prepaid: prepaid, reference: reference, peerId: peerId)
126126
}
127127

128-
public func buyStarGift(slug: String, peerId: EnginePeer.Id) -> Signal<Never, BuyStarGiftError> {
129-
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId)
128+
public func buyStarGift(slug: String, peerId: EnginePeer.Id, price: Int64?) -> Signal<Never, BuyStarGiftError> {
129+
return _internal_buyStarGift(account: self.account, slug: slug, peerId: peerId, price: price)
130130
}
131131

132132
public func upgradeStarGift(formId: Int64?, reference: StarGiftReference, keepOriginalInfo: Bool) -> Signal<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError> {

submodules/TelegramUI/Components/Gifts/GiftStoreScreen/Sources/GiftStoreScreen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ final class GiftStoreScreenComponent: Component {
269269
subject: .uniqueGift(uniqueGift, state.peerId),
270270
allSubjects: allSubjects,
271271
index: index,
272-
buyGift: { slug, peerId in
273-
return self.state?.starGiftsContext.buyStarGift(slug: slug, peerId: peerId) ?? .complete()
272+
buyGift: { slug, peerId, price in
273+
return self.state?.starGiftsContext.buyStarGift(slug: slug, peerId: peerId, price: price) ?? .complete()
274274
},
275275
updateResellStars: { price in
276276
return self.state?.starGiftsContext.updateStarGiftResellPrice(slug: uniqueGift.slug, price: price) ?? .complete()

submodules/TelegramUI/Components/Gifts/GiftViewScreen/Sources/GiftViewScreen.swift

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ private final class GiftViewSheetContent: CombinedComponent {
11451145
}
11461146
}
11471147

1148-
func commitBuy(skipConfirmation: Bool = false) {
1148+
func commitBuy(acceptedPrice: Int64? = nil, skipConfirmation: Bool = false) {
11491149
guard let resellStars = self.subject.arguments?.resellStars, let starsContext = self.context.starsContext, let starsState = starsContext.currentState, case let .unique(uniqueGift) = self.subject.arguments?.gift else {
11501150
return
11511151
}
@@ -1157,46 +1157,74 @@ private final class GiftViewSheetContent: CombinedComponent {
11571157
let recipientPeerId = self.recipientPeerId ?? self.context.account.peerId
11581158

11591159
let action = {
1160-
let proceed: (Int64) -> Void = { formId in
1160+
let proceed: () -> Void = {
11611161
guard let controller = self.getController() as? GiftViewScreen else {
11621162
return
11631163
}
11641164

11651165
self.inProgress = true
11661166
self.updated()
11671167

1168-
let buyGiftImpl: ((String, EnginePeer.Id) -> Signal<Never, BuyStarGiftError>)
1168+
let buyGiftImpl: ((String, EnginePeer.Id, Int64?) -> Signal<Never, BuyStarGiftError>)
11691169
if let buyGift = controller.buyGift {
1170-
buyGiftImpl = { slug, peerId in
1171-
return buyGift(slug, peerId)
1170+
buyGiftImpl = { slug, peerId, price in
1171+
return buyGift(slug, peerId, price)
11721172
|> afterCompleted {
11731173
context.starsContext?.load(force: true)
11741174
}
11751175
}
11761176
} else {
1177-
buyGiftImpl = { slug, peerId in
1178-
return self.context.engine.payments.buyStarGift(slug: slug, peerId: peerId)
1177+
buyGiftImpl = { slug, peerId, price in
1178+
return self.context.engine.payments.buyStarGift(slug: slug, peerId: peerId, price: price)
11791179
|> afterCompleted {
11801180
context.starsContext?.load(force: true)
11811181
}
11821182
}
11831183
}
11841184

1185-
self.buyDisposable = (buyGiftImpl(uniqueGift.slug, recipientPeerId)
1186-
|> deliverOnMainQueue).start(error: { [weak self] error in
1187-
guard let self, let controller = self.getController() else {
1188-
return
1189-
}
1190-
1191-
self.inProgress = false
1192-
self.updated()
1193-
1194-
let errorText = presentationData.strings.Gift_Send_ErrorUnknown
1195-
1196-
let alertController = textAlertController(context: context, title: nil, text: errorText, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_OK, action: {})], parseMarkdown: true)
1197-
controller.present(alertController, in: .window(.root))
1198-
}, completed: { [weak self, weak starsContext] in
1199-
guard let self, let controller = self.getController() as? GiftViewScreen else {
1185+
self.buyDisposable = (buyGiftImpl(uniqueGift.slug, recipientPeerId, acceptedPrice ?? resellStars)
1186+
|> deliverOnMainQueue).start(
1187+
error: { [weak self] error in
1188+
guard let self, let controller = self.getController() else {
1189+
return
1190+
}
1191+
1192+
self.inProgress = false
1193+
self.updated()
1194+
1195+
switch error {
1196+
case let .priceChanged(newPrice):
1197+
let errorTitle = presentationData.strings.Gift_Buy_ErrorPriceChanged_Title
1198+
let originalPriceString = presentationData.strings.Gift_Buy_ErrorPriceChanged_Text_Stars(Int32(resellStars))
1199+
let newPriceString = presentationData.strings.Gift_Buy_ErrorPriceChanged_Text_Stars(Int32(newPrice))
1200+
let errorText = presentationData.strings.Gift_Buy_ErrorPriceChanged_Text(originalPriceString, newPriceString).string
1201+
1202+
let alertController = textAlertController(
1203+
context: context,
1204+
title: errorTitle,
1205+
text: errorText,
1206+
actions: [
1207+
TextAlertAction(type: .defaultAction, title: presentationData.strings.Gift_Buy_Confirm_BuyFor(Int32(newPrice)), action: { [weak self] in
1208+
guard let self else {
1209+
return
1210+
}
1211+
self.commitBuy(acceptedPrice: newPrice, skipConfirmation: true)
1212+
}),
1213+
TextAlertAction(type: .genericAction, title: presentationData.strings.Common_Cancel, action: {
1214+
})
1215+
],
1216+
actionLayout: .vertical,
1217+
parseMarkdown: true
1218+
)
1219+
controller.present(alertController, in: .window(.root))
1220+
default:
1221+
let alertController = textAlertController(context: context, title: nil, text: presentationData.strings.Gift_Buy_ErrorUnknown, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_OK, action: {})], parseMarkdown: true)
1222+
controller.present(alertController, in: .window(.root))
1223+
}
1224+
},
1225+
completed: { [weak self, weak starsContext] in
1226+
guard let self,
1227+
let controller = self.getController() as? GiftViewScreen else {
12001228
return
12011229
}
12021230
self.inProgress = false
@@ -1303,7 +1331,7 @@ private final class GiftViewSheetContent: CombinedComponent {
13031331

13041332
self.commitBuy(skipConfirmation: true)
13051333
} else {
1306-
proceed(buyForm.id)
1334+
proceed()
13071335
}
13081336
});
13091337
})
@@ -1312,7 +1340,7 @@ private final class GiftViewSheetContent: CombinedComponent {
13121340
controller.push(purchaseController)
13131341
})
13141342
} else {
1315-
proceed(buyForm.id)
1343+
proceed()
13161344
}
13171345
}
13181346
}
@@ -1321,7 +1349,7 @@ private final class GiftViewSheetContent: CombinedComponent {
13211349
action()
13221350
} else {
13231351
let _ = (self.context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: recipientPeerId))
1324-
|> deliverOnMainQueue).start(next: { [weak self] peer in
1352+
|> deliverOnMainQueue).start(next: { [weak self] peer in
13251353
guard let self, let peer else {
13261354
return
13271355
}
@@ -3565,7 +3593,7 @@ public class GiftViewScreen: ViewControllerComponentContainer {
35653593
fileprivate let convertToStars: (() -> Void)?
35663594
fileprivate let transferGift: ((Bool, EnginePeer.Id) -> Signal<Never, TransferStarGiftError>)?
35673595
fileprivate let upgradeGift: ((Int64?, Bool) -> Signal<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError>)?
3568-
fileprivate let buyGift: ((String, EnginePeer.Id) -> Signal<Never, BuyStarGiftError>)?
3596+
fileprivate let buyGift: ((String, EnginePeer.Id, Int64?) -> Signal<Never, BuyStarGiftError>)?
35693597
fileprivate let updateResellStars: ((Int64?) -> Signal<Never, UpdateStarGiftPriceError>)?
35703598
fileprivate let togglePinnedToTop: ((Bool) -> Bool)?
35713599
fileprivate let shareStory: ((StarGift.UniqueGift) -> Void)?
@@ -3582,7 +3610,7 @@ public class GiftViewScreen: ViewControllerComponentContainer {
35823610
convertToStars: (() -> Void)? = nil,
35833611
transferGift: ((Bool, EnginePeer.Id) -> Signal<Never, TransferStarGiftError>)? = nil,
35843612
upgradeGift: ((Int64?, Bool) -> Signal<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError>)? = nil,
3585-
buyGift: ((String, EnginePeer.Id) -> Signal<Never, BuyStarGiftError>)? = nil,
3613+
buyGift: ((String, EnginePeer.Id, Int64?) -> Signal<Never, BuyStarGiftError>)? = nil,
35863614
updateResellStars: ((Int64?) -> Signal<Never, UpdateStarGiftPriceError>)? = nil,
35873615
togglePinnedToTop: ((Bool) -> Bool)? = nil,
35883616
shareStory: ((StarGift.UniqueGift) -> Void)? = nil

0 commit comments

Comments
 (0)