From 21ec0510164b06035207ef969c4e9081861d6e21 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 7 Dec 2020 10:04:38 +1100 Subject: [PATCH] Fix PN sending from share extension --- .../Jobs/NotifyPNServerJob.swift | 13 ++++++++++--- .../Sending & Receiving/MessageSender.swift | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/SessionMessagingKit/Jobs/NotifyPNServerJob.swift b/SessionMessagingKit/Jobs/NotifyPNServerJob.swift index c26ed550c..2c506dc43 100644 --- a/SessionMessagingKit/Jobs/NotifyPNServerJob.swift +++ b/SessionMessagingKit/Jobs/NotifyPNServerJob.swift @@ -34,18 +34,25 @@ public final class NotifyPNServerJob : NSObject, Job, NSCoding { // NSObject/NSC // MARK: Running public func execute() { + let _: Promise = execute() + } + + public func execute() -> Promise { let server = PushNotificationAPI.server let parameters = [ "data" : message.data.description, "send_to" : message.recipient ] let url = URL(string: "\(server)/notify")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.global()) { + let promise = attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.global()) { OnionRequestAPI.sendOnionRequest(request, to: server, target: "/loki/v2/lsrpc", using: PushNotificationAPI.serverPublicKey).map { _ in } - }.done(on: DispatchQueue.global()) { // Intentionally capture self + } + let _ = promise.done(on: DispatchQueue.global()) { // Intentionally capture self self.handleSuccess() - }.catch(on: DispatchQueue.global()) { error in + } + promise.catch(on: DispatchQueue.global()) { error in self.handleFailure(error: error) } + return promise } private func handleSuccess() { diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index b2f982fc3..063075333 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -90,6 +90,10 @@ public final class MessageSender : NSObject { let storage = SNMessagingKitConfiguration.shared.storage let transaction = transaction as! YapDatabaseReadWriteTransaction let userPublicKey = storage.getUserPublicKey() + var isMainAppAndActive = false + if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") { + isMainAppAndActive = sharedUserDefaults.bool(forKey: "isMainAppActive") + } // Set the timestamp, sender and recipient if message.sentTimestamp == nil { // Visible messages will already have their sent timestamp set message.sentTimestamp = NSDate.millisecondTimestamp() @@ -223,9 +227,18 @@ public final class MessageSender : NSObject { } if shouldNotify { let notifyPNServerJob = NotifyPNServerJob(message: snodeMessage) - JobQueue.shared.add(notifyPNServerJob, using: transaction) + if isMainAppAndActive { + JobQueue.shared.add(notifyPNServerJob, using: transaction) + seal.fulfill(()) + } else { + notifyPNServerJob.execute().done(on: DispatchQueue.global(qos: .userInitiated)) { + seal.fulfill(()) + }.catch(on: DispatchQueue.global(qos: .userInitiated)) { _ in + seal.fulfill(()) // Always fulfill because the notify PN server job isn't critical. + } + } + } - seal.fulfill(()) }, completion: { }) } $0.catch(on: DispatchQueue.global(qos: .userInitiated)) { error in