copy protection improvements

This commit is contained in:
Mikhail Filimonov 2026-02-20 10:58:14 +04:00
parent a2413e4aa8
commit 670c547aad
2 changed files with 41 additions and 4 deletions

View file

@ -4,15 +4,52 @@ import SwiftSignalKit
import TelegramApi
import MtProtoKit
func _internal_toggleMessageCopyProtection(account: Account, peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id?) -> Signal<Void, NoError> {
return account.postbox.transaction { transaction -> Signal<Void, NoError> in
public enum CopyProtectionResult {
case applied
case requested
}
func _internal_toggleMessageCopyProtection(account: Account, peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id?) -> Signal<CopyProtectionResult, NoError> {
return account.postbox.transaction { transaction -> Signal<CopyProtectionResult, NoError> 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<CopyProtectionResult, NoError> 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()

View file

@ -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<Void, NoError> {
public func toggleMessageCopyProtection(peerId: PeerId, enabled: Bool, requestMessageId: EngineMessage.Id? = nil) -> Signal<CopyProtectionResult, NoError> {
return _internal_toggleMessageCopyProtection(account: self.account, peerId: peerId, enabled: enabled, requestMessageId: requestMessageId)
}