2017-05-27 03:19:46 +02:00
//
2018-01-30 21:49:36 +01:00
// C o p y r i g h t ( c ) 2 0 1 8 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
2017-05-27 03:19:46 +02:00
//
import Foundation
2017-11-28 00:17:46 +01:00
import SignalServiceKit
2017-05-27 03:19:46 +02:00
2018-05-25 22:51:40 +02:00
@objc
public class SafetyNumberConfirmationAlert : NSObject {
2017-05-27 03:19:46 +02:00
let TAG = " [SafetyNumberConfirmationAlert] "
private let contactsManager : OWSContactsManager
2018-03-05 15:30:58 +01:00
private let primaryStorage : OWSPrimaryStorage
2017-05-27 03:19:46 +02:00
init ( contactsManager : OWSContactsManager ) {
self . contactsManager = contactsManager
2018-03-05 15:30:58 +01:00
self . primaryStorage = OWSPrimaryStorage . shared ( )
2017-05-27 03:19:46 +02:00
}
2018-05-25 22:51:40 +02:00
@objc
2017-06-06 21:52:30 +02:00
public class func presentAlertIfNecessary ( recipientId : String , confirmationText : String , contactsManager : OWSContactsManager , completion : @ escaping ( Bool ) -> Void ) -> Bool {
2018-02-21 01:52:49 +01:00
return self . presentAlertIfNecessary ( recipientIds : [ recipientId ] , confirmationText : confirmationText , contactsManager : contactsManager , completion : completion , beforePresentationHandler : nil )
}
2018-05-25 22:51:40 +02:00
@objc
2018-02-21 01:52:49 +01:00
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 )
2017-05-27 03:19:46 +02:00
}
2018-05-25 22:51:40 +02:00
@objc
2017-06-06 21:52:30 +02:00
public class func presentAlertIfNecessary ( recipientIds : [ String ] , confirmationText : String , contactsManager : OWSContactsManager , completion : @ escaping ( Bool ) -> Void ) -> Bool {
2018-02-21 01:52:49 +01:00
return self . presentAlertIfNecessary ( recipientIds : recipientIds , confirmationText : confirmationText , contactsManager : contactsManager , completion : completion , beforePresentationHandler : nil )
}
2018-05-25 22:51:40 +02:00
@objc
2018-02-21 01:52:49 +01:00
public class func presentAlertIfNecessary ( recipientIds : [ String ] , confirmationText : String , contactsManager : OWSContactsManager , completion : @ escaping ( Bool ) -> Void , beforePresentationHandler : ( ( ) -> Void ) ? = nil ) -> Bool {
2017-06-01 03:12:18 +02:00
return SafetyNumberConfirmationAlert ( contactsManager : contactsManager ) . presentIfNecessary ( recipientIds : recipientIds ,
2017-05-27 03:19:46 +02:00
confirmationText : confirmationText ,
2018-02-21 01:52:49 +01:00
completion : completion ,
beforePresentationHandler : beforePresentationHandler )
2017-05-27 03:19:46 +02:00
}
/* *
* 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
*/
2018-02-21 01:52:49 +01:00
public func presentIfNecessary ( recipientIds : [ String ] , confirmationText : String , completion : @ escaping ( Bool ) -> Void , beforePresentationHandler : ( ( ) -> Void ) ? = nil ) -> Bool {
2017-05-27 03:19:46 +02:00
2017-06-07 00:59:38 +02:00
guard let untrustedIdentity = untrustedIdentityForSending ( recipientIds : recipientIds ) else {
2017-05-27 03:19:46 +02:00
// N o i d e n t i t i e s t o c o n f i r m , n o a l e r t t o p r e s e n t .
return false
}
2017-06-07 00:59:38 +02:00
let displayName = contactsManager . displayName ( forPhoneIdentifier : untrustedIdentity . recipientId )
2017-05-27 03:19:46 +02:00
let titleFormat = NSLocalizedString ( " CONFIRM_SENDING_TO_CHANGED_IDENTITY_TITLE_FORMAT " ,
2018-03-26 15:37:43 +02:00
comment : " Action sheet title presented when a user's SN has recently changed. Embeds {{contact's name or phone number}} " )
2017-05-27 03:19:46 +02:00
let title = String ( format : titleFormat , displayName )
let bodyFormat = NSLocalizedString ( " CONFIRM_SENDING_TO_CHANGED_IDENTITY_BODY_FORMAT " ,
2018-03-26 15:37:43 +02:00
comment : " Action sheet body presented when a user's SN has recently changed. Embeds {{contact's name or phone number}} " )
2017-05-27 03:19:46 +02:00
let body = String ( format : bodyFormat , displayName )
2018-03-05 15:30:58 +01:00
let actionSheetController = UIAlertController ( title : title , message : body , preferredStyle : . actionSheet )
2017-05-27 03:19:46 +02:00
let confirmAction = UIAlertAction ( title : confirmationText , style : . default ) { _ in
2017-06-07 00:59:38 +02:00
Logger . info ( " \( self . TAG ) Confirmed identity: \( untrustedIdentity ) " )
2017-06-01 03:12:18 +02:00
2018-03-05 15:30:58 +01:00
self . primaryStorage . newDatabaseConnection ( ) . asyncReadWrite { ( transaction ) in
2018-02-02 20:21:35 +01:00
OWSIdentityManager . shared ( ) . setVerificationState ( . default , identityKey : untrustedIdentity . identityKey , recipientId : untrustedIdentity . recipientId , isUserInitiatedChange : true , transaction : transaction )
2017-05-27 03:19:46 +02:00
DispatchQueue . main . async {
completion ( true )
}
}
}
actionSheetController . addAction ( confirmAction )
2017-06-09 22:58:00 +02:00
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
2017-06-07 00:59:38 +02:00
Logger . info ( " \( self . TAG ) Opted to show Safety Number for identity: \( untrustedIdentity ) " )
2017-05-27 03:19:46 +02:00
2017-06-07 00:59:38 +02:00
self . presentSafetyNumberViewController ( theirIdentityKey : untrustedIdentity . identityKey ,
theirRecipientId : untrustedIdentity . recipientId ,
2017-05-27 03:19:46 +02:00
theirDisplayName : displayName ,
completion : { completion ( false ) } )
}
actionSheetController . addAction ( showSafetyNumberAction )
2018-02-21 01:52:49 +01:00
// W e c a n ' t u s e t h e d e f a u l t ` O W S A l e r t s . c a n c e l A c t i o n ` b e c a u s e w e n e e d t o s p e c i f y t h a t t h e c o m p l e t i o n
// h a n d l e r i s c a l l e d .
let cancelAction = UIAlertAction ( title : CommonStrings . cancelButton , style : . cancel ) { _ in
Logger . info ( " \( self . logTag ) user canceled. " )
completion ( false )
}
actionSheetController . addAction ( cancelAction )
beforePresentationHandler ? ( )
2017-05-27 03:19:46 +02:00
2017-06-01 03:12:18 +02:00
UIApplication . shared . frontmostViewController ? . present ( actionSheetController , animated : true )
2017-05-27 03:19:46 +02:00
return true
}
2017-06-01 03:12:18 +02:00
public func presentSafetyNumberViewController ( theirIdentityKey : Data , theirRecipientId : String , theirDisplayName : String , completion : ( ( ) -> Void ) ? = nil ) {
2017-06-09 22:58:00 +02:00
guard let fromViewController = UIApplication . shared . frontmostViewController else {
Logger . info ( " \( self . TAG ) Missing frontmostViewController " )
return
}
2018-03-05 15:30:58 +01:00
FingerprintViewController . present ( from : fromViewController , recipientId : theirRecipientId )
2017-05-27 03:19:46 +02:00
}
2017-06-07 00:59:38 +02:00
private func untrustedIdentityForSending ( recipientIds : [ String ] ) -> OWSRecipientIdentity ? {
2018-06-01 20:20:48 +02:00
return recipientIds . compactMap {
2017-06-07 00:59:38 +02:00
OWSIdentityManager . shared ( ) . untrustedIdentityForSending ( toRecipientId : $0 )
2017-06-06 21:44:20 +02:00
} . first
2017-05-27 03:19:46 +02:00
}
}