mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
6.3 KiB
Swift
118 lines
6.3 KiB
Swift
8 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
|
|
||
|
import Foundation
|
||
5 years ago
|
import SignalUtilitiesKit
|
||
8 years ago
|
|
||
7 years ago
|
@objc
|
||
|
public class SafetyNumberConfirmationAlert: NSObject {
|
||
8 years ago
|
|
||
|
private let contactsManager: OWSContactsManager
|
||
7 years ago
|
private let primaryStorage: OWSPrimaryStorage
|
||
8 years ago
|
|
||
|
init(contactsManager: OWSContactsManager) {
|
||
|
self.contactsManager = contactsManager
|
||
7 years ago
|
self.primaryStorage = OWSPrimaryStorage.shared()
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
8 years ago
|
public class func presentAlertIfNecessary(recipientId: String, confirmationText: String, contactsManager: OWSContactsManager, completion: @escaping (Bool) -> Void) -> Bool {
|
||
7 years ago
|
return self.presentAlertIfNecessary(recipientIds: [recipientId], confirmationText: confirmationText, contactsManager: contactsManager, completion: completion, beforePresentationHandler: nil)
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public class func presentAlertIfNecessary(recipientId: String, confirmationText: String, contactsManager: OWSContactsManager, completion: @escaping (Bool) -> Void, beforePresentationHandler: (() -> Void)? = nil) -> Bool {
|
||
|
return self.presentAlertIfNecessary(recipientIds: [recipientId], confirmationText: confirmationText, contactsManager: contactsManager, completion: completion, beforePresentationHandler: beforePresentationHandler)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
8 years ago
|
public class func presentAlertIfNecessary(recipientIds: [String], confirmationText: String, contactsManager: OWSContactsManager, completion: @escaping (Bool) -> Void) -> Bool {
|
||
7 years ago
|
return self.presentAlertIfNecessary(recipientIds: recipientIds, confirmationText: confirmationText, contactsManager: contactsManager, completion: completion, beforePresentationHandler: nil)
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public class func presentAlertIfNecessary(recipientIds: [String], confirmationText: String, contactsManager: OWSContactsManager, completion: @escaping (Bool) -> Void, beforePresentationHandler: (() -> Void)? = nil) -> Bool {
|
||
8 years ago
|
return SafetyNumberConfirmationAlert(contactsManager: contactsManager).presentIfNecessary(recipientIds: recipientIds,
|
||
8 years ago
|
confirmationText: confirmationText,
|
||
7 years ago
|
completion: completion,
|
||
|
beforePresentationHandler: beforePresentationHandler)
|
||
8 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Shows confirmation dialog if at least one of the recipient id's is not confirmed.
|
||
|
*
|
||
|
* @returns true if an alert was shown
|
||
|
* false if there were no unconfirmed identities
|
||
|
*/
|
||
7 years ago
|
public func presentIfNecessary(recipientIds: [String], confirmationText: String, completion: @escaping (Bool) -> Void, beforePresentationHandler: (() -> Void)? = nil) -> Bool {
|
||
8 years ago
|
|
||
8 years ago
|
guard let untrustedIdentity = untrustedIdentityForSending(recipientIds: recipientIds) else {
|
||
8 years ago
|
// No identities to confirm, no alert to present.
|
||
|
return false
|
||
|
}
|
||
|
|
||
8 years ago
|
let displayName = contactsManager.displayName(forPhoneIdentifier: untrustedIdentity.recipientId)
|
||
8 years ago
|
|
||
|
let titleFormat = NSLocalizedString("CONFIRM_SENDING_TO_CHANGED_IDENTITY_TITLE_FORMAT",
|
||
7 years ago
|
comment: "Action sheet title presented when a user's SN has recently changed. Embeds {{contact's name or phone number}}")
|
||
8 years ago
|
let title = String(format: titleFormat, displayName)
|
||
|
|
||
|
let bodyFormat = NSLocalizedString("CONFIRM_SENDING_TO_CHANGED_IDENTITY_BODY_FORMAT",
|
||
7 years ago
|
comment: "Action sheet body presented when a user's SN has recently changed. Embeds {{contact's name or phone number}}")
|
||
8 years ago
|
let body = String(format: bodyFormat, displayName)
|
||
|
|
||
6 years ago
|
let actionSheet = UIAlertController(title: title, message: body, preferredStyle: .actionSheet)
|
||
8 years ago
|
|
||
|
let confirmAction = UIAlertAction(title: confirmationText, style: .default) { _ in
|
||
7 years ago
|
Logger.info("Confirmed identity: \(untrustedIdentity)")
|
||
8 years ago
|
|
||
7 years ago
|
self.primaryStorage.newDatabaseConnection().asyncReadWrite { (transaction) in
|
||
7 years ago
|
OWSIdentityManager.shared().setVerificationState(.default, identityKey: untrustedIdentity.identityKey, recipientId: untrustedIdentity.recipientId, isUserInitiatedChange: true, transaction: transaction)
|
||
8 years ago
|
DispatchQueue.main.async {
|
||
|
completion(true)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
actionSheet.addAction(confirmAction)
|
||
8 years ago
|
|
||
8 years ago
|
let showSafetyNumberAction = UIAlertAction(title: NSLocalizedString("VERIFY_PRIVACY", comment: "Label for button or row which allows users to verify the safety number of another user."), style: .default) { _ in
|
||
7 years ago
|
Logger.info("Opted to show Safety Number for identity: \(untrustedIdentity)")
|
||
8 years ago
|
|
||
8 years ago
|
self.presentSafetyNumberViewController(theirIdentityKey: untrustedIdentity.identityKey,
|
||
|
theirRecipientId: untrustedIdentity.recipientId,
|
||
8 years ago
|
theirDisplayName: displayName,
|
||
|
completion: { completion(false) })
|
||
|
|
||
|
}
|
||
6 years ago
|
actionSheet.addAction(showSafetyNumberAction)
|
||
8 years ago
|
|
||
7 years ago
|
// We can't use the default `OWSAlerts.cancelAction` because we need to specify that the completion
|
||
|
// handler is called.
|
||
|
let cancelAction = UIAlertAction(title: CommonStrings.cancelButton, style: .cancel) { _ in
|
||
7 years ago
|
Logger.info("user canceled.")
|
||
7 years ago
|
completion(false)
|
||
|
}
|
||
6 years ago
|
actionSheet.addAction(cancelAction)
|
||
7 years ago
|
|
||
|
beforePresentationHandler?()
|
||
8 years ago
|
|
||
6 years ago
|
UIApplication.shared.frontmostViewController?.presentAlert(actionSheet)
|
||
8 years ago
|
return true
|
||
|
}
|
||
|
|
||
8 years ago
|
public func presentSafetyNumberViewController(theirIdentityKey: Data, theirRecipientId: String, theirDisplayName: String, completion: (() -> Void)? = nil) {
|
||
8 years ago
|
guard let fromViewController = UIApplication.shared.frontmostViewController else {
|
||
7 years ago
|
Logger.info("Missing frontmostViewController")
|
||
8 years ago
|
return
|
||
|
}
|
||
7 years ago
|
FingerprintViewController.present(from: fromViewController, recipientId: theirRecipientId)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
private func untrustedIdentityForSending(recipientIds: [String]) -> OWSRecipientIdentity? {
|
||
7 years ago
|
return recipientIds.compactMap {
|
||
8 years ago
|
OWSIdentityManager.shared().untrustedIdentityForSending(toRecipientId: $0)
|
||
8 years ago
|
}.first
|
||
8 years ago
|
}
|
||
|
}
|