From a526aee73e521a83c5bdda6d825647ffe78ba955 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Thu, 15 Jan 2026 19:19:24 +0800 Subject: [PATCH] Refactor Api types 13-22 to use struct-wrapped constructors Types refactored: - AvailableReaction, BankCardOpenUrl, Birthday, Boost - BotApp, BotAppSettings, BotBusinessConnection, BotCommand Updated pattern matching and construction sites in TelegramCore. Co-Authored-By: Claude Opus 4.5 --- submodules/TelegramApi/Sources/Api1.swift | 116 +++++++++++++++++- submodules/TelegramApi/Sources/Api2.swift | 28 ++++- .../Sources/ApiUtils/BotInfo.swift | 6 +- .../ApiUtils/TelegramMediaAction.swift | 3 +- .../State/AccountStateManagementUtils.swift | 3 +- .../Sources/State/AvailableReactions.swift | 3 +- .../Sources/State/ChannelBoost.swift | 3 +- .../Messages/AttachMenuBots.swift | 6 +- .../TelegramEngine/Payments/BankCards.swift | 3 +- .../TelegramEngine/Peers/Birthdays.swift | 5 +- 10 files changed, 157 insertions(+), 19 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api1.swift b/submodules/TelegramApi/Sources/Api1.swift index 29494ea66c..46ffc93f80 100644 --- a/submodules/TelegramApi/Sources/Api1.swift +++ b/submodules/TelegramApi/Sources/Api1.swift @@ -565,7 +565,31 @@ public extension Api { } public extension Api { enum AvailableReaction: TypeConstructorDescription { - case availableReaction(flags: Int32, reaction: String, title: String, staticIcon: Api.Document, appearAnimation: Api.Document, selectAnimation: Api.Document, activateAnimation: Api.Document, effectAnimation: Api.Document, aroundAnimation: Api.Document?, centerIcon: Api.Document?) + public class Cons_availableReaction { + public var flags: Int32 + public var reaction: String + public var title: String + public var staticIcon: Api.Document + public var appearAnimation: Api.Document + public var selectAnimation: Api.Document + public var activateAnimation: Api.Document + public var effectAnimation: Api.Document + public var aroundAnimation: Api.Document? + public var centerIcon: Api.Document? + public init(flags: Int32, reaction: String, title: String, staticIcon: Api.Document, appearAnimation: Api.Document, selectAnimation: Api.Document, activateAnimation: Api.Document, effectAnimation: Api.Document, aroundAnimation: Api.Document?, centerIcon: Api.Document?) { + self.flags = flags + self.reaction = reaction + self.title = title + self.staticIcon = staticIcon + self.appearAnimation = appearAnimation + self.selectAnimation = selectAnimation + self.activateAnimation = activateAnimation + self.effectAnimation = effectAnimation + self.aroundAnimation = aroundAnimation + self.centerIcon = centerIcon + } + } + case availableReaction(Cons_availableReaction) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG @@ -594,7 +618,15 @@ public extension Api { } public extension Api { enum BankCardOpenUrl: TypeConstructorDescription { - case bankCardOpenUrl(url: String, name: String) + public class Cons_bankCardOpenUrl { + public var url: String + public var name: String + public init(url: String, name: String) { + self.url = url + self.name = name + } + } + case bankCardOpenUrl(Cons_bankCardOpenUrl) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG @@ -684,7 +716,19 @@ public extension Api { } public extension Api { enum Birthday: TypeConstructorDescription { - case birthday(flags: Int32, day: Int32, month: Int32, year: Int32?) + public class Cons_birthday { + public var flags: Int32 + public var day: Int32 + public var month: Int32 + public var year: Int32? + public init(flags: Int32, day: Int32, month: Int32, year: Int32?) { + self.flags = flags + self.day = day + self.month = month + self.year = year + } + } + case birthday(Cons_birthday) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG @@ -750,7 +794,29 @@ public extension Api { } public extension Api { enum Boost: TypeConstructorDescription { - case boost(flags: Int32, id: String, userId: Int64?, giveawayMsgId: Int32?, date: Int32, expires: Int32, usedGiftSlug: String?, multiplier: Int32?, stars: Int64?) + public class Cons_boost { + public var flags: Int32 + public var id: String + public var userId: Int64? + public var giveawayMsgId: Int32? + public var date: Int32 + public var expires: Int32 + public var usedGiftSlug: String? + public var multiplier: Int32? + public var stars: Int64? + public init(flags: Int32, id: String, userId: Int64?, giveawayMsgId: Int32?, date: Int32, expires: Int32, usedGiftSlug: String?, multiplier: Int32?, stars: Int64?) { + self.flags = flags + self.id = id + self.userId = userId + self.giveawayMsgId = giveawayMsgId + self.date = date + self.expires = expires + self.usedGiftSlug = usedGiftSlug + self.multiplier = multiplier + self.stars = stars + } + } + case boost(Cons_boost) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG @@ -779,7 +845,29 @@ public extension Api { } public extension Api { enum BotApp: TypeConstructorDescription { - case botApp(flags: Int32, id: Int64, accessHash: Int64, shortName: String, title: String, description: String, photo: Api.Photo, document: Api.Document?, hash: Int64) + public class Cons_botApp { + public var flags: Int32 + public var id: Int64 + public var accessHash: Int64 + public var shortName: String + public var title: String + public var description: String + public var photo: Api.Photo + public var document: Api.Document? + public var hash: Int64 + public init(flags: Int32, id: Int64, accessHash: Int64, shortName: String, title: String, description: String, photo: Api.Photo, document: Api.Document?, hash: Int64) { + self.flags = flags + self.id = id + self.accessHash = accessHash + self.shortName = shortName + self.title = title + self.description = description + self.photo = photo + self.document = document + self.hash = hash + } + } + case botApp(Cons_botApp) case botAppNotModified public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { @@ -816,7 +904,23 @@ public extension Api { } public extension Api { enum BotAppSettings: TypeConstructorDescription { - case botAppSettings(flags: Int32, placeholderPath: Buffer?, backgroundColor: Int32?, backgroundDarkColor: Int32?, headerColor: Int32?, headerDarkColor: Int32?) + public class Cons_botAppSettings { + public var flags: Int32 + public var placeholderPath: Buffer? + public var backgroundColor: Int32? + public var backgroundDarkColor: Int32? + public var headerColor: Int32? + public var headerDarkColor: Int32? + public init(flags: Int32, placeholderPath: Buffer?, backgroundColor: Int32?, backgroundDarkColor: Int32?, headerColor: Int32?, headerDarkColor: Int32?) { + self.flags = flags + self.placeholderPath = placeholderPath + self.backgroundColor = backgroundColor + self.backgroundDarkColor = backgroundDarkColor + self.headerColor = headerColor + self.headerDarkColor = headerDarkColor + } + } + case botAppSettings(Cons_botAppSettings) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG diff --git a/submodules/TelegramApi/Sources/Api2.swift b/submodules/TelegramApi/Sources/Api2.swift index ef3a29019d..f594d8183d 100644 --- a/submodules/TelegramApi/Sources/Api2.swift +++ b/submodules/TelegramApi/Sources/Api2.swift @@ -1,6 +1,22 @@ public extension Api { enum BotBusinessConnection: TypeConstructorDescription { - case botBusinessConnection(flags: Int32, connectionId: String, userId: Int64, dcId: Int32, date: Int32, rights: Api.BusinessBotRights?) + public class Cons_botBusinessConnection { + public var flags: Int32 + public var connectionId: String + public var userId: Int64 + public var dcId: Int32 + public var date: Int32 + public var rights: Api.BusinessBotRights? + public init(flags: Int32, connectionId: String, userId: Int64, dcId: Int32, date: Int32, rights: Api.BusinessBotRights?) { + self.flags = flags + self.connectionId = connectionId + self.userId = userId + self.dcId = dcId + self.date = date + self.rights = rights + } + } + case botBusinessConnection(Cons_botBusinessConnection) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG @@ -29,7 +45,15 @@ public extension Api { } public extension Api { enum BotCommand: TypeConstructorDescription { - case botCommand(command: String, description: String) + public class Cons_botCommand { + public var command: String + public var description: String + public init(command: String, description: String) { + self.command = command + self.description = description + } + } + case botCommand(Cons_botCommand) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { #if DEBUG diff --git a/submodules/TelegramCore/Sources/ApiUtils/BotInfo.swift b/submodules/TelegramCore/Sources/ApiUtils/BotInfo.swift index b12c38fa1b..87ca18a5ed 100644 --- a/submodules/TelegramCore/Sources/ApiUtils/BotInfo.swift +++ b/submodules/TelegramCore/Sources/ApiUtils/BotInfo.swift @@ -16,7 +16,8 @@ extension BotMenuButton { extension BotAppSettings { init(apiBotAppSettings: Api.BotAppSettings) { switch apiBotAppSettings { - case let .botAppSettings(_, placeholder, backgroundColor, backgroundDarkColor, headerColor, headerDarkColor): + case let .botAppSettings(botAppSettingsData): + let (_, placeholder, backgroundColor, backgroundDarkColor, headerColor, headerDarkColor) = (botAppSettingsData.flags, botAppSettingsData.placeholderPath, botAppSettingsData.backgroundColor, botAppSettingsData.backgroundDarkColor, botAppSettingsData.headerColor, botAppSettingsData.headerDarkColor) self.init( placeholderData: placeholder.flatMap { $0.makeData() }, backgroundColor: backgroundColor, @@ -52,7 +53,8 @@ extension BotInfo { if let apiCommands = apiCommands { commands = apiCommands.map { command in switch command { - case let .botCommand(command, description): + case let .botCommand(botCommandData): + let (command, description) = (botCommandData.command, botCommandData.description) return BotCommand(text: command, description: description) } } diff --git a/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaAction.swift b/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaAction.swift index 2833b94014..e297fd0d61 100644 --- a/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaAction.swift +++ b/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaAction.swift @@ -56,7 +56,8 @@ func telegramMediaActionFromApiAction(_ action: Api.MessageAction) -> TelegramMe return TelegramMediaAction(action: .botDomainAccessGranted(domain: domain)) } else { var appName: String? - if case let .botApp(_, _, _, _, appNameValue, _, _, _, _) = app { + if case let .botApp(botAppData) = app { + let appNameValue = botAppData.title appName = appNameValue } var type: BotSendMessageAccessGrantedType? diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index 7caf086c88..7aaee98543 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -1774,7 +1774,8 @@ private func finalStateWithUpdatesAndServerTime(accountPeerId: PeerId, postbox: let botPeerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt64Value(botId)) let commands: [BotCommand] = apiCommands.map { command in switch command { - case let .botCommand(command, description): + case let .botCommand(botCommandData): + let (command, description) = (botCommandData.command, botCommandData.description) return BotCommand(text: command, description: description) } } diff --git a/submodules/TelegramCore/Sources/State/AvailableReactions.swift b/submodules/TelegramCore/Sources/State/AvailableReactions.swift index c9b304da70..6934e4f7a8 100644 --- a/submodules/TelegramCore/Sources/State/AvailableReactions.swift +++ b/submodules/TelegramCore/Sources/State/AvailableReactions.swift @@ -314,7 +314,8 @@ public final class AvailableReactions: Equatable, Codable { private extension AvailableReactions.Reaction { convenience init?(apiReaction: Api.AvailableReaction) { switch apiReaction { - case let .availableReaction(flags, reaction, title, staticIcon, appearAnimation, selectAnimation, activateAnimation, effectAnimation, aroundAnimation, centerIcon): + case let .availableReaction(availableReactionData): + let (flags, reaction, title, staticIcon, appearAnimation, selectAnimation, activateAnimation, effectAnimation, aroundAnimation, centerIcon) = (availableReactionData.flags, availableReactionData.reaction, availableReactionData.title, availableReactionData.staticIcon, availableReactionData.appearAnimation, availableReactionData.selectAnimation, availableReactionData.activateAnimation, availableReactionData.effectAnimation, availableReactionData.aroundAnimation, availableReactionData.centerIcon) guard let staticIconFile = telegramMediaFileFromApiDocument(staticIcon, altDocuments: []) else { return nil } diff --git a/submodules/TelegramCore/Sources/State/ChannelBoost.swift b/submodules/TelegramCore/Sources/State/ChannelBoost.swift index 6284735408..2d6713453b 100644 --- a/submodules/TelegramCore/Sources/State/ChannelBoost.swift +++ b/submodules/TelegramCore/Sources/State/ChannelBoost.swift @@ -279,7 +279,8 @@ private final class ChannelBoostersContextImpl { var resultBoosts: [ChannelBoostersContext.State.Boost] = [] for boost in boosts { switch boost { - case let .boost(flags, id, userId, giveawayMessageId, date, expires, usedGiftSlug, multiplier, stars): + case let .boost(boostData): + let (flags, id, userId, giveawayMessageId, date, expires, usedGiftSlug, multiplier, stars) = (boostData.flags, boostData.id, boostData.userId, boostData.giveawayMsgId, boostData.date, boostData.expires, boostData.usedGiftSlug, boostData.multiplier, boostData.stars) var boostFlags: ChannelBoostersContext.State.Boost.Flags = [] var boostPeer: EnginePeer? if let userId = userId { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/AttachMenuBots.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/AttachMenuBots.swift index 0528e3b4cc..79364ca1a8 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/AttachMenuBots.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/AttachMenuBots.swift @@ -749,7 +749,8 @@ func _internal_getBotApp(account: Account, reference: BotAppReference) -> Signal switch result { case let .botApp(botAppFlags, app): switch app { - case let .botApp(flags, id, accessHash, shortName, title, description, photo, document, hash): + case let .botApp(botAppData): + let (flags, id, accessHash, shortName, title, description, photo, document, hash) = (botAppData.flags, botAppData.id, botAppData.accessHash, botAppData.shortName, botAppData.title, botAppData.description, botAppData.photo, botAppData.document, botAppData.hash) let _ = flags var appFlags = BotApp.Flags() if (botAppFlags & (1 << 0)) != 0 { @@ -775,7 +776,8 @@ func _internal_getBotApp(account: Account, reference: BotAppReference) -> Signal extension BotApp { convenience init?(apiBotApp: Api.BotApp) { switch apiBotApp { - case let .botApp(_, id, accessHash, shortName, title, description, photo, document, hash): + case let .botApp(botAppData): + let (id, accessHash, shortName, title, description, photo, document, hash) = (botAppData.id, botAppData.accessHash, botAppData.shortName, botAppData.title, botAppData.description, botAppData.photo, botAppData.document, botAppData.hash) self.init(id: id, accessHash: accessHash, shortName: shortName, title: title, description: description, photo: telegramMediaImageFromApiPhoto(photo), document: document.flatMap { telegramMediaFileFromApiDocument($0, altDocuments: []) }, hash: hash, flags: []) case .botAppNotModified: return nil diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/BankCards.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/BankCards.swift index 7490c0b206..7cf49c0538 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/BankCards.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/BankCards.swift @@ -40,7 +40,8 @@ public struct BankCardInfo { extension BankCardUrl { init(apiBankCardOpenUrl: Api.BankCardOpenUrl) { switch apiBankCardOpenUrl { - case let .bankCardOpenUrl(url, name): + case let .bankCardOpenUrl(bankCardOpenUrlData): + let (url, name) = (bankCardOpenUrlData.url, bankCardOpenUrlData.name) self.title = name self.url = url } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/Birthdays.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/Birthdays.swift index 31253d0ed9..f339b2f85e 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/Birthdays.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/Birthdays.swift @@ -59,7 +59,8 @@ public final class TelegramBirthday: Codable, Equatable { extension TelegramBirthday { convenience init(apiBirthday: Api.Birthday) { switch apiBirthday { - case let .birthday(_, day, month, year): + case let .birthday(birthdayData): + let (_, day, month, year) = (birthdayData.flags, birthdayData.day, birthdayData.month, birthdayData.year) self.init( day: day, month: month, @@ -73,7 +74,7 @@ extension TelegramBirthday { if let _ = self.year { flags |= (1 << 0) } - return .birthday(flags: flags, day: self.day, month: self.month, year: self.year) + return .birthday(Api.Birthday.Cons_birthday(flags: flags, day: self.day, month: self.month, year: self.year)) } }