Fix PN sending from share extension

This commit is contained in:
Niels Andriesse 2020-12-07 10:04:38 +11:00
parent fa757e414b
commit 21ec051016
2 changed files with 25 additions and 5 deletions

View File

@ -34,18 +34,25 @@ public final class NotifyPNServerJob : NSObject, Job, NSCoding { // NSObject/NSC
// MARK: Running
public func execute() {
let _: Promise<Void> = execute()
}
public func execute() -> Promise<Void> {
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() {

View File

@ -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