From 0fc181ae9a654e7e0c6d5a72a79b8da44875eb1f Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 8 Apr 2021 15:32:36 +1000 Subject: [PATCH] Fix voice message handling --- .../ConversationVC+Interaction.swift | 35 +++++++++++++------ .../Content Views/MediaPlaceholderView.swift | 2 +- .../Message Cells/VisibleMessageCell.swift | 35 +++++++++++++------ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 3beaac5a6..8f5ef4d5f 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -373,23 +373,32 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc } func handleViewItemTapped(_ viewItem: ConversationViewItem, gestureRecognizer: UITapGestureRecognizer) { + func confirmDownload() { + let modal = DownloadAttachmentModal(viewItem: viewItem) + modal.modalPresentationStyle = .overFullScreen + modal.modalTransitionStyle = .crossDissolve + present(modal, animated: true, completion: nil) + } if let message = viewItem.interaction as? TSOutgoingMessage, message.messageState == .failed { // Show the failed message sheet showFailedMessageSheet(for: message) } else { switch viewItem.messageCellType { - case .audio: playOrPauseAudio(for: viewItem) + case .audio: + if viewItem.interaction is TSIncomingMessage, + let thread = self.thread as? TSContactThread, + Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { + confirmDownload() + } else { + playOrPauseAudio(for: viewItem) + } case .mediaMessage: guard let index = viewItems.firstIndex(where: { $0 === viewItem }), let cell = messagesTableView.cellForRow(at: IndexPath(row: index, section: 0)) as? VisibleMessageCell else { return } if viewItem.interaction is TSIncomingMessage, let thread = self.thread as? TSContactThread, Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { - // Ask the user whether they want to download this attachment - let modal = DownloadAttachmentModal(viewItem: viewItem) - modal.modalPresentationStyle = .overFullScreen - modal.modalTransitionStyle = .crossDissolve - present(modal, animated: true, completion: nil) + confirmDownload() } else { guard let albumView = cell.albumView else { return } let locationInCell = gestureRecognizer.location(in: cell) @@ -417,10 +426,16 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc gallery.presentDetailView(fromViewController: self, mediaAttachment: stream) } case .genericAttachment: - // Open the document if possible - guard let url = viewItem.attachmentStream?.originalMediaURL else { return } - let shareVC = UIActivityViewController(activityItems: [ url ], applicationActivities: nil) - navigationController!.present(shareVC, animated: true, completion: nil) + if viewItem.interaction is TSIncomingMessage, + let thread = self.thread as? TSContactThread, + Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { + confirmDownload() + } else { + // Open the document if possible + guard let url = viewItem.attachmentStream?.originalMediaURL else { return } + let shareVC = UIActivityViewController(activityItems: [ url ], applicationActivities: nil) + navigationController!.present(shareVC, animated: true, completion: nil) + } case .textOnlyMessage: if let preview = viewItem.linkPreview, let urlAsString = preview.urlString, let url = URL(string: urlAsString) { // Open the link preview URL diff --git a/Session/Conversations/Message Cells/Content Views/MediaPlaceholderView.swift b/Session/Conversations/Message Cells/Content Views/MediaPlaceholderView.swift index 3feaca735..5c4f586af 100644 --- a/Session/Conversations/Message Cells/Content Views/MediaPlaceholderView.swift +++ b/Session/Conversations/Message Cells/Content Views/MediaPlaceholderView.swift @@ -31,7 +31,7 @@ final class MediaPlaceholderView : UIView { attachments = message.attachments(with: transaction) } guard let contentType = attachments.first?.contentType else { return ("actionsheet_document_black", "file") } // Should never occur - if MIMETypeUtil.isAudio(contentType) { return ("Microphone", "audio") } + if MIMETypeUtil.isAudio(contentType) { return ("attachment_audio", "audio") } if MIMETypeUtil.isImage(contentType) || MIMETypeUtil.isVideo(contentType) { return ("actionsheet_camera_roll_black", "media") } return ("actionsheet_document_black", "file") }() diff --git a/Session/Conversations/Message Cells/VisibleMessageCell.swift b/Session/Conversations/Message Cells/VisibleMessageCell.swift index aefe2ccb3..e1b94861e 100644 --- a/Session/Conversations/Message Cells/VisibleMessageCell.swift +++ b/Session/Conversations/Message Cells/VisibleMessageCell.swift @@ -302,6 +302,11 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { private func populateContentView(for viewItem: ConversationViewItem) { snContentView.subviews.forEach { $0.removeFromSuperview() } + func showMediaPlaceholder() { + let mediaPlaceholderView = MediaPlaceholderView(viewItem: viewItem, textColor: bodyLabelTextColor) + snContentView.addSubview(mediaPlaceholderView) + mediaPlaceholderView.pin(to: snContentView) + } albumView = nil bodyTextView = nil mediaTextOverlayView = nil @@ -340,9 +345,7 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { if viewItem.interaction is TSIncomingMessage, let thread = viewItem.interaction.thread as? TSContactThread, Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { - let mediaPlaceholderView = MediaPlaceholderView(viewItem: viewItem, textColor: bodyLabelTextColor) - snContentView.addSubview(mediaPlaceholderView) - mediaPlaceholderView.pin(to: snContentView) + showMediaPlaceholder() } else { guard let cache = delegate?.getMediaCache() else { preconditionFailure() } let maxMessageWidth = VisibleMessageCell.getMaxWidth(for: viewItem) @@ -365,14 +368,26 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { unloadContent = { albumView.unloadMedia() } } case .audio: - let voiceMessageView = VoiceMessageView(viewItem: viewItem) - snContentView.addSubview(voiceMessageView) - voiceMessageView.pin(to: snContentView) - viewItem.lastAudioMessageView = voiceMessageView + if viewItem.interaction is TSIncomingMessage, + let thread = viewItem.interaction.thread as? TSContactThread, + Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { + showMediaPlaceholder() + } else { + let voiceMessageView = VoiceMessageView(viewItem: viewItem) + snContentView.addSubview(voiceMessageView) + voiceMessageView.pin(to: snContentView) + viewItem.lastAudioMessageView = voiceMessageView + } case .genericAttachment: - let documentView = DocumentView(viewItem: viewItem, textColor: bodyLabelTextColor) - snContentView.addSubview(documentView) - documentView.pin(to: snContentView) + if viewItem.interaction is TSIncomingMessage, + let thread = viewItem.interaction.thread as? TSContactThread, + Storage.shared.getContact(with: thread.contactIdentifier())?.isTrusted != true { + showMediaPlaceholder() + } else { + let documentView = DocumentView(viewItem: viewItem, textColor: bodyLabelTextColor) + snContentView.addSubview(documentView) + documentView.pin(to: snContentView) + } default: return } }