From 1899a3fe3d29090215e8012bacaa94f36a71d144 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 1 Oct 2021 14:44:52 +1000 Subject: [PATCH] fix sharing not work for session --- SessionShareExtension/ThreadPickerVC.swift | 18 +++++----- .../MediaMessageView.swift | 2 +- .../MessageSender+Convenience.swift | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/SessionShareExtension/ThreadPickerVC.swift b/SessionShareExtension/ThreadPickerVC.swift index 390639c3e..75355d23a 100644 --- a/SessionShareExtension/ThreadPickerVC.swift +++ b/SessionShareExtension/ThreadPickerVC.swift @@ -121,16 +121,14 @@ final class ThreadPickerVC : UIViewController, UITableViewDataSource, UITableVie } shareVC!.dismiss(animated: true, completion: nil) ModalActivityIndicatorViewController.present(fromViewController: shareVC!, canCancel: false, message: NSLocalizedString("vc_share_sending_message", comment: "")) { activityIndicator in - Storage.write { transaction in - MessageSender.sendNonDurably(message, with: attachments, in: self.selectedThread!, using: transaction).done { [weak self] _ in - guard let self = self else { return } - activityIndicator.dismiss { } - self.shareVC!.shareViewWasCompleted() - }.catch { [weak self] error in - guard let self = self else { return } - activityIndicator.dismiss { } - self.shareVC!.shareViewFailed(error: error) - } + MessageSender.sendNonDurably(message, with: attachments, in: self.selectedThread!).done { [weak self] _ in + guard let self = self else { return } + activityIndicator.dismiss { } + self.shareVC!.shareViewWasCompleted() + }.catch { [weak self] error in + guard let self = self else { return } + activityIndicator.dismiss { } + self.shareVC!.shareViewFailed(error: error) } } } diff --git a/SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift b/SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift index 7c13aae4e..443f5fc80 100644 --- a/SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift +++ b/SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift @@ -269,7 +269,7 @@ public class MediaMessageView: UIView, OWSAudioPlayerDelegate { private func createGenericPreview() { var subviews = [UIView]() - let imageView = createHeroImageView(imageName: "file-thin-black-filled-large") + let imageView = createHeroImageView(imageName: "actionsheet_document_black") subviews.append(imageView) let fileNameLabel = createFileNameLabel() diff --git a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift index 177cdb65d..3297ce14e 100644 --- a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift +++ b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift @@ -67,4 +67,38 @@ extension MessageSender { let destination = Message.Destination.from(thread) return MessageSender.send(message, to: destination, using: transaction) } + + public static func sendNonDurably(_ message: VisibleMessage, with attachments: [SignalAttachment], in thread: TSThread) -> Promise { + Storage.writeSync{ transaction in + prep(attachments, for: message, using: transaction) + } + let attachments = message.attachmentIDs.compactMap { TSAttachment.fetch(uniqueId: $0) as? TSAttachmentStream } + let attachmentsToUpload = attachments.filter { !$0.isUploaded } + let attachmentUploadPromises: [Promise] = attachmentsToUpload.map { stream in + let storage = SNMessagingKitConfiguration.shared.storage + if let v2OpenGroup = storage.getV2OpenGroup(for: thread.uniqueId!) { + let (promise, seal) = Promise.pending() + AttachmentUploadJob.upload(stream, using: { data in return OpenGroupAPIV2.upload(data, to: v2OpenGroup.room, on: v2OpenGroup.server) }, encrypt: false, onSuccess: { seal.fulfill(()) }, onFailure: { seal.reject($0) }) + return promise + } else { + let (promise, seal) = Promise.pending() + AttachmentUploadJob.upload(stream, using: FileServerAPIV2.upload, encrypt: true, onSuccess: { seal.fulfill(()) }, onFailure: { seal.reject($0) }) + return promise + } + } + let (promise, seal) = Promise.pending() + let results = when(resolved: attachmentUploadPromises).wait() + let errors = results.compactMap { result -> Swift.Error? in + if case .rejected(let error) = result { return error } else { return nil } + } + if let error = errors.first { seal.reject(error) } + Storage.write{ transaction in + sendNonDurably(message, in: thread, using: transaction).done { + seal.fulfill(()) + }.catch { error in + seal.reject(error) + } + } + return promise + } }