Improve nullability handling for SignalAccount.

This commit is contained in:
Matthew Chen 2018-07-27 14:19:11 -04:00
parent 20c82e6adb
commit 579e88bdc2
18 changed files with 64 additions and 99 deletions

View File

@ -55,7 +55,7 @@ class ConversationConfigurationSyncOperation: OWSOperation {
}
private func sync(contactThread: TSContactThread) {
guard let signalAccount: SignalAccount = self.contactsManager.signalAccount(forRecipientId: contactThread.contactIdentifier()) else {
guard let signalAccount: SignalAccount = self.contactsManager.fetchSignalAccount(forRecipientId: contactThread.contactIdentifier()) else {
reportAssertionError(description: "unable to find signalAccount")
return
}

View File

@ -83,16 +83,8 @@ NS_ASSUME_NONNULL_BEGIN
[blocklistSection addItem:[OWSTableItem
itemWithCustomCellBlock:^{
ContactTableViewCell *cell = [ContactTableViewCell new];
SignalAccount *signalAccount =
[helper signalAccountForRecipientId:phoneNumber];
if (signalAccount) {
[cell configureWithSignalAccount:signalAccount
contactsManager:helper.contactsManager];
} else {
[cell configureWithRecipientId:phoneNumber
contactsManager:helper.contactsManager];
}
[cell configureWithRecipientId:phoneNumber
contactsManager:helper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension

View File

@ -226,7 +226,7 @@ class ConversationSearchViewController: UITableViewController {
owsFail("searchResult was unexpectedly nil")
return UITableViewCell()
}
cell.configure(with: searchResult.signalAccount, contactsManager: contactsManager)
cell.configure(withRecipientId: searchResult.signalAccount.recipientId, contactsManager: contactsManager)
return cell
case .messages:
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomeViewCell.cellReuseIdentifier()) as? HomeViewCell else {

View File

@ -462,8 +462,8 @@ NS_ASSUME_NONNULL_BEGIN
@"An indicator that a contact has been blocked.");
}
[cell configureWithSignalAccount:signalAccount
contactsManager:self.contactsViewHelper.contactsManager];
[cell configureWithRecipientId:signalAccount.recipientId
contactsManager:self.contactsViewHelper.contactsManager];
return cell;
}
@ -510,15 +510,7 @@ NS_ASSUME_NONNULL_BEGIN
cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
SignalAccount *signalAccount = [helper signalAccountForRecipientId:phoneNumber];
if (signalAccount) {
[cell configureWithSignalAccount:signalAccount
contactsManager:helper.contactsManager];
} else {
[cell configureWithRecipientId:phoneNumber contactsManager:helper.contactsManager];
}
[cell configureWithRecipientId:phoneNumber contactsManager:helper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension
@ -566,7 +558,8 @@ NS_ASSUME_NONNULL_BEGIN
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
[cell configureWithSignalAccount:signalAccount contactsManager:helper.contactsManager];
[cell configureWithRecipientId:signalAccount.recipientId
contactsManager:helper.contactsManager];
return cell;
}

View File

@ -226,8 +226,6 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
OWSCAssert(strongSelf);
ContactTableViewCell *cell = [ContactTableViewCell new];
SignalAccount *signalAccount =
[contactsViewHelper signalAccountForRecipientId:recipientId];
BOOL isCurrentMember = [strongSelf.memberRecipientIds containsObject:recipientId];
BOOL isBlocked = [contactsViewHelper isRecipientIdBlocked:recipientId];
if (isCurrentMember) {
@ -239,15 +237,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
if (signalAccount) {
[cell configureWithSignalAccount:signalAccount
contactsManager:contactsViewHelper.contactsManager];
} else {
[cell configureWithRecipientId:recipientId
contactsManager:contactsViewHelper.contactsManager];
}
[cell configureWithRecipientId:recipientId
contactsManager:contactsViewHelper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension
@ -335,9 +326,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
[cell configureWithSignalAccount:signalAccount
contactsManager:contactsViewHelper.contactsManager];
[cell configureWithRecipientId:signalAccount.recipientId
contactsManager:contactsViewHelper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension

View File

@ -162,12 +162,12 @@ NS_ASSUME_NONNULL_BEGIN
__weak ShowGroupMembersViewController *weakSelf = self;
ContactsViewHelper *helper = self.contactsViewHelper;
// Sort the group members using contacts manager.
NSArray<NSString *> *sortedRecipientIds =
[recipientIds sortedArrayUsingComparator:^NSComparisonResult(NSString *recipientIdA, NSString *recipientIdB) {
SignalAccount *signalAccountA = [helper.contactsManager signalAccountForRecipientId:recipientIdA];
SignalAccount *signalAccountB = [helper.contactsManager signalAccountForRecipientId:recipientIdB];
return [helper.contactsManager compareSignalAccount:signalAccountA withSignalAccount:signalAccountB];
}];
NSArray<NSString *> *sortedRecipientIds = [recipientIds sortedArrayUsingComparator:^NSComparisonResult(
NSString *recipientIdA, NSString *recipientIdB) {
SignalAccount *signalAccountA = [helper.contactsManager fetchOrBuildSignalAccountForRecipientId:recipientIdA];
SignalAccount *signalAccountB = [helper.contactsManager fetchOrBuildSignalAccountForRecipientId:recipientIdB];
return [helper.contactsManager compareSignalAccount:signalAccountA withSignalAccount:signalAccountB];
}];
for (NSString *recipientId in sortedRecipientIds) {
[section addItem:[OWSTableItem
itemWithCustomCellBlock:^{
@ -175,7 +175,6 @@ NS_ASSUME_NONNULL_BEGIN
OWSCAssert(strongSelf);
ContactTableViewCell *cell = [ContactTableViewCell new];
SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId];
OWSVerificationState verificationState =
[[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId];
BOOL isVerified = verificationState == OWSVerificationStateVerified;
@ -189,12 +188,7 @@ NS_ASSUME_NONNULL_BEGIN
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
if (signalAccount) {
[cell configureWithSignalAccount:signalAccount
contactsManager:helper.contactsManager];
} else {
[cell configureWithRecipientId:recipientId contactsManager:helper.contactsManager];
}
[cell configureWithRecipientId:recipientId contactsManager:helper.contactsManager];
if (isVerified) {
[cell setAttributedSubtitle:cell.verifiedSubtitle];
@ -279,7 +273,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(recipientId.length > 0);
ContactsViewHelper *helper = self.contactsViewHelper;
SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount = [helper fetchSignalAccountForRecipientId:recipientId];
UIAlertController *actionSheetController =
[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

View File

@ -266,7 +266,6 @@ NS_ASSUME_NONNULL_BEGIN
OWSCAssert(strongSelf);
ContactTableViewCell *cell = [ContactTableViewCell new];
SignalAccount *signalAccount = [contactsViewHelper signalAccountForRecipientId:recipientId];
BOOL isPreviousMember = [strongSelf.previousMemberRecipientIds containsObject:recipientId];
BOOL isBlocked = [contactsViewHelper isRecipientIdBlocked:recipientId];
if (isPreviousMember) {
@ -286,19 +285,14 @@ NS_ASSUME_NONNULL_BEGIN
@"An indicator that a user is a new member of the group.");
}
if (signalAccount) {
[cell configureWithSignalAccount:signalAccount
contactsManager:contactsViewHelper.contactsManager];
} else {
[cell configureWithRecipientId:recipientId
contactsManager:contactsViewHelper.contactsManager];
}
[cell configureWithRecipientId:recipientId
contactsManager:contactsViewHelper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension
actionBlock:^{
SignalAccount *signalAccount = [contactsViewHelper signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount =
[contactsViewHelper fetchSignalAccountForRecipientId:recipientId];
BOOL isPreviousMember = [weakSelf.previousMemberRecipientIds containsObject:recipientId];
BOOL isBlocked = [contactsViewHelper isRecipientIdBlocked:recipientId];
if (isPreviousMember) {

View File

@ -682,9 +682,9 @@ private class SignalCallData: NSObject {
// For contacts not stored in our system contacts, we assume they are an unknown caller, and we force
// a TURN connection, so as not to reveal any connectivity information (IP/port) to the caller.
let unknownCaller = self.contactsManager.signalAccount(forRecipientId: thread.contactIdentifier()) == nil
let isUnknownCaller = !self.contactsManager.hasSignalAccount(forRecipientId: thread.contactIdentifier())
let useTurnOnly = unknownCaller || Environment.current().preferences.doCallsHideIPAddress()
let useTurnOnly = isUnknownCaller || Environment.current().preferences.doCallsHideIPAddress()
Logger.debug("\(self.logTag) setting peerConnectionClient in \(#function) for: \(newCall.identifiersForLogs)")
let peerConnectionClient = PeerConnectionClient(iceServers: iceServers, delegate: self, callDirection: .incoming, useTurnOnly: useTurnOnly)

View File

@ -8,15 +8,12 @@ extern const NSUInteger kContactCellAvatarSize;
extern const CGFloat kContactCellAvatarTextMargin;
@class OWSContactsManager;
@class SignalAccount;
@class TSThread;
@interface ContactCellView : UIStackView
@property (nonatomic, nullable) NSString *accessoryMessage;
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager;
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager;
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager;

View File

@ -103,11 +103,6 @@ const CGFloat kContactCellAvatarTextMargin = 12;
self.accessoryLabel.textColor = [UIColor colorWithWhite:0.5f alpha:1.f];
}
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager
{
[self configureWithRecipientId:signalAccount.recipientId contactsManager:contactsManager];
}
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager
{
OWSAssert(recipientId.length > 0);

View File

@ -7,15 +7,12 @@
NS_ASSUME_NONNULL_BEGIN
@class OWSContactsManager;
@class SignalAccount;
@class TSThread;
@interface ContactTableViewCell : UITableViewCell
+ (NSString *)reuseIdentifier;
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager;
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager;
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager;

View File

@ -53,11 +53,6 @@ NS_ASSUME_NONNULL_BEGIN
self.cellView.userInteractionEnabled = NO;
}
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager
{
[self configureWithRecipientId:signalAccount.recipientId contactsManager:contactsManager];
}
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager
{
[OWSTableItem configureCell:self];

View File

@ -55,7 +55,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithDelegate:(id<ContactsViewHelperDelegate>)delegate;
- (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId;
- (nullable SignalAccount *)fetchSignalAccountForRecipientId:(NSString *)recipientId;
- (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId;
// This method is faster than OWSBlockingManager but
// is only safe to be called on the main thread.

View File

@ -102,7 +102,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Contacts
- (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId
- (nullable SignalAccount *)fetchSignalAccountForRecipientId:(NSString *)recipientId
{
OWSAssertIsOnMainThread();
OWSAssert(recipientId.length > 0);
@ -110,6 +110,14 @@ NS_ASSUME_NONNULL_BEGIN
return self.signalAccountMap[recipientId];
}
- (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId
{
OWSAssert(recipientId.length > 0);
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
return (signalAccount ?: [[SignalAccount alloc] initWithRecipientId:recipientId]);
}
- (BOOL)isSignalAccountHidden:(SignalAccount *)signalAccount
{
OWSAssertIsOnMainThread();
@ -310,7 +318,7 @@ NS_ASSUME_NONNULL_BEGIN
editImmediately:(BOOL)shouldEditImmediately
addToExistingCnContact:(CNContact *_Nullable)existingContact
{
SignalAccount *signalAccount = [self signalAccountForRecipientId:recipientId];
SignalAccount *signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
if (!self.contactsManager.supportsContactEditing) {
// Should not expose UI that lets the user get here.

View File

@ -32,7 +32,11 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification;
// order of the signalAccounts array respects the systems contact sorting preference
@property (atomic, readonly) NSArray<SignalAccount *> *signalAccounts;
- (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId;
// This will return an instance of SignalAccount for _known_ signal accounts.
- (nullable SignalAccount *)fetchSignalAccountForRecipientId:(NSString *)recipientId;
// This will always return an instance of SignalAccount.
- (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId;
- (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId;
- (void)loadSignalAccountsFromCache;

View File

@ -588,7 +588,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
{
OWSAssert(recipientId.length > 0);
SignalAccount *_Nullable signalAccount = [self signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
if (!signalAccount) {
// search system contacts for no-longer-registered signal users, for which there will be no SignalAccount
DDLogDebug(@"%@ no signal account", self.logTag);
@ -616,7 +616,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
{
OWSAssert(recipientId.length > 0);
SignalAccount *_Nullable signalAccount = [self signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
return signalAccount.contact.firstName.filterStringForDisplay;
}
@ -624,7 +624,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
{
OWSAssert(recipientId.length > 0);
SignalAccount *_Nullable signalAccount = [self signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
return signalAccount.contact.lastName.filterStringForDisplay;
}
@ -821,7 +821,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
}
// Append unique label for contacts with multiple Signal accounts
SignalAccount *signalAccount = [self signalAccountForRecipientId:recipientId];
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
if (signalAccount && signalAccount.multipleAccountLabelText) {
OWSAssert(signalAccount.multipleAccountLabelText.length > 0);
@ -938,7 +938,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
return formattedPhoneNumber;
}
- (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId
- (nullable SignalAccount *)fetchSignalAccountForRecipientId:(NSString *)recipientId
{
OWSAssert(recipientId.length > 0);
@ -955,11 +955,18 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
return signalAccount;
}
- (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId
- (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId
{
return [self signalAccountForRecipientId:recipientId] != nil;
OWSAssert(recipientId.length > 0);
SignalAccount *_Nullable signalAccount = [self fetchSignalAccountForRecipientId:recipientId];
return (signalAccount ?: [[SignalAccount alloc] initWithRecipientId:recipientId]);
}
- (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId
{
return [self fetchSignalAccountForRecipientId:recipientId] != nil;
}
- (UIImage *_Nullable)systemContactImageForPhoneIdentifier:(NSString *_Nullable)identifier
{
@ -971,7 +978,7 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
if (!contact) {
// If we haven't loaded system contacts yet, we may have a cached
// copy in the db
contact = [self signalAccountForRecipientId:identifier].contact;
contact = [self fetchSignalAccountForRecipientId:identifier].contact;
}
return [self avatarImageForCNContactId:contact.cnContactId];

View File

@ -550,8 +550,8 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien
cell.accessoryMessage =
[weakSelf.delegate accessoryMessageForSignalAccount:signalAccount];
}
[cell configureWithSignalAccount:signalAccount
contactsManager:helper.contactsManager];
[cell configureWithRecipientId:signalAccount.recipientId
contactsManager:helper.contactsManager];
if (![weakSelf.delegate canSignalAccountBeSelected:signalAccount]) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

View File

@ -283,7 +283,8 @@ NS_ASSUME_NONNULL_BEGIN
cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
[cell configureWithSignalAccount:signalAccount contactsManager:helper.contactsManager];
[cell configureWithRecipientId:signalAccount.recipientId
contactsManager:helper.contactsManager];
return cell;
}
customRowHeight:UITableViewAutomaticDimension
@ -411,10 +412,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)recipientIdWasSelected:(NSString *)recipientId
{
SignalAccount *_Nullable signalAccount = [self.contactsViewHelper signalAccountForRecipientId:recipientId];
if (!signalAccount) {
signalAccount = [[SignalAccount alloc] initWithRecipientId:recipientId];
}
SignalAccount *signalAccount = [self.contactsViewHelper fetchOrBuildSignalAccountForRecipientId:recipientId];
[self signalAccountWasSelected:signalAccount];
}