Browse Source

Revert "WIP: download attachments in NSE"

This reverts commit 6fd5bbeab1.
pull/500/head
ryanzhao 3 months ago
parent
commit
e045808070
  1. 1
      Podfile
  2. 14
      SessionMessagingKit/Database/Storage+Jobs.swift
  3. 32
      SessionMessagingKit/Jobs/AttachmentDownloadJob.swift
  4. 1
      SessionMessagingKit/Storage.swift
  5. 21
      SessionNotificationServiceExtension/NotificationServiceExtension.swift

1
Podfile

@ -32,7 +32,6 @@ target 'SessionNotificationServiceExtension' do
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 '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
target 'SignalUtilitiesKit' do

14
SessionMessagingKit/Database/Storage+Jobs.swift

@ -73,20 +73,6 @@ extension Storage {
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] {
var result: [AttachmentDownloadJob] = []
Storage.read { transaction in

32
SessionMessagingKit/Jobs/AttachmentDownloadJob.swift

@ -2,7 +2,6 @@ import Foundation
import SessionUtilitiesKit
import SessionSnodeKit
import SignalCoreKit
import PromiseKit
public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
public let attachmentID: String
@ -61,29 +60,17 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
// MARK: Running
public func execute() {
executeAsync().retainUntilComplete()
}
public func executeAsync() -> Promise<Void> {
let (promise, seal) = Promise<Void>.pending()
if let id = id {
JobQueue.currentlyExecutingJobs.insert(id)
}
guard !isDeferred else {
seal.fulfill(())
return promise
}
guard !isDeferred else { return }
if TSAttachment.fetch(uniqueId: attachmentID) is TSAttachmentStream {
// 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.
handleSuccess()
seal.fulfill(())
return promise
return handleSuccess()
}
guard let pointer = TSAttachment.fetch(uniqueId: attachmentID) as? TSAttachmentPointer else {
handleFailure(error: Error.noAttachment)
seal.reject(Error.noAttachment)
return promise
return handleFailure(error: Error.noAttachment)
}
let storage = SNMessagingKitConfiguration.shared.storage
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) {
guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else {
handleFailure(Error.invalidURL)
seal.reject(Error.invalidURL)
return promise
return handleFailure(Error.invalidURL)
}
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)
seal.fulfill(())
}.catch(on: DispatchQueue.global()) { error in
handleFailure(error)
seal.reject(error)
}
} else {
guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else {
handleFailure(Error.invalidURL)
seal.reject(Error.invalidURL)
return promise
return handleFailure(Error.invalidURL)
}
let useOldServer = pointer.downloadURL.contains(FileServerAPIV2.oldServer)
FileServerAPIV2.download(file, useOldServer: useOldServer).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
seal.fulfill(())
}.catch(on: DispatchQueue.global()) { error in
handleFailure(error)
seal.reject(error)
}
}
return promise
}
private func handleDownloadedAttachment(data: Data, temporaryFilePath: URL, pointer: TSAttachmentPointer, failureHandler: (Swift.Error) -> Void) {

1
SessionMessagingKit/Storage.swift

@ -33,7 +33,6 @@ 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 getMessageSendJob(for messageSendJobID: String) -> MessageSendJob?
func resumeMessageSendJobIfNeeded(_ messageSendJobID: String)
func isJobCanceled(_ job: Job) -> Bool

21
SessionNotificationServiceExtension/NotificationServiceExtension.swift

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

Loading…
Cancel
Save