WIP: make the attachment download work in NSE

This commit is contained in:
ryanzhao 2021-08-27 13:32:31 +10:00
parent 6fd5bbeab1
commit 7295c14e17
5 changed files with 23 additions and 22 deletions

View file

@ -208,6 +208,6 @@ SPEC CHECKSUMS:
YYImage: 6db68da66f20d9f169ceb94dfb9947c3867b9665
ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb
PODFILE CHECKSUM: 50e6a35c838ba28d2ee02bc6018fdd297c04e55f
PODFILE CHECKSUM: 71474628a9c2c3e57bdb0992668809a4369452f1
COCOAPODS: 1.10.1

View file

@ -73,13 +73,11 @@ extension Storage {
return result.first
}
public func getAttachmentDownloadJob(for attachmentID: String) -> AttachmentDownloadJob? {
public func getAttachmentDownloadJob(for attachmentID: String, with transaction: YapDatabaseReadTransaction) -> AttachmentDownloadJob? {
var result: [AttachmentDownloadJob] = []
Storage.read { transaction in
transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in
guard let job = object as? AttachmentDownloadJob, job.attachmentID == attachmentID else { return }
result.append(job)
}
transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in
guard let job = object as? AttachmentDownloadJob, job.attachmentID == attachmentID else { return }
result.append(job)
}
#if DEBUG
assert(result.isEmpty || result.count == 1)

View file

@ -5,7 +5,7 @@ import PromiseKit
public final class PushNotificationAPI : NSObject {
// 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"
private static let maxRetryCount: UInt = 4
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60

View file

@ -1,5 +1,6 @@
import PromiseKit
import Sodium
import YapDatabase
public protocol SessionMessagingKitStorageProtocol {
@ -33,7 +34,7 @@ public protocol SessionMessagingKitStorageProtocol {
func markJobAsFailed(_ job: Job, using transaction: Any)
func getAllPendingJobs(of type: Job.Type) -> [Job]
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 resumeMessageSendJobIfNeeded(_ messageSendJobID: String)
func isJobCanceled(_ job: Job) -> Bool

View file

@ -34,9 +34,9 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else {
return self.handleFailure(for: notificationContent)
}
var attachmentDownloadJobs: [AttachmentDownloadJob] = []
Storage.write { transaction in // Intentionally capture self
do {
var attachmentDownloadJobs: [AttachmentDownloadJob] = []
let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction)
let senderPublicKey = message.sender!
if (senderPublicKey == userPublicKey) {
@ -72,10 +72,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
// Store the notification ID for unsend requests to later cancel this notification
tsIncomingMessage.setNotificationIdentifier(request.identifier, transaction: transaction)
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 }
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)
}
}
@ -106,20 +106,21 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
notificationContent.body = "You've got a new message"
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 {
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() {
// 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.
print("serviceExtensionTimeWillExpire")
let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
let notificationContent = self.notificationContent!
notificationContent.userInfo = userInfo