From abe7bc8fe20ee003260fcb41c3927f5f3a88dc84 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Tue, 26 Mar 2024 00:49:13 +0400 Subject: [PATCH] Update API --- .../TelegramEngine/Messages/ReportAds.swift | 52 +++++++++++++++++++ .../Messages/TelegramEngineMessages.swift | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 submodules/TelegramCore/Sources/TelegramEngine/Messages/ReportAds.swift diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/ReportAds.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/ReportAds.swift new file mode 100644 index 0000000000..f6b1ed6964 --- /dev/null +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/ReportAds.swift @@ -0,0 +1,52 @@ +import Foundation +import Postbox +import SwiftSignalKit +import TelegramApi + +public enum ReportAdMessageResult { + public struct Option { + let text: String + let option: Data + } + + case options(title: String, options: [Option]) + case adsHidden + case reported +} + +public enum ReportAdMessageError { + case generic + case premiumRequired +} + +func _internal_reportAdMessage(account: Account, peerId: EnginePeer.Id, opaqueId: Data, option: Data?) -> Signal { + return account.postbox.transaction { transaction -> Signal in + guard let peer = transaction.getPeer(peerId), let inputChannel = apiInputChannel(peer) else { + return .fail(.generic) + } + return account.network.request(Api.functions.channels.reportSponsoredMessage(channel: inputChannel, randomId: Buffer(data: opaqueId), option: Buffer(data: option))) + |> mapError { error -> ReportAdMessageError in + if error.errorDescription == "PREMIUM_ACCOUNT_REQUIRED" { + return .premiumRequired + } + return .generic + } + |> map { result -> ReportAdMessageResult in + switch result { + case let .sponsoredMessageReportResultChooseOption(title, options): + return .options(title: title, options: options.map { + switch $0 { + case let .sponsoredMessageReportOption(text, option): + return ReportAdMessageResult.Option(text: text, option: option.makeData()) + } + }) + case .sponsoredMessageReportResultAdsHidden: + return .adsHidden + case .sponsoredMessageReportResultReported: + return .reported + } + } + } + |> castError(ReportAdMessageError.self) + |> switchToLatest +} diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index d325726da8..de149ddb68 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -1402,5 +1402,9 @@ public extension TelegramEngine { transaction.reindexSavedMessagesCustomTagsWithTagsIfNeeded(peerId: self.account.peerId, threadId: threadId, tag: tag) }).startStandalone() } + + public func reportAdMessage(peerId: EnginePeer.Id, opaqueId: Data, option: Data?) -> Signal { + return _internal_reportAdMessage(account: self.account, peerId: peerId, opaqueId: opaqueId, option: option) + } } }