// // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import "OWSContactsManager.h" #import "Environment.h" #import "Signal-Swift.h" #import "SignalAccount.h" #import "Util.h" #import #import @import Contacts; NSString *const OWSContactsManagerSignalAccountsDidChangeNotification = @"OWSContactsManagerSignalAccountsDidChangeNotification"; @interface OWSContactsManager () @property (atomic) id addressBookReference; @property (atomic) TOCFuture *futureAddressBook; @property (nonatomic) BOOL isContactsUpdateInFlight; // This reflects the contents of the device phone book and includes // contacts that do not correspond to any signal account. @property (atomic) NSArray *allContacts; @property (atomic) NSDictionary *allContactsMap; @property (atomic) NSArray *signalAccounts; @property (atomic) NSDictionary *signalAccountMap; @property (nonatomic, readonly) SystemContactsFetcher *systemContactsFetcher; @end @implementation OWSContactsManager - (id)init { self = [super init]; if (!self) { return self; } _avatarCache = [NSCache new]; _allContacts = @[]; _signalAccountMap = @{}; _signalAccounts = @[]; _systemContactsFetcher = [SystemContactsFetcher new]; _systemContactsFetcher.delegate = self; OWSSingletonAssert(); return self; } #pragma mark - System Contact Fetching // Request contacts access if you haven't asked recently. - (void)requestSystemContactsOnce { [self.systemContactsFetcher requestOnce]; } - (void)fetchSystemContactsIfAlreadyAuthorized { [self.systemContactsFetcher fetchIfAlreadyAuthorized]; } - (BOOL)isSystemContactsAuthorized { return self.systemContactsFetcher.isAuthorized; } #pragma mark SystemContactsFetcherDelegate - (void)systemContactsFetcher:(SystemContactsFetcher *)systemsContactsFetcher updatedContacts:(NSArray *)contacts { [self updateWithContacts:contacts]; } #pragma mark - Intersection - (void)intersectContacts { [self intersectContactsWithRetryDelay:1]; } - (void)intersectContactsWithRetryDelay:(double)retryDelaySeconds { void (^success)() = ^{ DDLogInfo(@"%@ Successfully intersected contacts.", self.tag); [self updateSignalAccounts]; }; void (^failure)(NSError *error) = ^(NSError *error) { if ([error.domain isEqualToString:OWSSignalServiceKitErrorDomain] && error.code == OWSErrorCodeContactsUpdaterRateLimit) { DDLogError(@"Contact intersection hit rate limit with error: %@", error); return; } DDLogWarn(@"%@ Failed to intersect contacts with error: %@. Rescheduling", self.tag, error); // Retry with exponential backoff. // // TODO: Abort if another contact intersection succeeds in the meantime. dispatch_after( dispatch_time(DISPATCH_TIME_NOW, (int64_t)(retryDelaySeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self intersectContactsWithRetryDelay:retryDelaySeconds * 2]; }); }; [[ContactsUpdater sharedUpdater] updateSignalContactIntersectionWithABContacts:self.allContacts success:success failure:failure]; } - (void)updateWithContacts:(NSArray *)contacts { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableDictionary *allContactsMap = [NSMutableDictionary new]; for (Contact *contact in contacts) { for (PhoneNumber *phoneNumber in contact.parsedPhoneNumbers) { NSString *phoneNumberE164 = phoneNumber.toE164; if (phoneNumberE164.length > 0) { allContactsMap[phoneNumberE164] = contact; } } } dispatch_async(dispatch_get_main_queue(), ^{ self.allContacts = contacts; self.allContactsMap = [allContactsMap copy]; [self.avatarCache removeAllObjects]; [self intersectContacts]; [self updateSignalAccounts]; }); }); } - (void)updateSignalAccounts { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableDictionary *signalAccountMap = [NSMutableDictionary new]; NSMutableArray *signalAccounts = [NSMutableArray new]; NSArray *contacts = self.allContacts; // We use a transaction only to load the SignalRecipients for each contact, // in order to avoid database deadlock. NSMutableDictionary *> *contactIdToSignalRecipientsMap = [NSMutableDictionary new]; [[TSStorageManager sharedManager].dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { for (Contact *contact in contacts) { NSArray *signalRecipients = [contact signalRecipientsWithTransaction:transaction]; contactIdToSignalRecipientsMap[contact.uniqueId] = signalRecipients; } }]; for (Contact *contact in contacts) { NSArray *signalRecipients = contactIdToSignalRecipientsMap[contact.uniqueId]; for (SignalRecipient *signalRecipient in [signalRecipients sortedArrayUsingSelector:@selector(compare:)]) { SignalAccount *signalAccount = [[SignalAccount alloc] initWithSignalRecipient:signalRecipient]; signalAccount.contact = contact; if (signalRecipients.count > 1) { signalAccount.hasMultipleAccountContact = YES; signalAccount.multipleAccountLabelText = [[self class] accountLabelForContact:contact recipientId:signalRecipient.recipientId]; } if (signalAccountMap[signalAccount.recipientId]) { DDLogInfo(@"Ignoring duplicate contact: %@, %@", signalAccount.recipientId, contact.fullName); continue; } signalAccountMap[signalAccount.recipientId] = signalAccount; [signalAccounts addObject:signalAccount]; } } dispatch_async(dispatch_get_main_queue(), ^{ self.signalAccountMap = [signalAccountMap copy]; self.signalAccounts = [signalAccounts copy]; [[NSNotificationCenter defaultCenter] postNotificationName:OWSContactsManagerSignalAccountsDidChangeNotification object:nil]; }); }); } #pragma mark - View Helpers // TODO move into Contact class. + (NSString *)accountLabelForContact:(Contact *)contact recipientId:(NSString *)recipientId { OWSAssert(contact); OWSAssert(recipientId.length > 0); OWSAssert([contact.textSecureIdentifiers containsObject:recipientId]); if (contact.textSecureIdentifiers.count <= 1) { return nil; } // 1. Find the phone number type of this account. OWSPhoneNumberType phoneNumberType = [contact phoneNumberTypeForPhoneNumber:recipientId]; NSString *phoneNumberLabel; switch (phoneNumberType) { case OWSPhoneNumberTypeMobile: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_MOBILE", @"Label for 'Mobile' phone numbers."); break; case OWSPhoneNumberTypeIPhone: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_IPHONE", @"Label for 'IPhone' phone numbers."); break; case OWSPhoneNumberTypeMain: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_MAIN", @"Label for 'Main' phone numbers."); break; case OWSPhoneNumberTypeHomeFAX: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_HOME_FAX", @"Label for 'HomeFAX' phone numbers."); break; case OWSPhoneNumberTypeWorkFAX: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_WORK_FAX", @"Label for 'Work FAX' phone numbers."); break; case OWSPhoneNumberTypeOtherFAX: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_OTHER_FAX", @"Label for 'Other FAX' phone numbers."); break; case OWSPhoneNumberTypePager: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_PAGER", @"Label for 'Pager' phone numbers."); break; case OWSPhoneNumberTypeUnknown: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_UNKNOWN", @"Label used when we don't what kind of phone number it is (e.g. mobile/work/home)."); break; case OWSPhoneNumberTypeHome: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_HOME", @"Label for 'Home' phone numbers."); break; case OWSPhoneNumberTypeWork: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_WORK", @"Label for 'Work' phone numbers."); break; case OWSPhoneNumberTypeOther: phoneNumberLabel = NSLocalizedString(@"PHONE_NUMBER_TYPE_OTHER", @"Label for 'Other' phone numbers."); break; } // 2. Find all phone numbers for this contact of the same type. NSMutableArray *phoneNumbersOfTheSameType = [NSMutableArray new]; for (NSString *textSecureIdentifier in contact.textSecureIdentifiers) { if (phoneNumberType == [contact phoneNumberTypeForPhoneNumber:textSecureIdentifier]) { [phoneNumbersOfTheSameType addObject:textSecureIdentifier]; } } OWSAssert([phoneNumbersOfTheSameType containsObject:recipientId]); if (phoneNumbersOfTheSameType.count > 1) { NSUInteger index = [[phoneNumbersOfTheSameType sortedArrayUsingSelector:@selector(compare:)] indexOfObject:recipientId]; phoneNumberLabel = [NSString stringWithFormat:NSLocalizedString(@"PHONE_NUMBER_TYPE_AND_INDEX_FORMAT", @"Format for phone number label with an index. Embeds {{Phone number label " @"(e.g. 'home')}} and {{index, e.g. 2}}."), phoneNumberLabel, (int)index]; } return phoneNumberLabel; } - (BOOL)phoneNumber:(PhoneNumber *)phoneNumber1 matchesNumber:(PhoneNumber *)phoneNumber2 { return [phoneNumber1.toE164 isEqualToString:phoneNumber2.toE164]; } #pragma mark - Whisper User Management - (NSArray *)getSignalUsersFromContactsArray:(NSArray *)contacts { NSMutableDictionary *signalContacts = [NSMutableDictionary new]; for (Contact *contact in contacts) { if ([contact isSignalContact]) { signalContacts[contact.textSecureIdentifiers.firstObject] = contact; } } return [signalContacts.allValues sortedArrayUsingComparator:[[self class] contactComparator]]; } + (NSComparator)contactComparator { BOOL firstNameOrdering = ABPersonGetSortOrdering() == kABPersonCompositeNameFormatFirstNameFirst ? YES : NO; return [Contact comparatorSortingNamesByFirstThenLast:firstNameOrdering]; } - (NSArray * _Nonnull)signalContacts { return [self getSignalUsersFromContactsArray:[self allContacts]]; } - (NSString *)unknownContactName { return NSLocalizedString(@"UNKNOWN_CONTACT_NAME", @"Displayed if for some reason we can't determine a contacts phone number *or* name"); } - (NSString * _Nonnull)displayNameForPhoneIdentifier:(NSString * _Nullable)identifier { if (!identifier) { return self.unknownContactName; } // TODO: There's some overlap here with displayNameForSignalAccount. SignalAccount *signalAccount = [self signalAccountForRecipientId:identifier]; NSString *displayName = (signalAccount.contact.fullName.length > 0) ? signalAccount.contact.fullName : identifier; return displayName; } // TODO move into Contact class. - (NSString *_Nonnull)displayNameForContact:(Contact *)contact { OWSAssert(contact); NSString *displayName = (contact.fullName.length > 0) ? contact.fullName : self.unknownContactName; return displayName; } - (NSString *_Nonnull)displayNameForSignalAccount:(SignalAccount *)signalAccount { OWSAssert(signalAccount); NSString *baseName = (signalAccount.contact ? [self displayNameForContact:signalAccount.contact] : [self displayNameForPhoneIdentifier:signalAccount.recipientId]); OWSAssert(signalAccount.hasMultipleAccountContact == (signalAccount.multipleAccountLabelText != nil)); if (signalAccount.multipleAccountLabelText) { return [NSString stringWithFormat:@"%@ (%@)", baseName, signalAccount.multipleAccountLabelText]; } else { return baseName; } } - (NSAttributedString *_Nonnull)formattedDisplayNameForSignalAccount:(SignalAccount *)signalAccount font:(UIFont *_Nonnull)font { OWSAssert(signalAccount); OWSAssert(font); NSAttributedString *baseName = [self formattedFullNameForContact:signalAccount.contact font:font]; OWSAssert(signalAccount.hasMultipleAccountContact == (signalAccount.multipleAccountLabelText != nil)); if (signalAccount.multipleAccountLabelText) { NSMutableAttributedString *result = [NSMutableAttributedString new]; [result appendAttributedString:baseName]; [result appendAttributedString:[[NSAttributedString alloc] initWithString:@" (" attributes:@{ NSFontAttributeName : font, }]]; [result appendAttributedString:[[NSAttributedString alloc] initWithString:signalAccount.multipleAccountLabelText]]; [result appendAttributedString:[[NSAttributedString alloc] initWithString:@")" attributes:@{ NSFontAttributeName : font, }]]; return result; } else { return baseName; } } // TODO move into Contact class. - (NSAttributedString *_Nonnull)formattedFullNameForContact:(Contact *)contact font:(UIFont *_Nonnull)font { UIFont *boldFont = [UIFont ows_mediumFontWithSize:font.pointSize]; NSDictionary *boldFontAttributes = @{ NSFontAttributeName : boldFont, NSForegroundColorAttributeName : [UIColor blackColor] }; NSDictionary *normalFontAttributes = @{ NSFontAttributeName : font, NSForegroundColorAttributeName : [UIColor ows_darkGrayColor] }; NSAttributedString *_Nullable firstName, *_Nullable lastName; if (ABPersonGetSortOrdering() == kABPersonSortByFirstName) { if (contact.firstName) { firstName = [[NSAttributedString alloc] initWithString:contact.firstName attributes:boldFontAttributes]; } if (contact.lastName) { lastName = [[NSAttributedString alloc] initWithString:contact.lastName attributes:normalFontAttributes]; } } else { if (contact.firstName) { firstName = [[NSAttributedString alloc] initWithString:contact.firstName attributes:normalFontAttributes]; } if (contact.lastName) { lastName = [[NSAttributedString alloc] initWithString:contact.lastName attributes:boldFontAttributes]; } } NSAttributedString *_Nullable leftName, *_Nullable rightName; if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst) { leftName = firstName; rightName = lastName; } else { leftName = lastName; rightName = firstName; } NSMutableAttributedString *fullNameString = [NSMutableAttributedString new]; if (leftName) { [fullNameString appendAttributedString:leftName]; } if (leftName && rightName) { [fullNameString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; } if (rightName) { [fullNameString appendAttributedString:rightName]; } return fullNameString; } - (NSAttributedString *)formattedFullNameForRecipientId:(NSString *)recipientId font:(UIFont *)font { NSDictionary *normalFontAttributes = @{ NSFontAttributeName : font, NSForegroundColorAttributeName : [UIColor ows_darkGrayColor] }; return [[NSAttributedString alloc] initWithString:[PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:recipientId] attributes:normalFontAttributes]; } - (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId { OWSAssert(recipientId.length > 0); return self.signalAccountMap[recipientId]; } - (Contact *)getOrBuildContactForPhoneIdentifier:(NSString *)identifier { Contact *savedContact = self.allContactsMap[identifier]; if (savedContact) { return savedContact; } else { return [[Contact alloc] initWithContactWithFirstName:self.unknownContactName andLastName:nil andUserTextPhoneNumbers:@[ identifier ] phoneNumberTypeMap:nil andImage:nil andContactID:0]; } } - (UIImage * _Nullable)imageForPhoneIdentifier:(NSString * _Nullable)identifier { Contact *contact = self.allContactsMap[identifier]; return contact.image; } #pragma mark - Logging + (NSString *)tag { return [NSString stringWithFormat:@"[%@]", self.class]; } - (NSString *)tag { return self.class.tag; } @end