From 7fab450bec2cf5120059a6a02d6205456d20c2ce Mon Sep 17 00:00:00 2001 From: Mike Renoir <> Date: Fri, 20 Oct 2023 14:59:46 +0400 Subject: [PATCH 1/2] make optional value for updatePeerNameColorAndEmoji --- .../Sources/TelegramEngine/Peers/TelegramEnginePeers.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 0f759e5fec..a1e30e3e73 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -710,7 +710,7 @@ public extension TelegramEngine { return _internal_updateBotAbout(account: self.account, peerId: peerId, about: about) } - public func updatePeerNameColorAndEmoji(peerId: EnginePeer.Id, nameColor: PeerNameColor, backgroundEmojiId: Int64) -> Signal { + public func updatePeerNameColorAndEmoji(peerId: EnginePeer.Id, nameColor: PeerNameColor, backgroundEmojiId: Int64?) -> Signal { return _internal_updatePeerNameColorAndEmoji(account: self.account, peerId: peerId, nameColor: nameColor, backgroundEmojiId: backgroundEmojiId) } From d071e0873058b5cedcf0ce233dfa56a6f5ae5cd2 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Fri, 20 Oct 2023 15:02:32 +0400 Subject: [PATCH 2/2] Update API [skip ci] --- submodules/TelegramApi/Sources/Api0.swift | 6 +- submodules/TelegramApi/Sources/Api1.swift | 18 ++-- submodules/TelegramApi/Sources/Api29.swift | 8 +- submodules/TelegramApi/Sources/Api3.swift | 12 +-- submodules/TelegramApi/Sources/Api31.swift | 10 +- .../Sources/State/ChannelBoost.swift | 95 +++++++++++-------- .../TelegramEngine/Peers/JoinLink.swift | 2 +- .../Peers/TelegramEnginePeers.swift | 2 +- .../TelegramEngine/Peers/UpdatePeerInfo.swift | 48 ++++++---- 9 files changed, 117 insertions(+), 84 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index 1baaf00d8c..b1797c5ec8 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -75,7 +75,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[1834973166] = { return Api.BaseTheme.parse_baseThemeTinted($0) } dict[-1132882121] = { return Api.Bool.parse_boolFalse($0) } dict[-1720552011] = { return Api.Bool.parse_boolTrue($0) } - dict[1405288648] = { return Api.Boost.parse_boost($0) } + dict[706514033] = { return Api.Boost.parse_boost($0) } dict[-1778593322] = { return Api.BotApp.parse_botApp($0) } dict[1571189943] = { return Api.BotApp.parse_botAppNotModified($0) } dict[-1032140601] = { return Api.BotCommand.parse_botCommand($0) } @@ -176,7 +176,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1626209256] = { return Api.ChatBannedRights.parse_chatBannedRights($0) } dict[1915758525] = { return Api.ChatFull.parse_channelFull($0) } dict[-908914376] = { return Api.ChatFull.parse_chatFull($0) } - dict[808708181] = { return Api.ChatInvite.parse_chatInvite($0) } + dict[-840897472] = { return Api.ChatInvite.parse_chatInvite($0) } dict[1516793212] = { return Api.ChatInvite.parse_chatInviteAlready($0) } dict[1634294960] = { return Api.ChatInvite.parse_chatInvitePeek($0) } dict[-1940201511] = { return Api.ChatInviteImporter.parse_chatInviteImporter($0) } @@ -1181,7 +1181,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1916114267] = { return Api.photos.Photos.parse_photos($0) } dict[352657236] = { return Api.photos.Photos.parse_photosSlice($0) } dict[-2030542532] = { return Api.premium.BoostsList.parse_boostsList($0) } - dict[1029548774] = { return Api.premium.BoostsStatus.parse_boostsStatus($0) } + dict[1230586490] = { return Api.premium.BoostsStatus.parse_boostsStatus($0) } dict[-1696454430] = { return Api.premium.MyBoosts.parse_myBoosts($0) } dict[-1107852396] = { return Api.stats.BroadcastStats.parse_broadcastStats($0) } dict[-276825834] = { return Api.stats.MegagroupStats.parse_megagroupStats($0) } diff --git a/submodules/TelegramApi/Sources/Api1.swift b/submodules/TelegramApi/Sources/Api1.swift index c9f8f98538..f223f6a5e6 100644 --- a/submodules/TelegramApi/Sources/Api1.swift +++ b/submodules/TelegramApi/Sources/Api1.swift @@ -894,13 +894,13 @@ 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?) + case boost(flags: Int32, id: String, userId: Int64?, giveawayMsgId: Int32?, date: Int32, expires: Int32, usedGiftSlug: String?, multiplier: Int32?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .boost(let flags, let id, let userId, let giveawayMsgId, let date, let expires, let usedGiftSlug): + case .boost(let flags, let id, let userId, let giveawayMsgId, let date, let expires, let usedGiftSlug, let multiplier): if boxed { - buffer.appendInt32(1405288648) + buffer.appendInt32(706514033) } serializeInt32(flags, buffer: buffer, boxed: false) serializeString(id, buffer: buffer, boxed: false) @@ -909,14 +909,15 @@ public extension Api { serializeInt32(date, buffer: buffer, boxed: false) serializeInt32(expires, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 4) != 0 {serializeString(usedGiftSlug!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 5) != 0 {serializeInt32(multiplier!, buffer: buffer, boxed: false)} break } } public func descriptionFields() -> (String, [(String, Any)]) { switch self { - case .boost(let flags, let id, let userId, let giveawayMsgId, let date, let expires, let usedGiftSlug): - return ("boost", [("flags", flags as Any), ("id", id as Any), ("userId", userId as Any), ("giveawayMsgId", giveawayMsgId as Any), ("date", date as Any), ("expires", expires as Any), ("usedGiftSlug", usedGiftSlug as Any)]) + case .boost(let flags, let id, let userId, let giveawayMsgId, let date, let expires, let usedGiftSlug, let multiplier): + return ("boost", [("flags", flags as Any), ("id", id as Any), ("userId", userId as Any), ("giveawayMsgId", giveawayMsgId as Any), ("date", date as Any), ("expires", expires as Any), ("usedGiftSlug", usedGiftSlug as Any), ("multiplier", multiplier as Any)]) } } @@ -935,6 +936,8 @@ public extension Api { _6 = reader.readInt32() var _7: String? if Int(_1!) & Int(1 << 4) != 0 {_7 = parseString(reader) } + var _8: Int32? + if Int(_1!) & Int(1 << 5) != 0 {_8 = reader.readInt32() } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = (Int(_1!) & Int(1 << 0) == 0) || _3 != nil @@ -942,8 +945,9 @@ public extension Api { let _c5 = _5 != nil let _c6 = _6 != nil let _c7 = (Int(_1!) & Int(1 << 4) == 0) || _7 != nil - if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 && _c7 { - return Api.Boost.boost(flags: _1!, id: _2!, userId: _3, giveawayMsgId: _4, date: _5!, expires: _6!, usedGiftSlug: _7) + let _c8 = (Int(_1!) & Int(1 << 5) == 0) || _8 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 && _c7 && _c8 { + return Api.Boost.boost(flags: _1!, id: _2!, userId: _3, giveawayMsgId: _4, date: _5!, expires: _6!, usedGiftSlug: _7, multiplier: _8) } else { return nil diff --git a/submodules/TelegramApi/Sources/Api29.swift b/submodules/TelegramApi/Sources/Api29.swift index 0502381f99..e4681269b7 100644 --- a/submodules/TelegramApi/Sources/Api29.swift +++ b/submodules/TelegramApi/Sources/Api29.swift @@ -70,13 +70,13 @@ public extension Api.premium { switch self { case .boostsStatus(let flags, let level, let currentLevelBoosts, let boosts, let giftBoosts, let nextLevelBoosts, let premiumAudience, let boostUrl, let prepaidGiveaways, let myBoostSlots): if boxed { - buffer.appendInt32(1029548774) + buffer.appendInt32(1230586490) } serializeInt32(flags, buffer: buffer, boxed: false) serializeInt32(level, buffer: buffer, boxed: false) serializeInt32(currentLevelBoosts, buffer: buffer, boxed: false) serializeInt32(boosts, buffer: buffer, boxed: false) - if Int(flags) & Int(1 << 3) != 0 {serializeInt32(giftBoosts!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 4) != 0 {serializeInt32(giftBoosts!, buffer: buffer, boxed: false)} if Int(flags) & Int(1 << 0) != 0 {serializeInt32(nextLevelBoosts!, buffer: buffer, boxed: false)} if Int(flags) & Int(1 << 1) != 0 {premiumAudience!.serialize(buffer, true)} serializeString(boostUrl, buffer: buffer, boxed: false) @@ -111,7 +111,7 @@ public extension Api.premium { var _4: Int32? _4 = reader.readInt32() var _5: Int32? - if Int(_1!) & Int(1 << 3) != 0 {_5 = reader.readInt32() } + if Int(_1!) & Int(1 << 4) != 0 {_5 = reader.readInt32() } var _6: Int32? if Int(_1!) & Int(1 << 0) != 0 {_6 = reader.readInt32() } var _7: Api.StatsPercentValue? @@ -132,7 +132,7 @@ public extension Api.premium { let _c2 = _2 != nil let _c3 = _3 != nil let _c4 = _4 != nil - let _c5 = (Int(_1!) & Int(1 << 3) == 0) || _5 != nil + let _c5 = (Int(_1!) & Int(1 << 4) == 0) || _5 != nil let _c6 = (Int(_1!) & Int(1 << 0) == 0) || _6 != nil let _c7 = (Int(_1!) & Int(1 << 1) == 0) || _7 != nil let _c8 = _8 != nil diff --git a/submodules/TelegramApi/Sources/Api3.swift b/submodules/TelegramApi/Sources/Api3.swift index d192bd167e..b3d8cbead5 100644 --- a/submodules/TelegramApi/Sources/Api3.swift +++ b/submodules/TelegramApi/Sources/Api3.swift @@ -1238,7 +1238,7 @@ public extension Api { } public extension Api { indirect enum ChatInvite: TypeConstructorDescription { - case chatInvite(flags: Int32, title: String, about: String?, photo: Api.Photo, participantsCount: Int32, participants: [Api.User]?, color: Int32?) + case chatInvite(flags: Int32, title: String, about: String?, photo: Api.Photo, participantsCount: Int32, participants: [Api.User]?, color: Int32) case chatInviteAlready(chat: Api.Chat) case chatInvitePeek(chat: Api.Chat, expires: Int32) @@ -1246,7 +1246,7 @@ public extension Api { switch self { case .chatInvite(let flags, let title, let about, let photo, let participantsCount, let participants, let color): if boxed { - buffer.appendInt32(808708181) + buffer.appendInt32(-840897472) } serializeInt32(flags, buffer: buffer, boxed: false) serializeString(title, buffer: buffer, boxed: false) @@ -1258,7 +1258,7 @@ public extension Api { for item in participants! { item.serialize(buffer, true) }} - if Int(flags) & Int(1 << 10) != 0 {serializeInt32(color!, buffer: buffer, boxed: false)} + serializeInt32(color, buffer: buffer, boxed: false) break case .chatInviteAlready(let chat): if boxed { @@ -1305,16 +1305,16 @@ public extension Api { _6 = Api.parseVector(reader, elementSignature: 0, elementType: Api.User.self) } } var _7: Int32? - if Int(_1!) & Int(1 << 10) != 0 {_7 = reader.readInt32() } + _7 = reader.readInt32() let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = (Int(_1!) & Int(1 << 5) == 0) || _3 != nil let _c4 = _4 != nil let _c5 = _5 != nil let _c6 = (Int(_1!) & Int(1 << 4) == 0) || _6 != nil - let _c7 = (Int(_1!) & Int(1 << 10) == 0) || _7 != nil + let _c7 = _7 != nil if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 && _c7 { - return Api.ChatInvite.chatInvite(flags: _1!, title: _2!, about: _3, photo: _4!, participantsCount: _5!, participants: _6, color: _7) + return Api.ChatInvite.chatInvite(flags: _1!, title: _2!, about: _3, photo: _4!, participantsCount: _5!, participants: _6, color: _7!) } else { return nil diff --git a/submodules/TelegramApi/Sources/Api31.swift b/submodules/TelegramApi/Sources/Api31.swift index daefae63d9..881d3faef4 100644 --- a/submodules/TelegramApi/Sources/Api31.swift +++ b/submodules/TelegramApi/Sources/Api31.swift @@ -8377,9 +8377,9 @@ public extension Api.functions.photos { } } public extension Api.functions.premium { - static func applyBoost(flags: Int32, slots: [Int32]?, peer: Api.InputPeer) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + static func applyBoost(flags: Int32, slots: [Int32]?, peer: Api.InputPeer) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() - buffer.appendInt32(407618489) + buffer.appendInt32(1803396934) serializeInt32(flags, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 0) != 0 {buffer.appendInt32(481674261) buffer.appendInt32(Int32(slots!.count)) @@ -8387,11 +8387,11 @@ public extension Api.functions.premium { serializeInt32(item, buffer: buffer, boxed: false) }} peer.serialize(buffer, true) - return (FunctionDescription(name: "premium.applyBoost", parameters: [("flags", String(describing: flags)), ("slots", String(describing: slots)), ("peer", String(describing: peer))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in + return (FunctionDescription(name: "premium.applyBoost", parameters: [("flags", String(describing: flags)), ("slots", String(describing: slots)), ("peer", String(describing: peer))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.premium.MyBoosts? in let reader = BufferReader(buffer) - var result: Api.Bool? + var result: Api.premium.MyBoosts? if let signature = reader.readInt32() { - result = Api.parse(reader, signature: signature) as? Api.Bool + result = Api.parse(reader, signature: signature) as? Api.premium.MyBoosts } return result }) diff --git a/submodules/TelegramCore/Sources/State/ChannelBoost.swift b/submodules/TelegramCore/Sources/State/ChannelBoost.swift index e2faa5d636..caa5ded53e 100644 --- a/submodules/TelegramCore/Sources/State/ChannelBoost.swift +++ b/submodules/TelegramCore/Sources/State/ChannelBoost.swift @@ -96,13 +96,13 @@ func _internal_getChannelBoostStatus(account: Account, peerId: PeerId) -> Signal } } -func _internal_applyChannelBoost(account: Account, peerId: PeerId, slots: [Int32]) -> Signal { +func _internal_applyChannelBoost(account: Account, peerId: PeerId, slots: [Int32]) -> Signal { return account.postbox.transaction { transaction -> Api.InputPeer? in return transaction.getPeer(peerId).flatMap(apiInputPeer) } - |> mapToSignal { inputPeer -> Signal in + |> mapToSignal { inputPeer -> Signal in guard let inputPeer = inputPeer else { - return .single(false) + return .complete() } var flags: Int32 = 0 if !slots.isEmpty { @@ -110,14 +110,18 @@ func _internal_applyChannelBoost(account: Account, peerId: PeerId, slots: [Int32 } return account.network.request(Api.functions.premium.applyBoost(flags: flags, slots: !slots.isEmpty ? slots : nil, peer: inputPeer)) - |> `catch` { error -> Signal in - return .single(.boolFalse) + |> map (Optional.init) + |> `catch` { error -> Signal in + return .complete() } - |> map { result -> Bool in - if case .boolTrue = result { - return true + |> mapToSignal { result -> Signal in + if let result = result { + return account.postbox.transaction { transaction -> MyBoostStatus? in + return MyBoostStatus(apiMyBoostStatus: result, accountPeerId: account.peerId, transaction: transaction) + } + } else { + return .single(nil) } - return false } } } @@ -133,24 +137,7 @@ func _internal_getMyBoostStatus(account: Account) -> Signal MyBoostStatus? in - var boostsResult: [MyBoostStatus.Boost] = [] - switch result { - case let .myBoosts(myBoosts, chats, users): - let parsedPeers = AccumulatedPeers(transaction: transaction, chats: chats, users: users) - updatePeers(transaction: transaction, accountPeerId: account.peerId, peers: parsedPeers) - for boost in myBoosts { - let _ = boost - switch boost { - case let .myBoost(_, slot, peer, date, expires, cooldownUntilDate): - var boostPeer: EnginePeer? - if let peerId = peer?.peerId, let peer = transaction.getPeer(peerId) { - boostPeer = EnginePeer(peer) - } - boostsResult.append(MyBoostStatus.Boost(slot: slot, peer: boostPeer, date: date, expires: expires, cooldownUntil: cooldownUntilDate)) - } - } - } - return MyBoostStatus(boosts: boostsResult) + return MyBoostStatus(apiMyBoostStatus: result, accountPeerId: account.peerId, transaction: transaction) } } } @@ -159,6 +146,7 @@ private final class ChannelBoostersContextImpl { private let queue: Queue private let account: Account private let peerId: PeerId + private let gift: Bool private let disposable = MetaDisposable() private let updateDisposables = DisposableSet() private var isLoadingMore: Bool = false @@ -172,21 +160,22 @@ private final class ChannelBoostersContextImpl { let state = Promise() - init(queue: Queue, account: Account, peerId: PeerId) { + init(queue: Queue, account: Account, peerId: PeerId, gift: Bool) { self.queue = queue self.account = account self.peerId = peerId + self.gift = gift self.count = 0 self.isLoadingMore = true self.disposable.set((account.postbox.transaction { transaction -> (peers: [ChannelBoostersContext.State.Boost], count: Int32, canLoadMore: Bool)? in let cachedResult = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedChannelBoosts, key: CachedChannelBoosters.key(peerId: peerId)))?.get(CachedChannelBoosters.self) - if let cachedResult = cachedResult { + if let cachedResult = cachedResult, !gift { var result: [ChannelBoostersContext.State.Boost] = [] for boost in cachedResult.boosts { let peer = boost.peerId.flatMap { transaction.getPeer($0) } - result.append(ChannelBoostersContext.State.Boost(flags: ChannelBoostersContext.State.Boost.Flags(rawValue: boost.flags), id: boost.id, peer: peer.flatMap { EnginePeer($0) }, date: boost.date, expires: boost.expires)) + result.append(ChannelBoostersContext.State.Boost(flags: ChannelBoostersContext.State.Boost.Flags(rawValue: boost.flags), id: boost.id, peer: peer.flatMap { EnginePeer($0) }, date: boost.date, expires: boost.expires, multiplier: boost.multiplier)) } return (result, cachedResult.count, true) } else { @@ -229,6 +218,7 @@ private final class ChannelBoostersContextImpl { let account = self.account let accountPeerId = account.peerId let peerId = self.peerId + let gift = self.gift let populateCache = self.populateCache if self.loadedFromCache { @@ -244,7 +234,10 @@ private final class ChannelBoostersContextImpl { let offset = lastOffset ?? "" let limit: Int32 = lastOffset == nil ? 25 : 50 - let flags: Int32 = 0 + var flags: Int32 = 0 + if gift { + flags |= (1 << 0) + } let signal = account.network.request(Api.functions.premium.getBoostsList(flags: flags, peer: inputPeer, offset: offset, limit: limit)) |> map(Optional.init) |> `catch` { _ -> Signal in @@ -261,7 +254,7 @@ 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): + case let .boost(flags, id, userId, giveawayMessageId, date, expires, usedGiftSlug, multiplier): let _ = giveawayMessageId let _ = usedGiftSlug var boostFlags: ChannelBoostersContext.State.Boost.Flags = [] @@ -281,7 +274,7 @@ private final class ChannelBoostersContextImpl { if (flags & (1 << 3)) != 0 { boostFlags.insert(.isUnclaimed) } - resultBoosts.append(ChannelBoostersContext.State.Boost(flags: boostFlags, id: id, peer: boostPeer, date: date, expires: expires)) + resultBoosts.append(ChannelBoostersContext.State.Boost(flags: boostFlags, id: id, peer: boostPeer, date: date, expires: expires, multiplier: multiplier ?? 1)) } } if populateCache { @@ -363,6 +356,7 @@ public final class ChannelBoostersContext { public var peer: EnginePeer? public var date: Int32 public var expires: Int32 + public var multiplier: Int32 } public var boosts: [Boost] public var isLoadingMore: Bool @@ -390,10 +384,10 @@ public final class ChannelBoostersContext { } } - public init(account: Account, peerId: PeerId) { + public init(account: Account, peerId: PeerId, gift: Bool) { let queue = self.queue self.impl = QueueLocalObject(queue: queue, generate: { - return ChannelBoostersContextImpl(queue: queue, account: account, peerId: peerId) + return ChannelBoostersContextImpl(queue: queue, account: account, peerId: peerId, gift: gift) }) } @@ -423,6 +417,7 @@ private final class CachedChannelBoosters: Codable { case peerId case date case expires + case multiplier } var flags: Int32 @@ -430,13 +425,15 @@ private final class CachedChannelBoosters: Codable { var peerId: EnginePeer.Id? var date: Int32 var expires: Int32 + var multiplier: Int32 - init(flags: Int32, id: String, peerId: EnginePeer.Id?, date: Int32, expires: Int32) { + init(flags: Int32, id: String, peerId: EnginePeer.Id?, date: Int32, expires: Int32, multiplier: Int32) { self.flags = flags self.id = id self.peerId = peerId self.date = date self.expires = expires + self.multiplier = multiplier } init(from decoder: Decoder) throws { @@ -447,6 +444,7 @@ private final class CachedChannelBoosters: Codable { self.peerId = try container.decodeIfPresent(Int64.self, forKey: .peerId).flatMap { EnginePeer.Id($0) } self.date = try container.decode(Int32.self, forKey: .date) self.expires = try container.decode(Int32.self, forKey: .expires) + self.multiplier = try container.decode(Int32.self, forKey: .multiplier) } func encode(to encoder: Encoder) throws { @@ -457,6 +455,7 @@ private final class CachedChannelBoosters: Codable { try container.encodeIfPresent(self.peerId?.toInt64(), forKey: .peerId) try container.encode(self.date, forKey: .date) try container.encode(self.expires, forKey: .expires) + try container.encode(self.multiplier, forKey: .multiplier) } } @@ -470,7 +469,7 @@ private final class CachedChannelBoosters: Codable { } init(boosts: [ChannelBoostersContext.State.Boost], count: Int32) { - self.boosts = boosts.map { CachedBoost(flags: $0.flags.rawValue, id: $0.id, peerId: $0.peer?.id, date: $0.date, expires: $0.expires) } + self.boosts = boosts.map { CachedBoost(flags: $0.flags.rawValue, id: $0.id, peerId: $0.peer?.id, date: $0.date, expires: $0.expires, multiplier: $0.multiplier) } self.count = count } @@ -488,3 +487,25 @@ private final class CachedChannelBoosters: Codable { try container.encode(self.count, forKey: .count) } } + +extension MyBoostStatus { + init(apiMyBoostStatus: Api.premium.MyBoosts, accountPeerId: PeerId, transaction: Transaction) { + var boostsResult: [MyBoostStatus.Boost] = [] + switch apiMyBoostStatus { + case let .myBoosts(myBoosts, chats, users): + let parsedPeers = AccumulatedPeers(transaction: transaction, chats: chats, users: users) + updatePeers(transaction: transaction, accountPeerId: accountPeerId, peers: parsedPeers) + for boost in myBoosts { + switch boost { + case let .myBoost(_, slot, peer, date, expires, cooldownUntilDate): + var boostPeer: EnginePeer? + if let peerId = peer?.peerId, let peer = transaction.getPeer(peerId) { + boostPeer = EnginePeer(peer) + } + boostsResult.append(MyBoostStatus.Boost(slot: slot, peer: boostPeer, date: date, expires: expires, cooldownUntil: cooldownUntilDate)) + } + } + } + self.boosts = boostsResult + } +} diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift index fd0c211218..be5f03b5a7 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift @@ -109,7 +109,7 @@ func _internal_joinLinkInformation(_ hash: String, account: Account) -> Signal ExternalJoiningChatState in diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 0f759e5fec..90dd49613e 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -1201,7 +1201,7 @@ public extension TelegramEngine { return _internal_getMyBoostStatus(account: self.account) } - public func applyChannelBoost(peerId: EnginePeer.Id, slots: [Int32]) -> Signal { + public func applyChannelBoost(peerId: EnginePeer.Id, slots: [Int32]) -> Signal { return _internal_applyChannelBoost(account: self.account, peerId: peerId, slots: slots) } } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/UpdatePeerInfo.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/UpdatePeerInfo.swift index f3972e8ab3..86677c1743 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/UpdatePeerInfo.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/UpdatePeerInfo.swift @@ -95,29 +95,37 @@ public enum UpdatePeerNameColorAndEmojiError { func _internal_updatePeerNameColorAndEmoji(account: Account, peerId: EnginePeer.Id, nameColor: PeerNameColor, backgroundEmojiId: Int64?) -> Signal { let accountPeerId = account.peerId - return account.postbox.transaction { transaction -> Signal in - if let peer = transaction.getPeer(peerId) { - if let peer = peer as? TelegramChannel, let inputChannel = apiInputChannel(peer) { - let flags: Int32 = (1 << 0) - return account.network.request(Api.functions.channels.updateColor(flags: flags, channel: inputChannel, color: nameColor.rawValue, backgroundEmojiId: backgroundEmojiId ?? 0)) - |> mapError { _ -> UpdatePeerNameColorAndEmojiError in - return .generic + + return account.postbox.transaction { transaction -> Signal in + guard let peer = transaction.getPeer(account.peerId) as? TelegramChannel else { + return .complete() + } + updatePeersCustom(transaction: transaction, peers: [peer.withUpdatedNameColor(nameColor).withUpdatedBackgroundEmojiId(backgroundEmojiId)], update: { _, updated in + return updated + }) + return .single(peer) + } + |> switchToLatest + |> castError(UpdatePeerNameColorAndEmojiError.self) + |> mapToSignal { peer -> Signal in + if let peer = peer as? TelegramChannel, let inputChannel = apiInputChannel(peer) { + let flags: Int32 = (1 << 0) + return account.network.request(Api.functions.channels.updateColor(flags: flags, channel: inputChannel, color: nameColor.rawValue, backgroundEmojiId: backgroundEmojiId ?? 0)) + |> mapError { _ -> UpdatePeerNameColorAndEmojiError in + return .generic + } + |> mapToSignal { result -> Signal in + account.stateManager.addUpdates(result) + + return account.postbox.transaction { transaction -> Void in + if let apiChat = apiUpdatesGroups(result).first { + let parsedPeers = AccumulatedPeers(transaction: transaction, chats: [apiChat], users: []) + updatePeers(transaction: transaction, accountPeerId: accountPeerId, peers: parsedPeers) } - |> mapToSignal { result -> Signal in - account.stateManager.addUpdates(result) - - return account.postbox.transaction { transaction -> Void in - if let apiChat = apiUpdatesGroups(result).first { - let parsedPeers = AccumulatedPeers(transaction: transaction, chats: [apiChat], users: []) - updatePeers(transaction: transaction, accountPeerId: accountPeerId, peers: parsedPeers) - } - } |> mapError { _ -> UpdatePeerNameColorAndEmojiError in } - } - } else { - return .fail(.generic) + } |> mapError { _ -> UpdatePeerNameColorAndEmojiError in } } } else { return .fail(.generic) } - } |> mapError { _ -> UpdatePeerNameColorAndEmojiError in } |> switchToLatest + } }