From 5a65539234968ee3409cd9a92aaca405c10212f2 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 25 Sep 2020 22:01:11 +0400 Subject: [PATCH] Fix media validation --- submodules/SyncCore/Sources/TelegramMediaFile.swift | 6 ++++++ .../Sources/ValidationMessageAttribute.swift | 13 +++++++++++++ .../TelegramCore/Sources/AccountManager.swift | 1 + .../TelegramCore/Sources/FetchedMediaResource.swift | 13 ++++++++++++- .../ManagedSecretChatOutgoingOperations.swift | 8 ++++++++ .../Sources/PendingMessageUploadedContent.swift | 2 ++ .../TelegramCore/Sources/TelegramMediaFile.swift | 4 +++- submodules/TelegramUI/Sources/ChatMessageItem.swift | 10 ++++++++++ 8 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 submodules/SyncCore/Sources/ValidationMessageAttribute.swift diff --git a/submodules/SyncCore/Sources/TelegramMediaFile.swift b/submodules/SyncCore/Sources/TelegramMediaFile.swift index 865964df4d..66f8ac0d75 100644 --- a/submodules/SyncCore/Sources/TelegramMediaFile.swift +++ b/submodules/SyncCore/Sources/TelegramMediaFile.swift @@ -9,6 +9,7 @@ private let typeVideo: Int32 = 4 private let typeAudio: Int32 = 5 private let typeHasLinkedStickers: Int32 = 6 private let typeHintFileIsLarge: Int32 = 7 +private let typeHintIsValidated: Int32 = 8 public enum StickerPackReference: PostboxCoding, Hashable, Equatable { case id(id: Int64, accessHash: Int64) @@ -131,6 +132,7 @@ public enum TelegramMediaFileAttribute: PostboxCoding { case Audio(isVoice: Bool, duration: Int, title: String?, performer: String?, waveform: MemoryBuffer?) case HasLinkedStickers case hintFileIsLarge + case hintIsValidated public init(decoder: PostboxDecoder) { let type: Int32 = decoder.decodeInt32ForKey("t", orElse: 0) @@ -156,6 +158,8 @@ public enum TelegramMediaFileAttribute: PostboxCoding { self = .HasLinkedStickers case typeHintFileIsLarge: self = .hintFileIsLarge + case typeHintIsValidated: + self = .hintIsValidated default: preconditionFailure() } @@ -208,6 +212,8 @@ public enum TelegramMediaFileAttribute: PostboxCoding { encoder.encodeInt32(typeHasLinkedStickers, forKey: "t") case .hintFileIsLarge: encoder.encodeInt32(typeHintFileIsLarge, forKey: "t") + case .hintIsValidated: + encoder.encodeInt32(typeHintIsValidated, forKey: "t") } } } diff --git a/submodules/SyncCore/Sources/ValidationMessageAttribute.swift b/submodules/SyncCore/Sources/ValidationMessageAttribute.swift new file mode 100644 index 0000000000..186115c5b0 --- /dev/null +++ b/submodules/SyncCore/Sources/ValidationMessageAttribute.swift @@ -0,0 +1,13 @@ +import Foundation +import Postbox + +public class ValidationMessageAttribute: MessageAttribute { + public init() { + } + + required public init(decoder: PostboxDecoder) { + } + + public func encode(_ encoder: PostboxEncoder) { + } +} diff --git a/submodules/TelegramCore/Sources/AccountManager.swift b/submodules/TelegramCore/Sources/AccountManager.swift index e604d4f956..859e8d3cff 100644 --- a/submodules/TelegramCore/Sources/AccountManager.swift +++ b/submodules/TelegramCore/Sources/AccountManager.swift @@ -167,6 +167,7 @@ private var declaredEncodables: Void = { declareEncodable(Country.self, f: { Country(decoder: $0) }) declareEncodable(Country.CountryCode.self, f: { Country.CountryCode(decoder: $0) }) declareEncodable(CountriesList.self, f: { CountriesList(decoder: $0) }) + declareEncodable(ValidationMessageAttribute.self, f: { ValidationMessageAttribute(decoder: $0) }) return }() diff --git a/submodules/TelegramCore/Sources/FetchedMediaResource.swift b/submodules/TelegramCore/Sources/FetchedMediaResource.swift index f324e6ccdb..33f492620e 100644 --- a/submodules/TelegramCore/Sources/FetchedMediaResource.swift +++ b/submodules/TelegramCore/Sources/FetchedMediaResource.swift @@ -540,7 +540,18 @@ func revalidateMediaResourceReference(postbox: Postbox, network: Network, revali if let updatedResource = findUpdatedMediaResource(media: item.file, previousMedia: media, resource: resource) { return postbox.transaction { transaction -> RevalidatedMediaResource in if let id = media.id { - updateMessageMedia(transaction: transaction, id: id, media: item.file) + var attributes = item.file.attributes + if !attributes.contains(where: { attribute in + if case .hintIsValidated = attribute { + return true + } else { + return false + } + }) { + attributes.append(.hintIsValidated) + } + let file = item.file.withUpdatedAttributes(attributes) + updateMessageMedia(transaction: transaction, id: id, media: file) } return RevalidatedMediaResource(updatedResource: updatedResource, updatedReference: nil) } diff --git a/submodules/TelegramCore/Sources/ManagedSecretChatOutgoingOperations.swift b/submodules/TelegramCore/Sources/ManagedSecretChatOutgoingOperations.swift index c8015e0f5c..76685ab98e 100644 --- a/submodules/TelegramCore/Sources/ManagedSecretChatOutgoingOperations.swift +++ b/submodules/TelegramCore/Sources/ManagedSecretChatOutgoingOperations.swift @@ -523,6 +523,8 @@ private func decryptedAttributes46(_ attributes: [TelegramMediaFileAttribute], t break case .hintFileIsLarge: break + case .hintIsValidated: + break } } return result @@ -580,6 +582,10 @@ private func decryptedAttributes73(_ attributes: [TelegramMediaFileAttribute], t break case .hintFileIsLarge: break + case .hintIsValidated: + break + case .hintIsValidated: + break } } return result @@ -637,6 +643,8 @@ private func decryptedAttributes101(_ attributes: [TelegramMediaFileAttribute], break case .hintFileIsLarge: break + case .hintIsValidated: + break } } return result diff --git a/submodules/TelegramCore/Sources/PendingMessageUploadedContent.swift b/submodules/TelegramCore/Sources/PendingMessageUploadedContent.swift index 9feef62447..9370f00b36 100644 --- a/submodules/TelegramCore/Sources/PendingMessageUploadedContent.swift +++ b/submodules/TelegramCore/Sources/PendingMessageUploadedContent.swift @@ -489,6 +489,8 @@ func inputDocumentAttributesFromFileAttributes(_ fileAttributes: [TelegramMediaF attributes.append(.documentAttributeHasStickers) case .hintFileIsLarge: break + case .hintIsValidated: + break case let .Video(duration, size, videoFlags): var flags: Int32 = 0 if videoFlags.contains(.instantRoundVideo) { diff --git a/submodules/TelegramCore/Sources/TelegramMediaFile.swift b/submodules/TelegramCore/Sources/TelegramMediaFile.swift index 8a44e61601..d1285decc8 100644 --- a/submodules/TelegramCore/Sources/TelegramMediaFile.swift +++ b/submodules/TelegramCore/Sources/TelegramMediaFile.swift @@ -155,7 +155,9 @@ func telegramMediaFileThumbnailRepresentationsFromApiSizes(datacenterId: Int32, func telegramMediaFileFromApiDocument(_ document: Api.Document) -> TelegramMediaFile? { switch document { case let .document(_, id, accessHash, fileReference, _, mimeType, size, thumbs, videoThumbs, dcId, attributes): - let parsedAttributes = telegramMediaFileAttributesFromApiAttributes(attributes) + var parsedAttributes = telegramMediaFileAttributesFromApiAttributes(attributes) + parsedAttributes.append(.hintIsValidated) + let (immediateThumbnail, previewRepresentations) = telegramMediaFileThumbnailRepresentationsFromApiSizes(datacenterId: dcId, documentId: id, accessHash: accessHash, fileReference: fileReference.makeData(), sizes: thumbs ?? []) var videoThumbnails: [TelegramMediaFile.VideoThumbnail] = [] diff --git a/submodules/TelegramUI/Sources/ChatMessageItem.swift b/submodules/TelegramUI/Sources/ChatMessageItem.swift index 7d85a5730f..cda6679a83 100644 --- a/submodules/TelegramUI/Sources/ChatMessageItem.swift +++ b/submodules/TelegramUI/Sources/ChatMessageItem.swift @@ -347,10 +347,20 @@ public final class ChatMessageItem: ListViewItem, CustomStringConvertible { if telegramFile.isAnimatedSticker, (self.message.id.peerId.namespace == Namespaces.Peer.SecretChat || !telegramFile.previewRepresentations.isEmpty), let size = telegramFile.size, size > 0 && size <= 128 * 1024 { if self.message.id.peerId.namespace == Namespaces.Peer.SecretChat { if telegramFile.fileId.namespace == Namespaces.Media.CloudFile { + var isValidated = false + for attribute in telegramFile.attributes { + if case .hintIsValidated = attribute { + isValidated = true + break + } + } + inner: for attribute in telegramFile.attributes { if case let .Sticker(_, packReference, _) = attribute { if case .name = packReference { viewClassName = ChatMessageAnimatedStickerItemNode.self + } else if isValidated { + viewClassName = ChatMessageAnimatedStickerItemNode.self } break inner }