WIP: make the attachment download work in NSE
This commit is contained in:
parent
6fd5bbeab1
commit
7295c14e17
|
@ -208,6 +208,6 @@ SPEC CHECKSUMS:
|
||||||
YYImage: 6db68da66f20d9f169ceb94dfb9947c3867b9665
|
YYImage: 6db68da66f20d9f169ceb94dfb9947c3867b9665
|
||||||
ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb
|
ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb
|
||||||
|
|
||||||
PODFILE CHECKSUM: 50e6a35c838ba28d2ee02bc6018fdd297c04e55f
|
PODFILE CHECKSUM: 71474628a9c2c3e57bdb0992668809a4369452f1
|
||||||
|
|
||||||
COCOAPODS: 1.10.1
|
COCOAPODS: 1.10.1
|
||||||
|
|
|
@ -73,14 +73,12 @@ extension Storage {
|
||||||
return result.first
|
return result.first
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getAttachmentDownloadJob(for attachmentID: String) -> AttachmentDownloadJob? {
|
public func getAttachmentDownloadJob(for attachmentID: String, with transaction: YapDatabaseReadTransaction) -> AttachmentDownloadJob? {
|
||||||
var result: [AttachmentDownloadJob] = []
|
var result: [AttachmentDownloadJob] = []
|
||||||
Storage.read { transaction in
|
|
||||||
transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in
|
transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in
|
||||||
guard let job = object as? AttachmentDownloadJob, job.attachmentID == attachmentID else { return }
|
guard let job = object as? AttachmentDownloadJob, job.attachmentID == attachmentID else { return }
|
||||||
result.append(job)
|
result.append(job)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
assert(result.isEmpty || result.count == 1)
|
assert(result.isEmpty || result.count == 1)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -5,7 +5,7 @@ import PromiseKit
|
||||||
public final class PushNotificationAPI : NSObject {
|
public final class PushNotificationAPI : NSObject {
|
||||||
|
|
||||||
// MARK: Settings
|
// MARK: Settings
|
||||||
public static let server = "https://live.apns.getsession.org"
|
public static let server = "https://dev.apns.getsession.org"
|
||||||
public static let serverPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
public static let serverPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
||||||
private static let maxRetryCount: UInt = 4
|
private static let maxRetryCount: UInt = 4
|
||||||
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60
|
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import PromiseKit
|
import PromiseKit
|
||||||
import Sodium
|
import Sodium
|
||||||
|
import YapDatabase
|
||||||
|
|
||||||
public protocol SessionMessagingKitStorageProtocol {
|
public protocol SessionMessagingKitStorageProtocol {
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ public protocol SessionMessagingKitStorageProtocol {
|
||||||
func markJobAsFailed(_ job: Job, using transaction: Any)
|
func markJobAsFailed(_ job: Job, using transaction: Any)
|
||||||
func getAllPendingJobs(of type: Job.Type) -> [Job]
|
func getAllPendingJobs(of type: Job.Type) -> [Job]
|
||||||
func getAttachmentUploadJob(for attachmentID: String) -> AttachmentUploadJob?
|
func getAttachmentUploadJob(for attachmentID: String) -> AttachmentUploadJob?
|
||||||
func getAttachmentDownloadJob(for attachmentID: String) -> AttachmentDownloadJob?
|
func getAttachmentDownloadJob(for attachmentID: String, with transaction: YapDatabaseReadTransaction) -> AttachmentDownloadJob?
|
||||||
func getMessageSendJob(for messageSendJobID: String) -> MessageSendJob?
|
func getMessageSendJob(for messageSendJobID: String) -> MessageSendJob?
|
||||||
func resumeMessageSendJobIfNeeded(_ messageSendJobID: String)
|
func resumeMessageSendJobIfNeeded(_ messageSendJobID: String)
|
||||||
func isJobCanceled(_ job: Job) -> Bool
|
func isJobCanceled(_ job: Job) -> Bool
|
||||||
|
|
|
@ -34,9 +34,9 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
|
||||||
let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else {
|
let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else {
|
||||||
return self.handleFailure(for: notificationContent)
|
return self.handleFailure(for: notificationContent)
|
||||||
}
|
}
|
||||||
|
var attachmentDownloadJobs: [AttachmentDownloadJob] = []
|
||||||
Storage.write { transaction in // Intentionally capture self
|
Storage.write { transaction in // Intentionally capture self
|
||||||
do {
|
do {
|
||||||
var attachmentDownloadJobs: [AttachmentDownloadJob] = []
|
|
||||||
let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction)
|
let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction)
|
||||||
let senderPublicKey = message.sender!
|
let senderPublicKey = message.sender!
|
||||||
if (senderPublicKey == userPublicKey) {
|
if (senderPublicKey == userPublicKey) {
|
||||||
|
@ -72,10 +72,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
|
||||||
// Store the notification ID for unsend requests to later cancel this notification
|
// Store the notification ID for unsend requests to later cancel this notification
|
||||||
tsIncomingMessage.setNotificationIdentifier(request.identifier, transaction: transaction)
|
tsIncomingMessage.setNotificationIdentifier(request.identifier, transaction: transaction)
|
||||||
let storage = SNMessagingKitConfiguration.shared.storage
|
let storage = SNMessagingKitConfiguration.shared.storage
|
||||||
let attachments = visibleMessage.attachmentIDs.compactMap { TSAttachment.fetch(uniqueId: $0) as? TSAttachmentPointer }
|
let attachments = visibleMessage.attachmentIDs.compactMap { TSAttachment.fetch(uniqueId: $0, transaction: transaction) as? TSAttachmentPointer }
|
||||||
let attachmentsToDownload = attachments.filter { !$0.isDownloaded }
|
let attachmentsToDownload = attachments.filter { !$0.isDownloaded }
|
||||||
attachmentsToDownload.forEach { attachment in
|
attachmentsToDownload.forEach { attachment in
|
||||||
if let attachmentID = attachment.uniqueId, let job = storage.getAttachmentDownloadJob(for: attachmentID) {
|
if let attachmentID = attachment.uniqueId, let job = storage.getAttachmentDownloadJob(for: attachmentID, with: transaction) {
|
||||||
attachmentDownloadJobs.append(job)
|
attachmentDownloadJobs.append(job)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,20 +106,21 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
|
||||||
notificationContent.body = "You've got a new message"
|
notificationContent.body = "You've got a new message"
|
||||||
default: break
|
default: break
|
||||||
}
|
}
|
||||||
if attachmentDownloadJobs.isEmpty {
|
|
||||||
self.handleSuccess(for: notificationContent)
|
|
||||||
} else {
|
|
||||||
let promises = attachmentDownloadJobs.map { $0.executeAsync() }
|
|
||||||
when(fulfilled: promises).map { attachments in
|
|
||||||
self.handleSuccess(for: notificationContent)
|
|
||||||
}.catch { error in
|
|
||||||
self.handleSuccess(for: notificationContent)
|
|
||||||
}.retainUntilComplete()
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
self.handleFailure(for: notificationContent)
|
self.handleFailure(for: notificationContent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if attachmentDownloadJobs.isEmpty {
|
||||||
|
// self.handleSuccess(for: notificationContent)
|
||||||
|
// } else {
|
||||||
|
// let promises = attachmentDownloadJobs.map { $0.executeAsync() }
|
||||||
|
// when(fulfilled: promises).map { attachments in
|
||||||
|
// self.handleSuccess(for: notificationContent)
|
||||||
|
// }.catch { error in
|
||||||
|
// self.handleSuccess(for: notificationContent)
|
||||||
|
// }.retainUntilComplete()
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
|
||||||
override public func serviceExtensionTimeWillExpire() {
|
override public func serviceExtensionTimeWillExpire() {
|
||||||
// Called just before the extension will be terminated by the system.
|
// Called just before the extension will be terminated by the system.
|
||||||
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
||||||
|
print("serviceExtensionTimeWillExpire")
|
||||||
let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
|
let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
|
||||||
let notificationContent = self.notificationContent!
|
let notificationContent = self.notificationContent!
|
||||||
notificationContent.userInfo = userInfo
|
notificationContent.userInfo = userInfo
|
||||||
|
|
Loading…
Reference in New Issue