From 454003c02799ee07c4b904bc3af4a91d462a9da8 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 30 Aug 2021 10:00:04 +1000 Subject: [PATCH] Revert "WIP: make the attachment download work in NSE" This reverts commit 7295c14e17ba314b9336bf1e2a96cc32279ae0df. --- Podfile.lock | 2 +- .../Database/Storage+Jobs.swift | 10 ++++--- .../Notifications/PushNotificationAPI.swift | 2 +- SessionMessagingKit/Storage.swift | 3 +- .../NotificationServiceExtension.swift | 28 +++++++++---------- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 1b4e85988..804a148c7 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -208,6 +208,6 @@ SPEC CHECKSUMS: YYImage: 6db68da66f20d9f169ceb94dfb9947c3867b9665 ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb -PODFILE CHECKSUM: 71474628a9c2c3e57bdb0992668809a4369452f1 +PODFILE CHECKSUM: 50e6a35c838ba28d2ee02bc6018fdd297c04e55f COCOAPODS: 1.10.1 diff --git a/SessionMessagingKit/Database/Storage+Jobs.swift b/SessionMessagingKit/Database/Storage+Jobs.swift index 1cb71172d..e504fee0e 100644 --- a/SessionMessagingKit/Database/Storage+Jobs.swift +++ b/SessionMessagingKit/Database/Storage+Jobs.swift @@ -73,11 +73,13 @@ extension Storage { return result.first } - public func getAttachmentDownloadJob(for attachmentID: String, with transaction: YapDatabaseReadTransaction) -> AttachmentDownloadJob? { + public func getAttachmentDownloadJob(for attachmentID: String) -> AttachmentDownloadJob? { var result: [AttachmentDownloadJob] = [] - transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in - guard let job = object as? AttachmentDownloadJob, job.attachmentID == attachmentID else { return } - result.append(job) + 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) + } } #if DEBUG assert(result.isEmpty || result.count == 1) diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift index c44649c60..8fccb96ec 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift +++ b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift @@ -5,7 +5,7 @@ import PromiseKit public final class PushNotificationAPI : NSObject { // MARK: Settings - public static let server = "https://dev.apns.getsession.org" + public static let server = "https://live.apns.getsession.org" public static let serverPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049" private static let maxRetryCount: UInt = 4 private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60 diff --git a/SessionMessagingKit/Storage.swift b/SessionMessagingKit/Storage.swift index f523f34e8..75df638df 100644 --- a/SessionMessagingKit/Storage.swift +++ b/SessionMessagingKit/Storage.swift @@ -1,6 +1,5 @@ import PromiseKit import Sodium -import YapDatabase public protocol SessionMessagingKitStorageProtocol { @@ -34,7 +33,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, with transaction: YapDatabaseReadTransaction) -> AttachmentDownloadJob? + func getAttachmentDownloadJob(for attachmentID: String) -> AttachmentDownloadJob? func getMessageSendJob(for messageSendJobID: String) -> MessageSendJob? func resumeMessageSendJobIfNeeded(_ messageSendJobID: String) func isJobCanceled(_ job: Job) -> Bool diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index 06a5d1de2..d904dd902 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -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, transaction: transaction) as? TSAttachmentPointer } + let attachments = visibleMessage.attachmentIDs.compactMap { TSAttachment.fetch(uniqueId: $0) as? TSAttachmentPointer } let attachmentsToDownload = attachments.filter { !$0.isDownloaded } attachmentsToDownload.forEach { attachment in - if let attachmentID = attachment.uniqueId, let job = storage.getAttachmentDownloadJob(for: attachmentID, with: transaction) { + if let attachmentID = attachment.uniqueId, let job = storage.getAttachmentDownloadJob(for: attachmentID) { attachmentDownloadJobs.append(job) } } @@ -106,21 +106,20 @@ 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() -// } } } @@ -164,7 +163,6 @@ 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