diff --git a/submodules/TelegramCore/Sources/Account/AccountManager.swift b/submodules/TelegramCore/Sources/Account/AccountManager.swift index 035ffaa88e..12531720b9 100644 --- a/submodules/TelegramCore/Sources/Account/AccountManager.swift +++ b/submodules/TelegramCore/Sources/Account/AccountManager.swift @@ -206,6 +206,7 @@ private var declaredEncodables: Void = { declareEncodable(TelegramMediaGiveawayResults.self, f: { TelegramMediaGiveawayResults(decoder: $0) }) declareEncodable(WebpagePreviewMessageAttribute.self, f: { WebpagePreviewMessageAttribute(decoder: $0) }) declareEncodable(DerivedDataMessageAttribute.self, f: { DerivedDataMessageAttribute(decoder: $0) }) + declareEncodable(TelegramApplicationIcons.self, f: { TelegramApplicationIcons(decoder: $0) }) return }() diff --git a/submodules/TelegramCore/Sources/MacOS/MacInternalUpdater.swift b/submodules/TelegramCore/Sources/MacOS/MacInternal.swift similarity index 73% rename from submodules/TelegramCore/Sources/MacOS/MacInternalUpdater.swift rename to submodules/TelegramCore/Sources/MacOS/MacInternal.swift index afa55aabb5..cc90b955a9 100644 --- a/submodules/TelegramCore/Sources/MacOS/MacInternalUpdater.swift +++ b/submodules/TelegramCore/Sources/MacOS/MacInternal.swift @@ -177,4 +177,75 @@ public func downloadAppUpdate(account: Account, source: String, messageId: Int32 } } +public func requestApplicationIcons(engine: TelegramEngine, source: String = "macos_app_icons") -> Signal { + return engine.peers.resolvePeerByName(name: source) + |> mapToSignal { result -> Signal in + switch result { + case .progress: + return .never() + case let .result(peer): + return .single(peer?._asPeer()) + } + } |> mapToSignal { peer -> Signal in + if let peer = peer, let inputPeer = apiInputPeer(peer) { + return engine.account.network.request(Api.functions.messages.getHistory(peer: inputPeer, offsetId: 0, offsetDate: 0, addOffset: 0, limit: 100, maxId: Int32.max, minId: 0, hash: 0)) + |> retryRequest + |> mapToSignal { result in + + switch result { + case let .channelMessages(_, _, _, _, apiMessages, _, apiChats, apiUsers): + var icons: [TelegramApplicationIcons.Icon] = [] + for apiMessage in apiMessages.reversed() { + if let storeMessage = StoreMessage(apiMessage: apiMessage, accountPeerId: engine.account.peerId, peerIsForum: peer.isForum) { + var peers: [PeerId: Peer] = [:] + for chat in apiChats { + if let groupOrChannel = parseTelegramGroupOrChannel(chat: chat) { + peers[groupOrChannel.id] = groupOrChannel + } + } + for user in apiUsers { + let telegramUser = TelegramUser(user: user) + peers[telegramUser.id] = telegramUser + } + + if let message = locallyRenderedMessage(message: storeMessage, peers: peers), let media = message.media.first as? TelegramMediaFile { + icons.append(.init(file: media, reference: MessageReference(message))) + } + } + } + + return _internal_updateApplicationIcons(postbox: engine.account.postbox, icons: .init(icons: icons)) + default: + break + } + return .complete() + } + } else { + return .complete() + } + } +} + + #endif + +/* + return Signal { subscriber in + let fetchDispsable = fetchedMediaResource(mediaBox: engine.account.postbox.mediaBox, userLocation: .other, userContentType: .other, reference: MediaResourceReference.media(media: AnyMediaReference.message(message: MessageReference(message), media: media), resource: media.resource)).start() + + let dataDisposable = engine.account.postbox.mediaBox.resourceData(media.resource, option: .complete(waitUntilFetchStatus: true)).start(next: { data in + if data.complete { + if let data = try? Data(contentsOf: URL.init(fileURLWithPath: data.path)) { + subscriber.putNext(data) + subscriber.putCompletion() + } else { + subscriber.putError(.xmlLoad) + } + } + }) + return ActionDisposable { + fetchDispsable.dispose() + dataDisposable.dispose() + } + } + */ diff --git a/submodules/TelegramCore/Sources/MacOS/TelegramApplicationIcons.swift b/submodules/TelegramCore/Sources/MacOS/TelegramApplicationIcons.swift new file mode 100644 index 0000000000..f86ff4f2bb --- /dev/null +++ b/submodules/TelegramCore/Sources/MacOS/TelegramApplicationIcons.swift @@ -0,0 +1,71 @@ +// +// File.swift +// +// +// Created by Mikhail Filimonov on 25.01.2024. +// + +import Foundation +import Postbox +import SwiftSignalKit + +public struct TelegramApplicationIcons : PostboxCoding, Equatable { + public init(decoder: PostboxDecoder) { + self.icons = (try? decoder.decodeObjectArrayWithCustomDecoderForKey("i", decoder: { Icon(decoder: $0) })) ?? [] + } + + public func encode(_ encoder: PostboxEncoder) { + encoder.encodeObjectArray(self.icons, forKey: "i") + } + + public struct Icon : PostboxCoding, Equatable { + public init(decoder: PostboxDecoder) { + self.file = decoder.decodeObjectForKey("f") as! TelegramMediaFile + self.reference = decoder.decodeObjectForKey("r", decoder: { MessageReference(decoder: $0) }) as! MessageReference + } + + public func encode(_ encoder: PostboxEncoder) { + encoder.encodeObject(self.file, forKey: "f") + encoder.encodeObject(self.reference, forKey: "r") + } + + public let file: TelegramMediaFile + public let reference: MessageReference + init(file: TelegramMediaFile, reference: MessageReference) { + self.file = file + self.reference = reference + } + } + public var icons: [Icon] + + init(icons: [Icon]) { + self.icons = icons + } + + static var entryId: ItemCacheEntryId { + let cacheKey = ValueBoxKey(length: 1) + cacheKey.setInt8(0, value: 0) + return ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.applicationIcons, key: cacheKey) + } +} + + +func _internal_applicationIcons(account: Account) -> Signal { + let key = PostboxViewKey.cachedItem(TelegramApplicationIcons.entryId) + return account.postbox.combinedView(keys: [key]) + |> mapToSignal { views -> Signal in + guard let icons = (views.views[key] as? CachedItemView)?.value?.getLegacy(TelegramApplicationIcons.self) as? TelegramApplicationIcons else { + return .single(.init(icons: [])) + } + return .single(icons) + } +} + +func _internal_updateApplicationIcons(postbox: Postbox, icons: TelegramApplicationIcons) -> Signal { + return postbox.transaction { transaction -> Void in + let entry = CodableEntry(legacyValue: icons) + transaction.putItemCacheEntry(id: TelegramApplicationIcons.entryId, entry: entry) + } +} + + diff --git a/submodules/TelegramCore/Sources/SyncCore/SyncCore_Namespaces.swift b/submodules/TelegramCore/Sources/SyncCore/SyncCore_Namespaces.swift index 2f0e5193ce..c61b94706a 100644 --- a/submodules/TelegramCore/Sources/SyncCore/SyncCore_Namespaces.swift +++ b/submodules/TelegramCore/Sources/SyncCore/SyncCore_Namespaces.swift @@ -120,6 +120,7 @@ public struct Namespaces { public static let recommendedChannels: Int8 = 33 public static let peerColorOptions: Int8 = 34 public static let savedMessageTags: Int8 = 35 + public static let applicationIcons: Int8 = 36 } public struct UnorderedItemList { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Resources/TelegramEngineResources.swift b/submodules/TelegramCore/Sources/TelegramEngine/Resources/TelegramEngineResources.swift index f49db12037..ded57ff0d6 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Resources/TelegramEngineResources.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Resources/TelegramEngineResources.swift @@ -412,5 +412,9 @@ public extension TelegramEngine { public func pushPriorityDownload(resourceId: String, priority: Int = 1) -> Disposable { return self.account.network.multiplexedRequestManager.pushPriority(resourceId: resourceId, priority: priority) } + + public func applicationIcons() -> Signal { + return _internal_applicationIcons(account: account) + } } }