From b159e561336089f5e7ae3ba9c417b24f117b3757 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 21 Jun 2017 01:03:20 +0300 Subject: [PATCH] no message --- TelegramCore/Account.swift | 1 + .../ManagedSecretChatOutgoingOperations.swift | 43 ++++++- TelegramCore/RemovePeerChat.swift | 2 +- TelegramCore/ReportPeer.swift | 27 ++++- .../SecretChatOutgoingOperation.swift | 7 +- TelegramCore/SecretChatStateBridge.swift | 2 +- TelegramCore/TelegramSecretChat.swift | 30 +++++ TelegramCore/UpdateCachedPeerData.swift | 105 +++++++++++------- 8 files changed, 167 insertions(+), 50 deletions(-) diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index 06cc844f3e..01186e46f7 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -233,6 +233,7 @@ private var declaredEncodables: Void = { declareEncodable(SuggestedLocalizationEntry.self, f: { SuggestedLocalizationEntry(decoder: $0) }) declareEncodable(SynchronizeLocalizationUpdatesOperation.self, f: { SynchronizeLocalizationUpdatesOperation(decoder: $0) }) declareEncodable(ChannelMessageStateVersionAttribute.self, f: { ChannelMessageStateVersionAttribute(decoder: $0) }) + declareEncodable(CachedSecretChatData.self, f: { CachedSecretChatData(decoder: $0) }) return }() diff --git a/TelegramCore/ManagedSecretChatOutgoingOperations.swift b/TelegramCore/ManagedSecretChatOutgoingOperations.swift index 8124aced10..e0b5ce1bb8 100644 --- a/TelegramCore/ManagedSecretChatOutgoingOperations.swift +++ b/TelegramCore/ManagedSecretChatOutgoingOperations.swift @@ -156,8 +156,8 @@ func managedSecretChatOutgoingOperations(postbox: Postbox, network: Network) -> return sendServiceActionMessage(postbox: postbox, network: network, peerId: entry.peerId, action: .resendOperations(layer: layer, actionGloballyUniqueId: actionGloballyUniqueId, fromSeqNo: fromSeqNo, toSeqNo: toSeqNo), tagLocalIndex: entry.tagLocalIndex, wasDelivered: operation.delivered) case let .screenshotMessages(layer, actionGloballyUniqueId, globallyUniqueIds): return sendServiceActionMessage(postbox: postbox, network: network, peerId: entry.peerId, action: .screenshotMessages(layer: layer, actionGloballyUniqueId: actionGloballyUniqueId, globallyUniqueIds: globallyUniqueIds), tagLocalIndex: entry.tagLocalIndex, wasDelivered: operation.delivered) - case .terminate: - return requestTerminateSecretChat(postbox: postbox, network: network, peerId: entry.peerId, tagLocalIndex: entry.tagLocalIndex) + case let .terminate(reportSpam): + return requestTerminateSecretChat(postbox: postbox, network: network, peerId: entry.peerId, tagLocalIndex: entry.tagLocalIndex, reportSpam: reportSpam) } } else { assertionFailure() @@ -831,12 +831,49 @@ private func sendBoxedDecryptedMessage(postbox: Postbox, network: Network, peer: } } -private func requestTerminateSecretChat(postbox: Postbox, network: Network, peerId: PeerId, tagLocalIndex: Int32) -> Signal { +private func requestTerminateSecretChat(postbox: Postbox, network: Network, peerId: PeerId, tagLocalIndex: Int32, reportSpam: Bool) -> Signal { return network.request(Api.functions.messages.discardEncryption(chatId: peerId.id)) |> map { Optional($0) } |> `catch` { _ in return .single(nil) } + |> mapToSignal { _ -> Signal in + if reportSpam { + return postbox.modify { modifier -> TelegramSecretChat? in + if let peer = modifier.getPeer(peerId) as? TelegramSecretChat { + return peer + } else { + return nil + } + } + |> mapToSignal { peer -> Signal in + if let peer = peer { + return network.request(Api.functions.messages.reportEncryptedSpam(peer: Api.InputEncryptedChat.inputEncryptedChat(chatId: peer.id.id, accessHash: peer.accessHash))) + |> map { Optional($0) } + |> `catch` { _ -> Signal in + return .single(nil) + } + |> mapToSignal { result -> Signal in + return postbox.modify { modifier -> Void in + if result != nil { + modifier.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in + if let current = current as? CachedSecretChatData { + return current.withUpdatedReportStatus(.didReport) + } else { + return current + } + }) + } + } + } + } else { + return .single(Void()) + } + } + } else { + return .single(Void()) + } + } |> mapToSignal { _ -> Signal in return postbox.modify { modifier -> Void in markOutgoingOperationAsCompleted(modifier: modifier, peerId: peerId, tagLocalIndex: tagLocalIndex, forceRemove: true) diff --git a/TelegramCore/RemovePeerChat.swift b/TelegramCore/RemovePeerChat.swift index 556f9a360e..30a22bb660 100644 --- a/TelegramCore/RemovePeerChat.swift +++ b/TelegramCore/RemovePeerChat.swift @@ -12,7 +12,7 @@ public func removePeerChat(postbox: Postbox, peerId: PeerId, reportChatSpam: Boo if peerId.namespace == Namespaces.Peer.SecretChat { if let state = modifier.getPeerChatState(peerId) as? SecretChatState { - let updatedState = addSecretChatOutgoingOperation(modifier: modifier, peerId: peerId, operation: SecretChatOutgoingOperationContents.terminate, state: state).withUpdatedEmbeddedState(.terminated) + let updatedState = addSecretChatOutgoingOperation(modifier: modifier, peerId: peerId, operation: SecretChatOutgoingOperationContents.terminate(reportSpam: reportChatSpam), state: state).withUpdatedEmbeddedState(.terminated) if updatedState != state { modifier.setPeerChatState(peerId, state: updatedState) if let peer = modifier.getPeer(peerId) as? TelegramSecretChat { diff --git a/TelegramCore/ReportPeer.swift b/TelegramCore/ReportPeer.swift index c08e6cf2eb..3e5d2a8185 100644 --- a/TelegramCore/ReportPeer.swift +++ b/TelegramCore/ReportPeer.swift @@ -12,8 +12,29 @@ import Foundation public func reportPeer(account: Account, peerId: PeerId) -> Signal { return account.postbox.modify { modifier -> Signal in if let peer = modifier.getPeer(peerId) { - if let _ = peer as? TelegramSecretChat { - return .complete() + if let peer = peer as? TelegramSecretChat { + return account.network.request(Api.functions.messages.reportEncryptedSpam(peer: Api.InputEncryptedChat.inputEncryptedChat(chatId: peer.id.id, accessHash: peer.accessHash))) + |> map { Optional($0) } + |> `catch` { _ -> Signal in + return .single(nil) + } + |> mapToSignal { result -> Signal in + return account.postbox.modify { modifier -> Void in + if result != nil { + modifier.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in + if let current = current as? CachedUserData { + return current.withUpdatedReportStatus(.didReport) + } else if let current = current as? CachedGroupData { + return current.withUpdatedReportStatus(.didReport) + } else if let current = current as? CachedChannelData { + return current.withUpdatedReportStatus(.didReport) + } else { + return current + } + }) + } + } + } } else if let inputPeer = apiInputPeer(peer) { return account.network.request(Api.functions.messages.reportSpam(peer: inputPeer)) |> map { Optional($0) } @@ -126,6 +147,8 @@ public func dismissReportPeer(account: Account, peerId: PeerId) -> Signal PeerChatState { - return addSecretChatOutgoingOperation(modifier: modifier, peerId: peerId, operation: SecretChatOutgoingOperationContents.terminate, state: state.state as! SecretChatState).withUpdatedEmbeddedState(.terminated) + return addSecretChatOutgoingOperation(modifier: modifier, peerId: peerId, operation: SecretChatOutgoingOperationContents.terminate(reportSpam: false), state: state.state as! SecretChatState).withUpdatedEmbeddedState(.terminated) } diff --git a/TelegramCore/TelegramSecretChat.swift b/TelegramCore/TelegramSecretChat.swift index af1c83efee..ac2a7c83ac 100644 --- a/TelegramCore/TelegramSecretChat.swift +++ b/TelegramCore/TelegramSecretChat.swift @@ -75,3 +75,33 @@ public final class TelegramSecretChat: Peer { return TelegramSecretChat(id: self.id, creationDate: self.creationDate, regularPeerId: self.regularPeerId, accessHash: self.accessHash, role: self.role, embeddedState: self.embeddedState, messageAutoremoveTimeout: messageAutoremoveTimeout) } } + +public final class CachedSecretChatData: CachedPeerData { + public let peerIds: Set = Set() + + public let reportStatus: PeerReportStatus + + public init(reportStatus: PeerReportStatus) { + self.reportStatus = reportStatus + } + + public init(decoder: Decoder) { + self.reportStatus = PeerReportStatus(rawValue: decoder.decodeInt32ForKey("rs", orElse: 0))! + } + + public func encode(_ encoder: Encoder) { + encoder.encodeInt32(self.reportStatus.rawValue, forKey: "rs") + } + + public func isEqual(to: CachedPeerData) -> Bool { + if let to = to as? CachedSecretChatData { + return self.reportStatus == to.reportStatus + } else { + return false + } + } + + func withUpdatedReportStatus(_ reportStatus: PeerReportStatus) -> CachedSecretChatData { + return CachedSecretChatData(reportStatus: reportStatus) + } +} diff --git a/TelegramCore/UpdateCachedPeerData.swift b/TelegramCore/UpdateCachedPeerData.swift index 3dd5f0ea27..0a5592a60c 100644 --- a/TelegramCore/UpdateCachedPeerData.swift +++ b/TelegramCore/UpdateCachedPeerData.swift @@ -9,7 +9,7 @@ import Foundation func fetchAndUpdateSupplementalCachedPeerData(peerId: PeerId, network: Network, postbox: Postbox) -> Signal { return postbox.modify { modifier -> Signal in - if let peer = modifier.getPeer(peerId), let inputPeer = apiInputPeer(peer) { + if let peer = modifier.getPeer(peerId) { let cachedData = modifier.getPeerCachedData(peerId: peerId) if let cachedData = cachedData as? CachedUserData { @@ -24,51 +24,76 @@ func fetchAndUpdateSupplementalCachedPeerData(peerId: PeerId, network: Network, if cachedData.reportStatus != .unknown { return .complete() } + } else if let cachedData = cachedData as? CachedSecretChatData { + if cachedData.reportStatus != .unknown { + return .complete() + } } - return network.request(Api.functions.messages.getPeerSettings(peer: inputPeer)) - |> retryRequest - |> mapToSignal { peerSettings -> Signal in + if peerId.namespace == Namespaces.Peer.SecretChat { + return postbox.modify { modifier -> Void in let reportStatus: PeerReportStatus - switch peerSettings { - case let .peerSettings(flags): - reportStatus = (flags & (1 << 0) != 0) ? .canReport : .none + if let peer = modifier.getPeer(peerId), let associatedPeerId = peer.associatedPeerId, modifier.isPeerContact(peerId: associatedPeerId) { + reportStatus = .canReport + } else { + reportStatus = .canReport } - return postbox.modify { modifier -> Void in - modifier.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in - switch peerId.namespace { - case Namespaces.Peer.CloudUser: - let previous: CachedUserData - if let current = current as? CachedUserData { - previous = current - } else { - previous = CachedUserData() - } - return previous.withUpdatedReportStatus(reportStatus) - case Namespaces.Peer.CloudGroup: - let previous: CachedGroupData - if let current = current as? CachedGroupData { - previous = current - } else { - previous = CachedGroupData() - } - return previous.withUpdatedReportStatus(reportStatus) - case Namespaces.Peer.CloudChannel: - let previous: CachedChannelData - if let current = current as? CachedChannelData { - previous = current - } else { - previous = CachedChannelData() - } - return previous.withUpdatedReportStatus(reportStatus) - default: - break - } - return current - }) - } + modifier.updatePeerCachedData(peerIds: [peerId], update: { peerId, current in + if let current = current as? CachedSecretChatData { + return current.withUpdatedReportStatus(reportStatus) + } else { + return CachedSecretChatData(reportStatus: reportStatus) + } + }) } + } else if let inputPeer = apiInputPeer(peer) { + return network.request(Api.functions.messages.getPeerSettings(peer: inputPeer)) + |> retryRequest + |> mapToSignal { peerSettings -> Signal in + let reportStatus: PeerReportStatus + switch peerSettings { + case let .peerSettings(flags): + reportStatus = (flags & (1 << 0) != 0) ? .canReport : .none + } + + return postbox.modify { modifier -> Void in + modifier.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in + switch peerId.namespace { + case Namespaces.Peer.CloudUser: + let previous: CachedUserData + if let current = current as? CachedUserData { + previous = current + } else { + previous = CachedUserData() + } + return previous.withUpdatedReportStatus(reportStatus) + case Namespaces.Peer.CloudGroup: + let previous: CachedGroupData + if let current = current as? CachedGroupData { + previous = current + } else { + previous = CachedGroupData() + } + return previous.withUpdatedReportStatus(reportStatus) + case Namespaces.Peer.CloudChannel: + let previous: CachedChannelData + if let current = current as? CachedChannelData { + previous = current + } else { + previous = CachedChannelData() + } + return previous.withUpdatedReportStatus(reportStatus) + default: + break + } + return current + }) + } + } + } else { + return .complete() + } } else { return .complete() }