clear open group inbox when clear account data

pull/941/head
ryanzhao 3 years ago
parent 123c458c32
commit c4e6f3fe47

@ -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<String> = 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)
}
}
}
}

@ -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<NoBody, Endpoint>(
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

Loading…
Cancel
Save