diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/CopyProtection.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/CopyProtection.swift index eeb9058f1a..0f88652490 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/CopyProtection.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/CopyProtection.swift @@ -4,15 +4,52 @@ import SwiftSignalKit import TelegramApi import MtProtoKit -func _internal_toggleMessageCopyProtection(account: Account, peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id?) -> Signal { - return account.postbox.transaction { transaction -> Signal in +public enum CopyProtectionResult { + case applied + case requested +} + +func _internal_toggleMessageCopyProtection(account: Account, peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id?) -> Signal { + return account.postbox.transaction { transaction -> Signal in if let peer = transaction.getPeer(peerId), let inputPeer = apiInputPeer(peer) { var flags: Int32 = 0 if let _ = requestMessageId { flags = (1 << 0) } - return account.network.request(Api.functions.messages.toggleNoForwards(flags: flags, peer: inputPeer, enabled: enabled ? .boolTrue : .boolFalse, requestMsgId: requestMessageId?.id)) |> `catch` { _ in .complete() } |> map { updates -> Void in + return account.network.request(Api.functions.messages.toggleNoForwards(flags: flags, peer: inputPeer, enabled: enabled ? .boolTrue : .boolFalse, requestMsgId: requestMessageId?.id)) |> `catch` { _ in .complete() } |> mapToSignal { updates -> Signal in account.stateManager.addUpdates(updates) + + var isRequest = false + switch updates { + case let .updates(data): + for update in data.updates { + if case let .updateNewMessage(msgData) = update, case let .messageService(serviceData) = msgData.message, case .messageActionNoForwardsRequest = serviceData.action { + isRequest = true + } + } + default: + break + } + + if !isRequest { + return account.postbox.transaction { transaction -> CopyProtectionResult in + transaction.updatePeerCachedData(peerIds: [peerId], update: { _, current in + if let previous = current as? CachedUserData { + var updatedFlags = previous.flags + if enabled { + updatedFlags.insert(.copyProtectionEnabled) + } else { + updatedFlags.remove(.copyProtectionEnabled) + } + return previous.withUpdatedFlags(updatedFlags) + } + return current + }) + return .applied + } + } else { + return .single(.requested) + } } } else { return .complete() diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 8e0d5d238a..4b3dfda9b2 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -505,7 +505,7 @@ public extension TelegramEngine { return _internal_toggleShouldChannelMessagesSignatures(account: self.account, peerId: peerId, signaturesEnabled: signaturesEnabled, profilesEnabled: profilesEnabled) } - public func toggleMessageCopyProtection(peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id? = nil) -> Signal { + public func toggleMessageCopyProtection(peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id? = nil) -> Signal { return _internal_toggleMessageCopyProtection(account: self.account, peerId: peerId, enabled: enabled, requestMessageId: requestMessageId) }