diff --git a/Session/Settings/NukeDataModal.swift b/Session/Settings/NukeDataModal.swift index d8a791a70..d88e64fe8 100644 --- a/Session/Settings/NukeDataModal.swift +++ b/Session/Settings/NukeDataModal.swift @@ -5,6 +5,7 @@ import SessionUIKit import SessionSnodeKit import SessionMessagingKit import SignalUtilitiesKit +import PromiseKit final class NukeDataModal: Modal { // MARK: - Initialization @@ -162,49 +163,74 @@ final class NukeDataModal: Modal { private func clearEntireAccount(presentedViewController: UIViewController) { ModalActivityIndicatorViewController .present(fromViewController: presentedViewController, canCancel: false) { [weak self] _ in - SnodeAPI.clearAllData() - .done(on: DispatchQueue.main) { confirmations in - self?.dismiss(animated: true, completion: nil) // Dismiss the loader - - let potentiallyMaliciousSnodes = confirmations.compactMap { $0.value == false ? $0.key : nil } - - if potentiallyMaliciousSnodes.isEmpty { - self?.deleteAllLocalData() - } - else { - let message: String - if potentiallyMaliciousSnodes.count == 1 { - message = String(format: "dialog_clear_all_data_deletion_failed_1".localized(), potentiallyMaliciousSnodes[0]) + let servers: Set = Storage.shared.read { db in + try OpenGroup + .select(.server) + .distinct() + .asRequest(of: String.self) + .fetchSet(db) + + }.defaulting(to: []) + + var promises: [Promise<[String: Bool]>] = Storage.shared.read { db in + servers.map { server in + OpenGroupAPI + .clearInbox( + db, + on: server + ).map { _, response in + [server: true] } - else { - message = String(format: "dialog_clear_all_data_deletion_failed_2".localized(), String(potentiallyMaliciousSnodes.count), potentiallyMaliciousSnodes.joined(separator: ", ")) + } + }.defaulting(to: []) + + when(resolved: promises) + .done(on: DispatchQueue.main) { results in + SnodeAPI.clearAllData() + .done(on: DispatchQueue.main) { confirmations in + self?.dismiss(animated: true, completion: nil) // Dismiss the loader + + let potentiallyMaliciousSnodes: [String] = confirmations.compactMap { $0.value == false ? $0.key : nil } + + + if potentiallyMaliciousSnodes.isEmpty { + self?.deleteAllLocalData() + } + else { + let message: String + if potentiallyMaliciousSnodes.count == 1 { + message = String(format: "dialog_clear_all_data_deletion_failed_1".localized(), potentiallyMaliciousSnodes[0]) + } + else { + message = String(format: "dialog_clear_all_data_deletion_failed_2".localized(), String(potentiallyMaliciousSnodes.count), potentiallyMaliciousSnodes.joined(separator: ", ")) + } + + let modal: ConfirmationModal = ConfirmationModal( + targetView: self?.view, + info: ConfirmationModal.Info( + title: "ALERT_ERROR_TITLE".localized(), + explanation: message, + cancelTitle: "BUTTON_OK".localized(), + cancelStyle: .alert_text + ) + ) + self?.present(modal, animated: true) + } } - - let modal: ConfirmationModal = ConfirmationModal( - targetView: self?.view, - info: ConfirmationModal.Info( - title: "ALERT_ERROR_TITLE".localized(), - explanation: message, - cancelTitle: "BUTTON_OK".localized(), - cancelStyle: .alert_text + .catch(on: DispatchQueue.main) { error in + self?.dismiss(animated: true, completion: nil) // Dismiss the loader + + let modal: ConfirmationModal = ConfirmationModal( + targetView: self?.view, + info: ConfirmationModal.Info( + title: "ALERT_ERROR_TITLE".localized(), + explanation: error.localizedDescription, + cancelTitle: "BUTTON_OK".localized(), + cancelStyle: .alert_text + ) ) - ) - self?.present(modal, animated: true) - } - } - .catch(on: DispatchQueue.main) { error in - self?.dismiss(animated: true, completion: nil) // Dismiss the loader - - let modal: ConfirmationModal = ConfirmationModal( - targetView: self?.view, - info: ConfirmationModal.Info( - title: "ALERT_ERROR_TITLE".localized(), - explanation: error.localizedDescription, - cancelTitle: "BUTTON_OK".localized(), - cancelStyle: .alert_text - ) - ) - self?.present(modal, animated: true) + self?.present(modal, animated: true) + } } } } diff --git a/SessionMessagingKit/Open Groups/OpenGroupAPI.swift b/SessionMessagingKit/Open Groups/OpenGroupAPI.swift index 051ba7cc2..177c2b5de 100644 --- a/SessionMessagingKit/Open Groups/OpenGroupAPI.swift +++ b/SessionMessagingKit/Open Groups/OpenGroupAPI.swift @@ -948,6 +948,25 @@ public enum OpenGroupAPI { .decoded(as: [DirectMessage]?.self, on: OpenGroupAPI.workQueue, using: dependencies) } + /// Remove all message requests from inbox, this methrod will return the number of messages deleted + public static func clearInbox( + _ db: Database, + on server: String, + using dependencies: SMKDependencies = SMKDependencies() + ) -> Promise<(OnionRequestResponseInfoType, [String: Int])> { + return OpenGroupAPI + .send( + db, + request: Request( + method: .delete, + server: server, + endpoint: .inbox + ), + using: dependencies + ) + .decoded(as: [String: Int].self, on: OpenGroupAPI.workQueue, using: dependencies) + } + /// Delivers a direct message to a user via their blinded Session ID /// /// The body of this request is a JSON object containing a message key with a value of the encrypted-then-base64-encoded message to deliver