From d2e8f2142e19881dde353bc052f0455d7481eaab Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 23 Nov 2020 15:08:01 +1100 Subject: [PATCH] WIP --- .../Jobs/AttachmentUploadJob.swift | 3 +++ SessionMessagingKit/Jobs/JobDelegate.swift | 1 + SessionMessagingKit/Jobs/JobQueue.swift | 4 ++++ SessionMessagingKit/Jobs/MessageSendJob.swift | 20 +++++++++++++++++-- .../Sending & Receiving/MessageReceiver.swift | 6 ------ SessionMessagingKit/Storage.swift | 1 + 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift index 2efedb6f4..838e4a63e 100644 --- a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift @@ -11,6 +11,9 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N public class var collection: String { return "AttachmentUploadJobCollection" } public static let maxFailureCount: UInt = 20 + // MARK: Initialization + public override init() { } + // MARK: Coding public init?(coder: NSCoder) { } diff --git a/SessionMessagingKit/Jobs/JobDelegate.swift b/SessionMessagingKit/Jobs/JobDelegate.swift index b45a5bf28..d4b27f5b8 100644 --- a/SessionMessagingKit/Jobs/JobDelegate.swift +++ b/SessionMessagingKit/Jobs/JobDelegate.swift @@ -5,4 +5,5 @@ public protocol JobDelegate { func handleJobSucceeded(_ job: Job) func handleJobFailed(_ job: Job, with error: Error) func handleJobFailedPermanently(_ job: Job, with error: Error) + func postpone(_ job: Job) } diff --git a/SessionMessagingKit/Jobs/JobQueue.swift b/SessionMessagingKit/Jobs/JobQueue.swift index f02dc971e..f2328bc8d 100644 --- a/SessionMessagingKit/Jobs/JobQueue.swift +++ b/SessionMessagingKit/Jobs/JobQueue.swift @@ -64,6 +64,10 @@ public final class JobQueue : NSObject, JobDelegate { }) }) } + + public func postpone(_ job: Job) { + Timer.weakScheduledTimer(withTimeInterval: 3, target: self, selector: #selector(self.retry(_:)), userInfo: job, repeats: false) + } private func getRetryInterval(for job: Job) -> TimeInterval { // Arbitrary backoff factor... diff --git a/SessionMessagingKit/Jobs/MessageSendJob.swift b/SessionMessagingKit/Jobs/MessageSendJob.swift index 7f6bb4282..4574a1e33 100644 --- a/SessionMessagingKit/Jobs/MessageSendJob.swift +++ b/SessionMessagingKit/Jobs/MessageSendJob.swift @@ -10,7 +10,7 @@ public final class MessageSendJob : NSObject, Job, NSCoding { // NSObject/NSCodi // MARK: Settings public class var collection: String { return "MessageSendJobCollection" } - public static let maxFailureCount: UInt = 20 + public static let maxFailureCount: UInt = 10 // MARK: Initialization @objc public convenience init(message: Message, publicKey: String) { self.init(message: message, destination: .contact(publicKey: publicKey)) } @@ -61,7 +61,23 @@ public final class MessageSendJob : NSObject, Job, NSCoding { // NSObject/NSCodi // MARK: Running public func execute() { - Configuration.shared.storage.withAsync({ transaction in // Intentionally capture self + let storage = Configuration.shared.storage + if let message = message as? VisibleMessage { + let attachments = message.attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0) } + let attachmentsToUpload = attachments.filter { !$0.isUploaded } + attachmentsToUpload.forEach { attachment in + if storage.getAttachmentUploadJob(for: attachment.uniqueId!) != nil { + // Wait for it to finish + } else { + let job = AttachmentUploadJob() + storage.withAsync({ transaction in + JobQueue.shared.add(job, using: transaction) + }, completion: { }) + } + } + if !attachmentsToUpload.isEmpty { delegate?.postpone(self); return } // Wait for all attachments to upload before continuing + } + storage.withAsync({ transaction in // Intentionally capture self Threading.workQueue.async { MessageSender.send(self.message, to: self.destination, using: transaction).done(on: Threading.workQueue) { self.handleSuccess() diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index 885298ba2..ee1a48c3a 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -1,11 +1,5 @@ import SessionUtilitiesKit -// TODO: -// • Threads don't show up on the first message; only on the second. -// • Profile pictures aren't showing up. -// • Check that message expiration works. -// • Open group messages (sync messages). - internal enum MessageReceiver { internal enum Error : LocalizedError { diff --git a/SessionMessagingKit/Storage.swift b/SessionMessagingKit/Storage.swift index 62978ad4b..92476e886 100644 --- a/SessionMessagingKit/Storage.swift +++ b/SessionMessagingKit/Storage.swift @@ -29,6 +29,7 @@ public protocol SessionMessagingKitStorageProtocol { func markJobAsSucceeded(_ job: Job, using transaction: Any) func markJobAsFailed(_ job: Job, using transaction: Any) func getAllPendingJobs(of type: Job.Type) -> [Job] + func getAttachmentUploadJob(for attachmentID: String) -> AttachmentUploadJob? // MARK: - Authorization