Revert "WIP: download attachments in NSE"

This reverts commit 6fd5bbeab1.
This commit is contained in:
ryanzhao 2021-08-30 10:00:19 +10:00
parent 454003c027
commit e045808070
5 changed files with 6 additions and 63 deletions

View File

@ -32,7 +32,6 @@ target 'SessionNotificationServiceExtension' do
pod 'Curve25519Kit', git: 'https://github.com/signalapp/Curve25519Kit.git', :inhibit_warnings => true pod 'Curve25519Kit', git: 'https://github.com/signalapp/Curve25519Kit.git', :inhibit_warnings => true
pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true pod 'SignalCoreKit', git: 'https://github.com/signalapp/SignalCoreKit.git', :inhibit_warnings => true
pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true pod 'YapDatabase/SQLCipher', :git => 'https://github.com/loki-project/session-ios-yap-database.git', branch: 'signal-release', :inhibit_warnings => true
pod 'PromiseKit', :inhibit_warnings => true
end end
target 'SignalUtilitiesKit' do target 'SignalUtilitiesKit' do

View File

@ -73,20 +73,6 @@ extension Storage {
return result.first return result.first
} }
public func getAttachmentDownloadJob(for attachmentID: String) -> 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)
}
}
#if DEBUG
assert(result.isEmpty || result.count == 1)
#endif
return result.first
}
public func getAttachmentDownloadJobs(for threadID: String) -> [AttachmentDownloadJob] { public func getAttachmentDownloadJobs(for threadID: String) -> [AttachmentDownloadJob] {
var result: [AttachmentDownloadJob] = [] var result: [AttachmentDownloadJob] = []
Storage.read { transaction in Storage.read { transaction in

View File

@ -2,7 +2,6 @@ import Foundation
import SessionUtilitiesKit import SessionUtilitiesKit
import SessionSnodeKit import SessionSnodeKit
import SignalCoreKit import SignalCoreKit
import PromiseKit
public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
public let attachmentID: String public let attachmentID: String
@ -61,29 +60,17 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
// MARK: Running // MARK: Running
public func execute() { public func execute() {
executeAsync().retainUntilComplete()
}
public func executeAsync() -> Promise<Void> {
let (promise, seal) = Promise<Void>.pending()
if let id = id { if let id = id {
JobQueue.currentlyExecutingJobs.insert(id) JobQueue.currentlyExecutingJobs.insert(id)
} }
guard !isDeferred else { guard !isDeferred else { return }
seal.fulfill(())
return promise
}
if TSAttachment.fetch(uniqueId: attachmentID) is TSAttachmentStream { if TSAttachment.fetch(uniqueId: attachmentID) is TSAttachmentStream {
// FIXME: It's not clear * how * this happens, but apparently we can get to this point // FIXME: It's not clear * how * this happens, but apparently we can get to this point
// from time to time with an already downloaded attachment. // from time to time with an already downloaded attachment.
handleSuccess() return handleSuccess()
seal.fulfill(())
return promise
} }
guard let pointer = TSAttachment.fetch(uniqueId: attachmentID) as? TSAttachmentPointer else { guard let pointer = TSAttachment.fetch(uniqueId: attachmentID) as? TSAttachmentPointer else {
handleFailure(error: Error.noAttachment) return handleFailure(error: Error.noAttachment)
seal.reject(Error.noAttachment)
return promise
} }
let storage = SNMessagingKitConfiguration.shared.storage let storage = SNMessagingKitConfiguration.shared.storage
storage.write(with: { transaction in storage.write(with: { transaction in
@ -112,33 +99,24 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
} }
if let tsMessage = TSMessage.fetch(uniqueId: tsMessageID), let v2OpenGroup = storage.getV2OpenGroup(for: tsMessage.uniqueThreadId) { if let tsMessage = TSMessage.fetch(uniqueId: tsMessageID), let v2OpenGroup = storage.getV2OpenGroup(for: tsMessage.uniqueThreadId) {
guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else { guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else {
handleFailure(Error.invalidURL) return handleFailure(Error.invalidURL)
seal.reject(Error.invalidURL)
return promise
} }
OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure) self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
seal.fulfill(())
}.catch(on: DispatchQueue.global()) { error in }.catch(on: DispatchQueue.global()) { error in
handleFailure(error) handleFailure(error)
seal.reject(error)
} }
} else { } else {
guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else { guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else {
handleFailure(Error.invalidURL) return handleFailure(Error.invalidURL)
seal.reject(Error.invalidURL)
return promise
} }
let useOldServer = pointer.downloadURL.contains(FileServerAPIV2.oldServer) let useOldServer = pointer.downloadURL.contains(FileServerAPIV2.oldServer)
FileServerAPIV2.download(file, useOldServer: useOldServer).done(on: DispatchQueue.global(qos: .userInitiated)) { data in FileServerAPIV2.download(file, useOldServer: useOldServer).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure) self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
seal.fulfill(())
}.catch(on: DispatchQueue.global()) { error in }.catch(on: DispatchQueue.global()) { error in
handleFailure(error) handleFailure(error)
seal.reject(error)
} }
} }
return promise
} }
private func handleDownloadedAttachment(data: Data, temporaryFilePath: URL, pointer: TSAttachmentPointer, failureHandler: (Swift.Error) -> Void) { private func handleDownloadedAttachment(data: Data, temporaryFilePath: URL, pointer: TSAttachmentPointer, failureHandler: (Swift.Error) -> Void) {

View File

@ -33,7 +33,6 @@ 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 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

View File

@ -1,7 +1,6 @@
import UserNotifications import UserNotifications
import SessionMessagingKit import SessionMessagingKit
import SignalUtilitiesKit import SignalUtilitiesKit
import PromiseKit
public final class NotificationServiceExtension : UNNotificationServiceExtension { public final class NotificationServiceExtension : UNNotificationServiceExtension {
private var didPerformSetup = false private var didPerformSetup = false
@ -36,7 +35,6 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
} }
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) {
@ -71,14 +69,6 @@ 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 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) {
attachmentDownloadJobs.append(job)
}
}
case let unsendRequest as UnsendRequest: case let unsendRequest as UnsendRequest:
MessageReceiver.handleUnsendRequest(unsendRequest, using: transaction) MessageReceiver.handleUnsendRequest(unsendRequest, using: transaction)
return self.completeSilenty() return self.completeSilenty()
@ -106,16 +96,7 @@ 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)
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)
} }