From 997134f82c29d4f2a4fc29a9d2868240b19d0486 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Fri, 29 Nov 2024 11:31:37 +0400 Subject: [PATCH 1/2] Update API --- submodules/TelegramApi/Sources/Api0.swift | 7 +-- submodules/TelegramApi/Sources/Api24.swift | 36 +++++++++----- submodules/TelegramApi/Sources/Api26.swift | 48 ------------------- submodules/TelegramApi/Sources/Api35.swift | 8 ++-- submodules/TelegramApi/Sources/Api37.swift | 13 +++-- .../SyncCore/SyncCore_CachedUserData.swift | 16 +++++-- .../TelegramEngine/Messages/BotWebView.swift | 44 ++++++++--------- .../Peers/TelegramEnginePeers.swift | 4 +- .../Sources/AffiliateProgramSetupScreen.swift | 13 ++--- .../Sources/JoinAffiliateProgramScreen.swift | 2 + 10 files changed, 83 insertions(+), 108 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index 4a67cbd86a..e742c06760 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -903,7 +903,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[1301522832] = { return Api.SponsoredMessage.parse_sponsoredMessage($0) } dict[1124938064] = { return Api.SponsoredMessageReportOption.parse_sponsoredMessageReportOption($0) } dict[1237678029] = { return Api.StarGift.parse_starGift($0) } - dict[708628759] = { return Api.StarRefProgram.parse_starRefProgram($0) } + dict[-586389774] = { return Api.StarRefProgram.parse_starRefProgram($0) } dict[-1145654109] = { return Api.StarsAmount.parse_starsAmount($0) } dict[1577421297] = { return Api.StarsGiftOption.parse_starsGiftOption($0) } dict[-1798404822] = { return Api.StarsGiveawayOption.parse_starsGiveawayOption($0) } @@ -950,7 +950,6 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1870436597] = { return Api.StoryView.parse_storyViewPublicForward($0) } dict[-1116418231] = { return Api.StoryView.parse_storyViewPublicRepost($0) } dict[-1923523370] = { return Api.StoryViews.parse_storyViews($0) } - dict[642695037] = { return Api.SuggestedBotStarRef.parse_suggestedBotStarRef($0) } dict[1964978502] = { return Api.TextWithEntities.parse_textWithEntities($0) } dict[-1609668650] = { return Api.Theme.parse_theme($0) } dict[-94849324] = { return Api.ThemeSettings.parse_themeSettings($0) } @@ -1371,7 +1370,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-919881925] = { return Api.payments.StarsRevenueStats.parse_starsRevenueStats($0) } dict[497778871] = { return Api.payments.StarsRevenueWithdrawalUrl.parse_starsRevenueWithdrawalUrl($0) } dict[1822222573] = { return Api.payments.StarsStatus.parse_starsStatus($0) } - dict[-937776981] = { return Api.payments.SuggestedStarRefBots.parse_suggestedStarRefBots($0) } + dict[-1261053863] = { return Api.payments.SuggestedStarRefBots.parse_suggestedStarRefBots($0) } dict[1801827607] = { return Api.payments.UserStarGifts.parse_userStarGifts($0) } dict[-784000893] = { return Api.payments.ValidatedRequestedInfo.parse_validatedRequestedInfo($0) } dict[541839704] = { return Api.phone.ExportedGroupCallInvite.parse_exportedGroupCallInvite($0) } @@ -2111,8 +2110,6 @@ public extension Api { _1.serialize(buffer, boxed) case let _1 as Api.StoryViews: _1.serialize(buffer, boxed) - case let _1 as Api.SuggestedBotStarRef: - _1.serialize(buffer, boxed) case let _1 as Api.TextWithEntities: _1.serialize(buffer, boxed) case let _1 as Api.Theme: diff --git a/submodules/TelegramApi/Sources/Api24.swift b/submodules/TelegramApi/Sources/Api24.swift index b562efd7c2..4372c5a76d 100644 --- a/submodules/TelegramApi/Sources/Api24.swift +++ b/submodules/TelegramApi/Sources/Api24.swift @@ -644,44 +644,54 @@ public extension Api { } public extension Api { enum StarRefProgram: TypeConstructorDescription { - case starRefProgram(flags: Int32, commissionPermille: Int32, durationMonths: Int32?, endDate: Int32?) + case starRefProgram(flags: Int32, botId: Int64, commissionPermille: Int32, durationMonths: Int32?, endDate: Int32?, dailyRevenuePerUser: Api.StarsAmount?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .starRefProgram(let flags, let commissionPermille, let durationMonths, let endDate): + case .starRefProgram(let flags, let botId, let commissionPermille, let durationMonths, let endDate, let dailyRevenuePerUser): if boxed { - buffer.appendInt32(708628759) + buffer.appendInt32(-586389774) } serializeInt32(flags, buffer: buffer, boxed: false) + serializeInt64(botId, buffer: buffer, boxed: false) serializeInt32(commissionPermille, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 0) != 0 {serializeInt32(durationMonths!, buffer: buffer, boxed: false)} if Int(flags) & Int(1 << 1) != 0 {serializeInt32(endDate!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 2) != 0 {dailyRevenuePerUser!.serialize(buffer, true)} break } } public func descriptionFields() -> (String, [(String, Any)]) { switch self { - case .starRefProgram(let flags, let commissionPermille, let durationMonths, let endDate): - return ("starRefProgram", [("flags", flags as Any), ("commissionPermille", commissionPermille as Any), ("durationMonths", durationMonths as Any), ("endDate", endDate as Any)]) + case .starRefProgram(let flags, let botId, let commissionPermille, let durationMonths, let endDate, let dailyRevenuePerUser): + return ("starRefProgram", [("flags", flags as Any), ("botId", botId as Any), ("commissionPermille", commissionPermille as Any), ("durationMonths", durationMonths as Any), ("endDate", endDate as Any), ("dailyRevenuePerUser", dailyRevenuePerUser as Any)]) } } public static func parse_starRefProgram(_ reader: BufferReader) -> StarRefProgram? { var _1: Int32? _1 = reader.readInt32() - var _2: Int32? - _2 = reader.readInt32() + var _2: Int64? + _2 = reader.readInt64() var _3: Int32? - if Int(_1!) & Int(1 << 0) != 0 {_3 = reader.readInt32() } + _3 = reader.readInt32() var _4: Int32? - if Int(_1!) & Int(1 << 1) != 0 {_4 = reader.readInt32() } + if Int(_1!) & Int(1 << 0) != 0 {_4 = reader.readInt32() } + var _5: Int32? + if Int(_1!) & Int(1 << 1) != 0 {_5 = reader.readInt32() } + var _6: Api.StarsAmount? + if Int(_1!) & Int(1 << 2) != 0 {if let signature = reader.readInt32() { + _6 = Api.parse(reader, signature: signature) as? Api.StarsAmount + } } let _c1 = _1 != nil let _c2 = _2 != nil - let _c3 = (Int(_1!) & Int(1 << 0) == 0) || _3 != nil - let _c4 = (Int(_1!) & Int(1 << 1) == 0) || _4 != nil - if _c1 && _c2 && _c3 && _c4 { - return Api.StarRefProgram.starRefProgram(flags: _1!, commissionPermille: _2!, durationMonths: _3, endDate: _4) + let _c3 = _3 != nil + let _c4 = (Int(_1!) & Int(1 << 0) == 0) || _4 != nil + let _c5 = (Int(_1!) & Int(1 << 1) == 0) || _5 != nil + let _c6 = (Int(_1!) & Int(1 << 2) == 0) || _6 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 { + return Api.StarRefProgram.starRefProgram(flags: _1!, botId: _2!, commissionPermille: _3!, durationMonths: _4, endDate: _5, dailyRevenuePerUser: _6) } else { return nil diff --git a/submodules/TelegramApi/Sources/Api26.swift b/submodules/TelegramApi/Sources/Api26.swift index 76a74ddd32..d91cf204b7 100644 --- a/submodules/TelegramApi/Sources/Api26.swift +++ b/submodules/TelegramApi/Sources/Api26.swift @@ -272,54 +272,6 @@ public extension Api { } } -public extension Api { - enum SuggestedBotStarRef: TypeConstructorDescription { - case suggestedBotStarRef(flags: Int32, botId: Int64, commissionPermille: Int32, durationMonths: Int32?) - - public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { - switch self { - case .suggestedBotStarRef(let flags, let botId, let commissionPermille, let durationMonths): - if boxed { - buffer.appendInt32(642695037) - } - serializeInt32(flags, buffer: buffer, boxed: false) - serializeInt64(botId, buffer: buffer, boxed: false) - serializeInt32(commissionPermille, buffer: buffer, boxed: false) - if Int(flags) & Int(1 << 0) != 0 {serializeInt32(durationMonths!, buffer: buffer, boxed: false)} - break - } - } - - public func descriptionFields() -> (String, [(String, Any)]) { - switch self { - case .suggestedBotStarRef(let flags, let botId, let commissionPermille, let durationMonths): - return ("suggestedBotStarRef", [("flags", flags as Any), ("botId", botId as Any), ("commissionPermille", commissionPermille as Any), ("durationMonths", durationMonths as Any)]) - } - } - - public static func parse_suggestedBotStarRef(_ reader: BufferReader) -> SuggestedBotStarRef? { - var _1: Int32? - _1 = reader.readInt32() - var _2: Int64? - _2 = reader.readInt64() - var _3: Int32? - _3 = reader.readInt32() - var _4: Int32? - if Int(_1!) & Int(1 << 0) != 0 {_4 = reader.readInt32() } - let _c1 = _1 != nil - let _c2 = _2 != nil - let _c3 = _3 != nil - let _c4 = (Int(_1!) & Int(1 << 0) == 0) || _4 != nil - if _c1 && _c2 && _c3 && _c4 { - return Api.SuggestedBotStarRef.suggestedBotStarRef(flags: _1!, botId: _2!, commissionPermille: _3!, durationMonths: _4) - } - else { - return nil - } - } - - } -} public extension Api { enum TextWithEntities: TypeConstructorDescription { case textWithEntities(text: String, entities: [Api.MessageEntity]) diff --git a/submodules/TelegramApi/Sources/Api35.swift b/submodules/TelegramApi/Sources/Api35.swift index ac046082c8..ea5ad93cbd 100644 --- a/submodules/TelegramApi/Sources/Api35.swift +++ b/submodules/TelegramApi/Sources/Api35.swift @@ -214,13 +214,13 @@ public extension Api.payments { } public extension Api.payments { enum SuggestedStarRefBots: TypeConstructorDescription { - case suggestedStarRefBots(flags: Int32, count: Int32, suggestedBots: [Api.SuggestedBotStarRef], users: [Api.User], nextOffset: String?) + case suggestedStarRefBots(flags: Int32, count: Int32, suggestedBots: [Api.StarRefProgram], users: [Api.User], nextOffset: String?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { case .suggestedStarRefBots(let flags, let count, let suggestedBots, let users, let nextOffset): if boxed { - buffer.appendInt32(-937776981) + buffer.appendInt32(-1261053863) } serializeInt32(flags, buffer: buffer, boxed: false) serializeInt32(count, buffer: buffer, boxed: false) @@ -251,9 +251,9 @@ public extension Api.payments { _1 = reader.readInt32() var _2: Int32? _2 = reader.readInt32() - var _3: [Api.SuggestedBotStarRef]? + var _3: [Api.StarRefProgram]? if let _ = reader.readInt32() { - _3 = Api.parseVector(reader, elementSignature: 0, elementType: Api.SuggestedBotStarRef.self) + _3 = Api.parseVector(reader, elementSignature: 0, elementType: Api.StarRefProgram.self) } var _4: [Api.User]? if let _ = reader.readInt32() { diff --git a/submodules/TelegramApi/Sources/Api37.swift b/submodules/TelegramApi/Sources/Api37.swift index c8254885b6..936754286d 100644 --- a/submodules/TelegramApi/Sources/Api37.swift +++ b/submodules/TelegramApi/Sources/Api37.swift @@ -7873,16 +7873,21 @@ public extension Api.functions.messages { } } public extension Api.functions.messages { - static func searchStickers(flags: Int32, q: String, langCode: String, offset: Int32, limit: Int32, hash: Int64) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + static func searchStickers(flags: Int32, q: String, emoticon: String, langCode: [String], offset: Int32, limit: Int32, hash: Int64) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() - buffer.appendInt32(1277568311) + buffer.appendInt32(699516522) serializeInt32(flags, buffer: buffer, boxed: false) serializeString(q, buffer: buffer, boxed: false) - serializeString(langCode, buffer: buffer, boxed: false) + serializeString(emoticon, buffer: buffer, boxed: false) + buffer.appendInt32(481674261) + buffer.appendInt32(Int32(langCode.count)) + for item in langCode { + serializeString(item, buffer: buffer, boxed: false) + } serializeInt32(offset, buffer: buffer, boxed: false) serializeInt32(limit, buffer: buffer, boxed: false) serializeInt64(hash, buffer: buffer, boxed: false) - return (FunctionDescription(name: "messages.searchStickers", parameters: [("flags", String(describing: flags)), ("q", String(describing: q)), ("langCode", String(describing: langCode)), ("offset", String(describing: offset)), ("limit", String(describing: limit)), ("hash", String(describing: hash))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.FoundStickers? in + return (FunctionDescription(name: "messages.searchStickers", parameters: [("flags", String(describing: flags)), ("q", String(describing: q)), ("emoticon", String(describing: emoticon)), ("langCode", String(describing: langCode)), ("offset", String(describing: offset)), ("limit", String(describing: limit)), ("hash", String(describing: hash))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.FoundStickers? in let reader = BufferReader(buffer) var result: Api.messages.FoundStickers? if let signature = reader.readInt32() { diff --git a/submodules/TelegramCore/Sources/SyncCore/SyncCore_CachedUserData.swift b/submodules/TelegramCore/Sources/SyncCore/SyncCore_CachedUserData.swift index d4e86caf10..6e392dc6ed 100644 --- a/submodules/TelegramCore/Sources/SyncCore/SyncCore_CachedUserData.swift +++ b/submodules/TelegramCore/Sources/SyncCore/SyncCore_CachedUserData.swift @@ -627,17 +627,24 @@ extension TelegramBusinessChatLinks { } public final class TelegramStarRefProgram: Codable, Equatable { + public let botId: PeerId public let commissionPermille: Int32 public let durationMonths: Int32? public let endDate: Int32? + public let dailyRevenuePerUser: StarsAmount? - public init(commissionPermille: Int32, durationMonths: Int32?, endDate: Int32?) { + public init(botId: PeerId, commissionPermille: Int32, durationMonths: Int32?, endDate: Int32?, dailyRevenuePerUser: StarsAmount?) { + self.botId = botId self.commissionPermille = commissionPermille self.durationMonths = durationMonths self.endDate = endDate + self.dailyRevenuePerUser = dailyRevenuePerUser } public static func ==(lhs: TelegramStarRefProgram, rhs: TelegramStarRefProgram) -> Bool { + if lhs.botId != rhs.botId { + return false + } if lhs.commissionPermille != rhs.commissionPermille { return false } @@ -647,6 +654,9 @@ public final class TelegramStarRefProgram: Codable, Equatable { if lhs.endDate != rhs.endDate { return false } + if lhs.dailyRevenuePerUser != rhs.dailyRevenuePerUser { + return false + } return true } } @@ -654,8 +664,8 @@ public final class TelegramStarRefProgram: Codable, Equatable { extension TelegramStarRefProgram { convenience init(apiStarRefProgram: Api.StarRefProgram) { switch apiStarRefProgram { - case let .starRefProgram(_, commissionPermille, durationMonths, endDate): - self.init(commissionPermille: commissionPermille, durationMonths: durationMonths, endDate: endDate) + case let .starRefProgram(_, botId, commissionPermille, durationMonths, endDate, dailyRevenuePerUser): + self.init(botId: PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt64Value(botId)), commissionPermille: commissionPermille, durationMonths: durationMonths, endDate: endDate, dailyRevenuePerUser: dailyRevenuePerUser.flatMap(StarsAmount.init(apiAmount:))) } } } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift index 22594e5a06..91403e5290 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift @@ -753,27 +753,23 @@ public final class TelegramSuggestedStarRefBotList: Equatable { public enum SortMode { case date case commission + case revenue } public final class Item: Equatable { public let peer: EnginePeer - public let commissionPermille: Int32 - public let durationMonths: Int32? + public let program: TelegramStarRefProgram - public init(peer: EnginePeer, commissionPermille: Int32, durationMonths: Int32?) { + public init(peer: EnginePeer, program: TelegramStarRefProgram) { self.peer = peer - self.commissionPermille = commissionPermille - self.durationMonths = durationMonths + self.program = program } public static func ==(lhs: Item, rhs: Item) -> Bool { if lhs.peer != rhs.peer { return false } - if lhs.commissionPermille != rhs.commissionPermille { - return false - } - if lhs.durationMonths != rhs.durationMonths { + if lhs.program != rhs.program { return false } return true @@ -795,7 +791,7 @@ public final class TelegramSuggestedStarRefBotList: Equatable { } } -func _internal_requestSuggestedStarRefBots(account: Account, id: EnginePeer.Id, orderByCommission: Bool, offset: String?, limit: Int) -> Signal { +func _internal_requestSuggestedStarRefBots(account: Account, id: EnginePeer.Id, sortMode: TelegramSuggestedStarRefBotList.SortMode, offset: String?, limit: Int) -> Signal { return account.postbox.transaction { transaction -> Api.InputPeer? in return transaction.getPeer(id).flatMap(apiInputPeer) } @@ -804,8 +800,13 @@ func _internal_requestSuggestedStarRefBots(account: Account, id: EnginePeer.Id, return .single(nil) } var flags: Int32 = 0 - if orderByCommission { - flags |= 1 << 2 + switch sortMode { + case .revenue: + flags |= 1 << 0 + case .date: + flags |= 1 << 1 + case .commission: + break } return account.network.request(Api.functions.payments.getSuggestedStarRefBots( flags: flags, @@ -827,18 +828,15 @@ func _internal_requestSuggestedStarRefBots(account: Account, id: EnginePeer.Id, updatePeers(transaction: transaction, accountPeerId: account.peerId, peers: AccumulatedPeers(users: users)) var items: [TelegramSuggestedStarRefBotList.Item] = [] - for suggestedBot in suggestedBots { - switch suggestedBot { - case let .suggestedBotStarRef(_, botId, commissionPermille, durationMonths): - guard let botPeer = transaction.getPeer(PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt64Value(botId))) else { - continue - } - items.append(TelegramSuggestedStarRefBotList.Item( - peer: EnginePeer(botPeer), - commissionPermille: commissionPermille, - durationMonths: durationMonths - )) + for starRefProgram in suggestedBots { + let parsedProgram = TelegramStarRefProgram(apiStarRefProgram: starRefProgram) + guard let botPeer = transaction.getPeer(parsedProgram.botId) else { + continue } + items.append(TelegramSuggestedStarRefBotList.Item( + peer: EnginePeer(botPeer), + program: parsedProgram + )) } return TelegramSuggestedStarRefBotList(items: items, totalCount: Int(count), nextOffset: nextOffset) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 2ccdd25a1f..09238b232f 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -1641,8 +1641,8 @@ public extension TelegramEngine { return _internal_requestConnectedStarRefBots(account: self.account, id: id, offset: offset, limit: limit) } - public func requestSuggestedStarRefBots(id: EnginePeer.Id, orderByCommission: Bool, offset: String?, limit: Int) -> Signal { - return _internal_requestSuggestedStarRefBots(account: self.account, id: id, orderByCommission: orderByCommission, offset: offset, limit: limit) + public func requestSuggestedStarRefBots(id: EnginePeer.Id, sortMode: TelegramSuggestedStarRefBotList.SortMode, offset: String?, limit: Int) -> Signal { + return _internal_requestSuggestedStarRefBots(account: self.account, id: id, sortMode: sortMode, offset: offset, limit: limit) } public func connectStarRefBot(id: EnginePeer.Id, botId: EnginePeer.Id) -> Signal { diff --git a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift index 39323b7ae7..bc06084a21 100644 --- a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift +++ b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift @@ -412,6 +412,7 @@ If you end your affiliate program: let availableModes: [(TelegramSuggestedStarRefBotList.SortMode, String)] = [ (.date, "Date"), + (.revenue, "Revenue"), (.commission, "Commission") ] for (mode, title) in availableModes { @@ -436,7 +437,7 @@ If you end your affiliate program: self.suggestedStarBotListDisposable?.dispose() self.suggestedStarBotListDisposable = (component.context.engine.peers.requestSuggestedStarRefBots( id: component.initialContent.peerId, - orderByCommission: self.suggestedSortMode == .commission, + sortMode: self.suggestedSortMode, offset: nil, limit: 100) |> deliverOnMainQueue).startStrict(next: { [weak self] list in @@ -559,7 +560,7 @@ If you end your affiliate program: self.suggestedStarBotListDisposable = (component.context.engine.peers.requestSuggestedStarRefBots( id: component.initialContent.peerId, - orderByCommission: false, + sortMode: self.suggestedSortMode, offset: nil, limit: 100) |> deliverOnMainQueue).startStrict(next: { [weak self] list in @@ -1344,9 +1345,9 @@ If you end your affiliate program: do { var suggestedSectionItems: [AnyComponentWithIdentity] = [] for item in suggestedStarBotListItems { - let commissionTitle = "\(item.commissionPermille / 10)%" + let commissionTitle = "\(item.program.commissionPermille / 10)%" let durationTitle: String - if let durationMonths = item.durationMonths { + if let durationMonths = item.program.durationMonths { durationTitle = timeIntervalString(strings: environment.strings, value: durationMonths * (24 * 60 * 60)) } else { durationTitle = "Lifetime" @@ -1401,8 +1402,8 @@ If you end your affiliate program: self.environment?.controller()?.push(JoinAffiliateProgramScreen( context: component.context, sourcePeer: botPeer, - commissionPermille: item.commissionPermille, - programDuration: item.durationMonths, + commissionPermille: item.program.commissionPermille, + programDuration: item.program.durationMonths, mode: .join(JoinAffiliateProgramScreenMode.Join( initialTargetPeer: targetPeer, canSelectTargetPeer: false, diff --git a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift index dedb249075..60f2336070 100644 --- a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift +++ b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift @@ -1636,6 +1636,8 @@ final class BotSectionSortButtonComponent: Component { sortByString = "SORT BY [DATE]()" case .commission: sortByString = "SORT BY [COMMISSION]()" + case .revenue: + sortByString = "SORT BY [REVENUE]()" } let textSize = self.text.update( transition: .immediate, From ef652c0e306d56a1b6fd818deb59d778cd54143f Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Fri, 29 Nov 2024 12:42:50 +0400 Subject: [PATCH 2/2] Stars refs --- .../TelegramEngine/Messages/BotWebView.swift | 49 ++++++++++++- .../Peers/TelegramEnginePeers.swift | 4 + .../Sources/AffiliateProgramSetupScreen.swift | 2 +- .../Sources/JoinAffiliateProgramScreen.swift | 73 +++++++++++++------ 4 files changed, 104 insertions(+), 24 deletions(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift index 91403e5290..ed675dcccd 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/BotWebView.swift @@ -752,7 +752,7 @@ func _internal_requestConnectedStarRefBots(account: Account, id: EnginePeer.Id, public final class TelegramSuggestedStarRefBotList: Equatable { public enum SortMode { case date - case commission + case profitability case revenue } @@ -805,7 +805,7 @@ func _internal_requestSuggestedStarRefBots(account: Account, id: EnginePeer.Id, flags |= 1 << 0 case .date: flags |= 1 << 1 - case .commission: + case .profitability: break } return account.network.request(Api.functions.payments.getSuggestedStarRefBots( @@ -1008,3 +1008,48 @@ func _internal_getStarRefBotConnection(account: Account, id: EnginePeer.Id, targ } } } + +func _internal_getPossibleStarRefBotTargets(account: Account) -> Signal<[EnginePeer], NoError> { + return combineLatest( + account.network.request(Api.functions.bots.getAdminedBots()) + |> `catch` { _ -> Signal<[Api.User], NoError> in + return .single([]) + }, + account.network.request(Api.functions.channels.getAdminedPublicChannels(flags: 0)) + |> map(Optional.init) + |> `catch` { _ -> Signal in + return .single(nil) + } + ) + |> mapToSignal { apiBots, apiChannels -> Signal<[EnginePeer], NoError> in + return account.postbox.transaction { transaction -> [EnginePeer] in + var result: [EnginePeer] = [] + + if let peer = transaction.getPeer(account.peerId) { + result.append(EnginePeer(peer)) + } + + updatePeers(transaction: transaction, accountPeerId: account.peerId, peers: AccumulatedPeers(users: apiBots)) + for bot in apiBots { + if let peer = transaction.getPeer(bot.peerId) { + result.append(EnginePeer(peer)) + } + } + + if let apiChannels { + switch apiChannels { + case let .chats(chats), let .chatsSlice(_, chats): + updatePeers(transaction: transaction, accountPeerId: account.peerId, peers: AccumulatedPeers(chats: chats, users: [])) + + for chat in chats { + if let peer = transaction.getPeer(chat.peerId) { + result.append(EnginePeer(peer)) + } + } + } + } + + return result + } + } +} diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 09238b232f..b5f66d6f2b 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -1656,6 +1656,10 @@ public extension TelegramEngine { public func getStarRefBotConnection(id: EnginePeer.Id, targetId: EnginePeer.Id) -> Signal { return _internal_getStarRefBotConnection(account: self.account, id: id, targetId: targetId) } + + public func getPossibleStarRefBotTargets() -> Signal<[EnginePeer], NoError> { + return _internal_getPossibleStarRefBotTargets(account: self.account) + } } } diff --git a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift index bc06084a21..3580b86fde 100644 --- a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift +++ b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/AffiliateProgramSetupScreen.swift @@ -413,7 +413,7 @@ If you end your affiliate program: let availableModes: [(TelegramSuggestedStarRefBotList.SortMode, String)] = [ (.date, "Date"), (.revenue, "Revenue"), - (.commission, "Commission") + (.profitability, "Profitability") ] for (mode, title) in availableModes { let isSelected = mode == self.suggestedSortMode diff --git a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift index 60f2336070..a16b6c5df0 100644 --- a/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift +++ b/submodules/TelegramUI/Components/PeerInfo/AffiliateProgramSetupScreen/Sources/JoinAffiliateProgramScreen.swift @@ -120,6 +120,9 @@ private final class JoinAffiliateProgramScreenComponent: Component { private var currentTargetPeer: EnginePeer? + private var possibleTargetPeers: [EnginePeer] = [] + private var possibleTargetPeersDisposable: Disposable? + private var cachedCloseImage: UIImage? override init(frame: CGRect) { @@ -189,6 +192,10 @@ private final class JoinAffiliateProgramScreenComponent: Component { fatalError("init(coder:) has not been implemented") } + deinit { + self.possibleTargetPeersDisposable?.dispose() + } + func scrollViewDidScroll(_ scrollView: UIScrollView) { if !self.ignoreScrolling { self.updateScrolling(transition: .immediate) @@ -338,9 +345,9 @@ private final class JoinAffiliateProgramScreenComponent: Component { let presentationData = component.context.sharedContext.currentPresentationData.with({ $0 }) - let peers: [EnginePeer] = [ + let peers: [EnginePeer] = self.possibleTargetPeers.isEmpty ? [ join.initialTargetPeer - ] + ] : self.possibleTargetPeers let avatarSize = CGSize(width: 30.0, height: 30.0) @@ -383,11 +390,24 @@ private final class JoinAffiliateProgramScreenComponent: Component { let sideInset: CGFloat = 16.0 + environment.safeInsets.left if self.component == nil { + var loadPossibleTargetPeers = false switch component.mode { case let .join(join): self.currentTargetPeer = join.initialTargetPeer + loadPossibleTargetPeers = join.canSelectTargetPeer case let .active(active): self.currentTargetPeer = active.targetPeer + loadPossibleTargetPeers = true + } + + if loadPossibleTargetPeers { + self.possibleTargetPeersDisposable = (component.context.engine.peers.getPossibleStarRefBotTargets() + |> deliverOnMainQueue).startStrict(next: { [weak self] result in + guard let self else { + return + } + self.possibleTargetPeers = result + }) } } @@ -701,6 +721,7 @@ private final class JoinAffiliateProgramScreenComponent: Component { isTargetPeerSelectable = join.canSelectTargetPeer case .active: displayTargetPeer = true + isTargetPeerSelectable = true } if displayTargetPeer { @@ -1154,7 +1175,7 @@ private final class PeerBadgeComponent: Component { if let current = self.avatarNode { avatarNode = current } else { - avatarNode = AvatarNode(font: avatarPlaceholderFont(size: 15.0)) + avatarNode = AvatarNode(font: avatarPlaceholderFont(size: floor(avatarDiameter * 0.5))) avatarNode.isUserInteractionEnabled = false avatarNode.displaysAsynchronously = false self.avatarNode = avatarNode @@ -1252,18 +1273,13 @@ private final class AvatarComponent: Component { } final class View: UIView { - private let avatarNode: AvatarNode + private var avatarNode: AvatarNode? private var component: AvatarComponent? private weak var state: EmptyComponentState? override init(frame: CGRect) { - self.avatarNode = AvatarNode(font: avatarPlaceholderFont(size: 18.0)) - self.avatarNode.displaysAsynchronously = false - super.init(frame: frame) - - self.addSubnode(self.avatarNode) } required init?(coder: NSCoder) { @@ -1276,14 +1292,24 @@ private final class AvatarComponent: Component { let size = component.size ?? availableSize - self.avatarNode.frame = CGRect(origin: CGPoint(), size: size) - self.avatarNode.setPeer( + let avatarNode: AvatarNode + if let current = self.avatarNode { + avatarNode = current + } else { + avatarNode = AvatarNode(font: avatarPlaceholderFont(size: floor(size.width * 0.5))) + avatarNode.displaysAsynchronously = false + self.avatarNode = avatarNode + self.addSubview(avatarNode.view) + } + avatarNode.frame = CGRect(origin: CGPoint(), size: size) + avatarNode.setPeer( context: component.context, theme: component.context.sharedContext.currentPresentationData.with({ $0 }).theme, peer: component.peer, synchronousLoad: true, displayDimensions: size ) + avatarNode.updateSize(size: size) return size } @@ -1634,8 +1660,8 @@ final class BotSectionSortButtonComponent: Component { switch component.sortMode { case .date: sortByString = "SORT BY [DATE]()" - case .commission: - sortByString = "SORT BY [COMMISSION]()" + case .profitability: + sortByString = "SORT BY [PROFITABILITY]()" case .revenue: sortByString = "SORT BY [REVENUE]()" } @@ -1732,7 +1758,7 @@ final class PeerBadgeAvatarComponent: Component { } final class View: UIView { - private let avatarNode: AvatarNode + private var avatarNode: AvatarNode? private let badgeBackground = UIImageView() private let badgeIcon = UIImageView() @@ -1744,12 +1770,7 @@ final class PeerBadgeAvatarComponent: Component { private static let badgeIconImage = generateTintedImage(image: UIImage(bundleImageName: "Chat/Links/Link"), color: .white)?.withRenderingMode(.alwaysTemplate) override init(frame: CGRect) { - self.avatarNode = AvatarNode(font: avatarPlaceholderFont(size: 15.0)) - self.avatarNode.displaysAsynchronously = false - super.init(frame: frame) - - self.addSubnode(self.avatarNode) } required init?(coder: NSCoder) { @@ -1765,8 +1786,18 @@ final class PeerBadgeAvatarComponent: Component { let badgeFrame = CGRect(origin: CGPoint(x: size.width - badgeSize, y: size.height - badgeSize), size: CGSize(width: badgeSize, height: badgeSize)) - self.avatarNode.frame = CGRect(origin: CGPoint(), size: size) - self.avatarNode.setPeer( + let avatarNode: AvatarNode + if let current = self.avatarNode { + avatarNode = current + } else { + avatarNode = AvatarNode(font: avatarPlaceholderFont(size: floor(size.width * 0.5))) + avatarNode.displaysAsynchronously = false + self.avatarNode = avatarNode + self.addSubview(avatarNode.view) + } + + avatarNode.frame = CGRect(origin: CGPoint(), size: size) + avatarNode.setPeer( context: component.context, theme: component.context.sharedContext.currentPresentationData.with({ $0 }).theme, peer: component.peer,