From 152d25f4ff9bf9890ffcb8e53ff65abc89bdd14e Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 21 Jul 2018 21:31:04 +0300 Subject: [PATCH] no message --- .../AutoremoveTimeoutMessageAttribute.swift | 2 +- .../ManagedSecretChatOutgoingOperations.swift | 120 +++++++++--------- TelegramCore/PeerLiveLocationsContext.swift | 16 ++- TelegramCore/PendingMessageManager.swift | 8 ++ TelegramCore/RequestEditMessage.swift | 6 +- 5 files changed, 86 insertions(+), 66 deletions(-) diff --git a/TelegramCore/AutoremoveTimeoutMessageAttribute.swift b/TelegramCore/AutoremoveTimeoutMessageAttribute.swift index f65129bd86..7ca46931a3 100644 --- a/TelegramCore/AutoremoveTimeoutMessageAttribute.swift +++ b/TelegramCore/AutoremoveTimeoutMessageAttribute.swift @@ -57,7 +57,7 @@ public extension Message { case _ as TelegramMediaImage: return true case let file as TelegramMediaFile: - if file.isVideo { + if file.isVideo || file.isAnimated || file.isVoice { return true } default: diff --git a/TelegramCore/ManagedSecretChatOutgoingOperations.swift b/TelegramCore/ManagedSecretChatOutgoingOperations.swift index d298d9392a..373f9d65c7 100644 --- a/TelegramCore/ManagedSecretChatOutgoingOperations.swift +++ b/TelegramCore/ManagedSecretChatOutgoingOperations.swift @@ -1032,61 +1032,61 @@ private func sendMessage(postbox: Postbox, network: Network, messageId: MessageI if let message = transaction.getMessage(messageId), let globallyUniqueId = message.globallyUniqueId { let decryptedMessage = boxedDecryptedMessage(transaction: transaction, message: message, globallyUniqueId: globallyUniqueId, uploadedFile: file, thumbnailData: thumbnailData, layer: layer) return sendBoxedDecryptedMessage(postbox: postbox, network: network, peer: peer, state: state, operationIndex: tagLocalIndex, decryptedMessage: decryptedMessage, globallyUniqueId: globallyUniqueId, file: file, asService: wasDelivered, wasDelivered: wasDelivered) - |> mapToSignal { result in - return postbox.transaction { transaction -> Void in - if result == nil { - replaceOutgoingOperationWithEmptyMessage(transaction: transaction, peerId: messageId.peerId, tagLocalIndex: tagLocalIndex, globallyUniqueId: globallyUniqueId) - } else { - markOutgoingOperationAsCompleted(transaction: transaction, peerId: messageId.peerId, tagLocalIndex: tagLocalIndex, forceRemove: result == nil) - } - - var timestamp = message.timestamp - var encryptedFile: SecretChatFileReference? - if let result = result { - switch result { - case let .sentEncryptedMessage(date): - timestamp = date - case let .sentEncryptedFile(date, file): - timestamp = date - encryptedFile = SecretChatFileReference(file) - } - } - - transaction.offsetPendingMessagesTimestamps(lowerBound: message.id, excludeIds: Set([messageId]), timestamp: timestamp) - - transaction.updateMessage(message.id, update: { currentMessage in - var flags = StoreMessageFlags(currentMessage.flags) - if let _ = result { - flags.remove(.Unsent) - flags.remove(.Sending) - } else { - flags = [.Failed] - } - var storeForwardInfo: StoreMessageForwardInfo? - if let forwardInfo = currentMessage.forwardInfo { - storeForwardInfo = StoreMessageForwardInfo(authorId: forwardInfo.author.id, sourceId: forwardInfo.source?.id, sourceMessageId: forwardInfo.sourceMessageId, date: forwardInfo.date, authorSignature: forwardInfo.authorSignature) - } - - var updatedMedia = currentMessage.media - - if let fromMedia = currentMessage.media.first, let encryptedFile = encryptedFile, let file = file { - var toMedia: Media? - if let fromMedia = fromMedia as? TelegramMediaFile { - let updatedFile = TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.CloudSecretFile, id: encryptedFile.id), reference: nil, resource: SecretFileMediaResource(fileId: encryptedFile.id, accessHash: encryptedFile.accessHash, containerSize: encryptedFile.size, decryptedSize: file.size, datacenterId: Int(encryptedFile.datacenterId), key: file.key), previewRepresentations: fromMedia.previewRepresentations, mimeType: fromMedia.mimeType, size: fromMedia.size, attributes: fromMedia.attributes) - toMedia = updatedFile - updatedMedia = [updatedFile] - } - - if let toMedia = toMedia { - applyMediaResourceChanges(from: fromMedia, to: toMedia, postbox: postbox) - } - } - - return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, timestamp: timestamp, flags: flags, tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: currentMessage.attributes, media: updatedMedia)) - }) - - maybeReadSecretOutgoingMessage(transaction: transaction, index: MessageIndex(id: message.id, timestamp: timestamp)) + |> mapToSignal { result in + return postbox.transaction { transaction -> Void in + if result == nil { + replaceOutgoingOperationWithEmptyMessage(transaction: transaction, peerId: messageId.peerId, tagLocalIndex: tagLocalIndex, globallyUniqueId: globallyUniqueId) + } else { + markOutgoingOperationAsCompleted(transaction: transaction, peerId: messageId.peerId, tagLocalIndex: tagLocalIndex, forceRemove: result == nil) } + + var timestamp = message.timestamp + var encryptedFile: SecretChatFileReference? + if let result = result { + switch result { + case let .sentEncryptedMessage(date): + timestamp = date + case let .sentEncryptedFile(date, file): + timestamp = date + encryptedFile = SecretChatFileReference(file) + } + } + + transaction.offsetPendingMessagesTimestamps(lowerBound: message.id, excludeIds: Set([messageId]), timestamp: timestamp) + + transaction.updateMessage(message.id, update: { currentMessage in + var flags = StoreMessageFlags(currentMessage.flags) + if let _ = result { + flags.remove(.Unsent) + flags.remove(.Sending) + } else { + flags = [.Failed] + } + var storeForwardInfo: StoreMessageForwardInfo? + if let forwardInfo = currentMessage.forwardInfo { + storeForwardInfo = StoreMessageForwardInfo(authorId: forwardInfo.author.id, sourceId: forwardInfo.source?.id, sourceMessageId: forwardInfo.sourceMessageId, date: forwardInfo.date, authorSignature: forwardInfo.authorSignature) + } + + var updatedMedia = currentMessage.media + + if let fromMedia = currentMessage.media.first, let encryptedFile = encryptedFile, let file = file { + var toMedia: Media? + if let fromMedia = fromMedia as? TelegramMediaFile { + let updatedFile = TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.CloudSecretFile, id: encryptedFile.id), reference: nil, resource: SecretFileMediaResource(fileId: encryptedFile.id, accessHash: encryptedFile.accessHash, containerSize: encryptedFile.size, decryptedSize: file.size, datacenterId: Int(encryptedFile.datacenterId), key: file.key), previewRepresentations: fromMedia.previewRepresentations, mimeType: fromMedia.mimeType, size: fromMedia.size, attributes: fromMedia.attributes) + toMedia = updatedFile + updatedMedia = [updatedFile] + } + + if let toMedia = toMedia { + applyMediaResourceChanges(from: fromMedia, to: toMedia, postbox: postbox) + } + } + + return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, timestamp: timestamp, flags: flags, tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: currentMessage.attributes, media: updatedMedia)) + }) + + maybeReadSecretOutgoingMessage(transaction: transaction, index: MessageIndex(id: message.id, timestamp: timestamp)) + } } } else { replaceOutgoingOperationWithEmptyMessage(transaction: transaction, peerId: messageId.peerId, tagLocalIndex: tagLocalIndex, globallyUniqueId: arc4random64()) @@ -1211,12 +1211,12 @@ private func sendBoxedDecryptedMessage(postbox: Postbox, network: Network, peer: } } return sendMessage - |> map { next -> Api.messages.SentEncryptedMessage? in - return next - } - |> `catch`{ _ in - return .single(nil) - } + |> map { next -> Api.messages.SentEncryptedMessage? in + return next + } + |> `catch`{ _ in + return .single(nil) + } } private func requestTerminateSecretChat(postbox: Postbox, network: Network, peerId: PeerId, tagLocalIndex: Int32, reportSpam: Bool) -> Signal { diff --git a/TelegramCore/PeerLiveLocationsContext.swift b/TelegramCore/PeerLiveLocationsContext.swift index 0ec5b9c10f..306ea1f94e 100644 --- a/TelegramCore/PeerLiveLocationsContext.swift +++ b/TelegramCore/PeerLiveLocationsContext.swift @@ -7,9 +7,17 @@ import Foundation import SwiftSignalKit #endif -public func topPeerActiveLiveLocationMessages(viewTracker: AccountViewTracker, peerId: PeerId) -> Signal<[Message], NoError> { - return viewTracker.aroundMessageHistoryViewForLocation(.peer(peerId), index: .upperBound, anchorIndex: .upperBound, count: 100, fixedCombinedReadStates: nil, tagMask: .liveLocation, orderStatistics: []) - |> map { (view, _, _) -> [Message] in +public func topPeerActiveLiveLocationMessages(viewTracker: AccountViewTracker, accountPeerId: PeerId, peerId: PeerId) -> Signal<(Peer?, [Message]), NoError> { + return viewTracker.aroundMessageHistoryViewForLocation(.peer(peerId), index: .upperBound, anchorIndex: .upperBound, count: 100, fixedCombinedReadStates: nil, tagMask: .liveLocation, orderStatistics: [], additionalData: [.peer(accountPeerId)]) + |> map { (view, _, _) -> (Peer?, [Message]) in + var accountPeer: Peer? + for entry in view.additionalData { + if case let .peer(id, peer) = entry { + accountPeer = peer + break + } + } + let timestamp = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) var result: [Message] = [] for entry in view.entries { @@ -25,6 +33,6 @@ public func topPeerActiveLiveLocationMessages(viewTracker: AccountViewTracker, p } } } - return result + return (accountPeer, result) } } diff --git a/TelegramCore/PendingMessageManager.swift b/TelegramCore/PendingMessageManager.swift index b9baefe8bf..98b333196b 100644 --- a/TelegramCore/PendingMessageManager.swift +++ b/TelegramCore/PendingMessageManager.swift @@ -141,6 +141,14 @@ public final class PendingMessageManager { updateUploadingPeerIds.insert(id.peerId) context.sendDisposable.dispose() context.uploadDisposable.dispose() + + if context.status != nil { + context.status = nil + for subscriber in context.statusSubscribers.copyItems() { + subscriber(nil) + } + } + if context.statusSubscribers.isEmpty { self.messageContexts.removeValue(forKey: id) } diff --git a/TelegramCore/RequestEditMessage.swift b/TelegramCore/RequestEditMessage.swift index 32e6ac4740..c2acd75499 100644 --- a/TelegramCore/RequestEditMessage.swift +++ b/TelegramCore/RequestEditMessage.swift @@ -21,6 +21,7 @@ public enum RequestEditMessageResult { public enum RequestEditMessageError { case generic + case restricted } public func requestEditMessage(account: Account, messageId: MessageId, text: String, media: RequestEditMessageMedia, entities: TextEntitiesMessageAttribute? = nil, disableUrlPreview: Bool = false) -> Signal { @@ -125,7 +126,10 @@ public func requestEditMessage(account: Account, messageId: MessageId, text: Str return .fail(error) } } - |> mapError { _ -> RequestEditMessageError in + |> mapError { error -> RequestEditMessageError in + if error.errorDescription.hasPrefix("CHAT_SEND_") && error.errorDescription.hasSuffix("_FORBIDDEN") { + return .restricted + } return .generic } |> mapToSignal { result -> Signal in