From eadb04efccca8701bb0862c4d7cd498c65bdcd58 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sun, 9 Sep 2018 12:11:41 -0500 Subject: [PATCH] WIP: ContactViewHelper incorporates group blocking -[ ] UI -[ ] Conversation Settings -[x] Show switch for group -[ ] localize -[ ] migrate existing localizations? (nice to have) -[ ] can view conversation settings (but not edit them) in left group -[ ] special block copy for groups -[ ] special unblock copy for groups -[ ] ConversationViewHelper -[x] Track blocked groups -[ ] HomeView -[ ] ConversationView -[ ] Any others? -[ ] Rename? Extract BlockList cache? -[ ] Block List -[ ] Group Section -[ ] Unblock group -[ ] Interstitial interacting with blocked threads (e.g. thread picker) -[ ] BlockListUIUtils w/ thread -[x] Block -[x] Unblock -[ ] Replace usages where possible -[x] block manager -[ ] Sync -[x] tentative protos -[ ] confirm protos w/ team -[x] send new protos -[ ] Message Processing -[ ] Drop messages from blocked groups --- .../ConversationViewController.m | 7 ++-- .../ConversationSearchViewController.swift | 1 + .../ViewControllers/HomeView/HomeViewCell.m | 1 + .../HomeView/HomeViewController.m | 8 +++-- .../SelectRecipientViewController.m | 1 - .../SelectThreadViewController.m | 10 +++--- SignalMessaging/Views/ContactsViewHelper.h | 3 ++ SignalMessaging/Views/ContactsViewHelper.m | 33 ++++++++++++++++--- SignalMessaging/utils/ThreadUtil.m | 7 ++++ .../src/Messages/OWSBlockingManager.h | 2 +- .../src/Messages/OWSBlockingManager.m | 4 +-- 11 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 3378f63ba..b40a9b8ee 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -293,8 +293,8 @@ typedef enum : NSUInteger { - (void)addNotificationListeners { [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(blockedPhoneNumbersDidChange:) - name:kNSNotificationName_BlockedPhoneNumbersDidChange + selector:@selector(blockListDidChange:) + name:kNSNotificationName_BlockListDidChange object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowManagerCallDidChange:) @@ -410,7 +410,7 @@ typedef enum : NSUInteger { } } -- (void)blockedPhoneNumbersDidChange:(id)notification +- (void)blockListDidChange:(id)notification { OWSAssertIsOnMainThread(); @@ -865,6 +865,7 @@ typedef enum : NSUInteger { } NSString *blockStateMessage = nil; + // FIXME - group blocking if ([self isBlockedContactConversation]) { blockStateMessage = NSLocalizedString( @"MESSAGES_VIEW_CONTACT_BLOCKED", @"Indicates that this 1:1 conversation has been blocked."); diff --git a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift index 05eb6840e..cac221626 100644 --- a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift +++ b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift @@ -55,6 +55,7 @@ class ConversationSearchViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() + // MJK TODO let blockingManager = OWSBlockingManager.shared() blockedPhoneNumberSet = Set(blockingManager.blockedPhoneNumbers()) diff --git a/Signal/src/ViewControllers/HomeView/HomeViewCell.m b/Signal/src/ViewControllers/HomeView/HomeViewCell.m index 7b460a899..eed7c27ee 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewCell.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewCell.m @@ -347,6 +347,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(thread); BOOL isBlocked = NO; + // FIXME if (!thread.isGroupThread) { NSString *contactIdentifier = thread.contactIdentifier; isBlocked = [blockedPhoneNumberSet containsObject:contactIdentifier]; diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index b78a7fdb0..77c0a43c9 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -159,8 +159,8 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations #pragma GCC diagnostic pop [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(blockedPhoneNumbersDidChange:) - name:kNSNotificationName_BlockedPhoneNumbersDidChange + selector:@selector(blockListDidChange:) + name:kNSNotificationName_BlockListDidChange object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signalAccountsDidChange:) @@ -207,12 +207,14 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations #pragma mark - Notifications -- (void)blockedPhoneNumbersDidChange:(id)notification +- (void)blockListDidChange:(id)notification { OWSAssertIsOnMainThread(); _blockedPhoneNumberSet = [NSSet setWithArray:[_blockingManager blockedPhoneNumbers]]; + // FIXME rather than tracking blockedPhoneNumberSet, use ContactViewHelper? + [self reloadTableViewData]; } diff --git a/SignalMessaging/ViewControllers/SelectRecipientViewController.m b/SignalMessaging/ViewControllers/SelectRecipientViewController.m index f0cfe4a45..6b60a7d89 100644 --- a/SignalMessaging/ViewControllers/SelectRecipientViewController.m +++ b/SignalMessaging/ViewControllers/SelectRecipientViewController.m @@ -17,7 +17,6 @@ #import #import #import -#import #import #import #import diff --git a/SignalMessaging/ViewControllers/SelectThreadViewController.m b/SignalMessaging/ViewControllers/SelectThreadViewController.m index f89ee721c..8900f282e 100644 --- a/SignalMessaging/ViewControllers/SelectThreadViewController.m +++ b/SignalMessaging/ViewControllers/SelectThreadViewController.m @@ -197,12 +197,10 @@ NS_ASSUME_NONNULL_BEGIN // instead of HomeViewCell to present contacts and threads. ContactTableViewCell *cell = [ContactTableViewCell new]; - if ([thread isKindOfClass:[TSContactThread class]]) { - BOOL isBlocked = [helper isRecipientIdBlocked:thread.contactIdentifier]; - if (isBlocked) { - cell.accessoryMessage = NSLocalizedString( - @"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked."); - } + BOOL isBlocked = [helper isThreadBlocked:thread]; + if (isBlocked) { + cell.accessoryMessage = NSLocalizedString( + @"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked."); } [cell configureWithThread:thread contactsManager:helper.contactsManager]; diff --git a/SignalMessaging/Views/ContactsViewHelper.h b/SignalMessaging/Views/ContactsViewHelper.h index c12792ebc..29306575e 100644 --- a/SignalMessaging/Views/ContactsViewHelper.h +++ b/SignalMessaging/Views/ContactsViewHelper.h @@ -46,6 +46,7 @@ NS_ASSUME_NONNULL_BEGIN // Useful to differentiate between having no signal accounts vs. haven't checked yet @property (nonatomic, readonly) BOOL hasUpdatedContactsAtLeastOnce; +// MJK TODO Can we remove? Do we need blockedGroupList? @property (nonatomic, readonly) NSArray *blockedPhoneNumbers; // Suitable when the user tries to perform an action which is not possible due to the user having @@ -64,6 +65,8 @@ NS_ASSUME_NONNULL_BEGIN // is only safe to be called on the main thread. - (BOOL)isRecipientIdBlocked:(NSString *)recipientId; +// This method is faster than OWSBlockingManager but +// is only safe to be called on the main thread. - (BOOL)isThreadBlocked:(TSThread *)thread; // NOTE: This method uses a transaction. diff --git a/SignalMessaging/Views/ContactsViewHelper.m b/SignalMessaging/Views/ContactsViewHelper.m index 50fb4cf65..19a1e8914 100644 --- a/SignalMessaging/Views/ContactsViewHelper.m +++ b/SignalMessaging/Views/ContactsViewHelper.m @@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) NSArray *signalAccounts; @property (nonatomic) NSArray *blockedPhoneNumbers; +@property (nonatomic) NSArray *blockedGroupIds; @property (nonatomic) BOOL shouldNotifyDelegateOfUpdatedContacts; @property (nonatomic) BOOL hasUpdatedContactsAtLeastOnce; @@ -52,6 +53,8 @@ NS_ASSUME_NONNULL_BEGIN _blockingManager = [OWSBlockingManager sharedManager]; _blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers]; + _blockedGroupIds = [_blockingManager blockedGroupIds]; + _conversationSearcher = ConversationSearcher.shared; _contactsManager = [Environment current].contactsManager; @@ -74,8 +77,8 @@ NS_ASSUME_NONNULL_BEGIN name:OWSContactsManagerSignalAccountsDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(blockedPhoneNumbersDidChange:) - name:kNSNotificationName_BlockedPhoneNumbersDidChange + selector:@selector(blockListDidChange:) + name:kNSNotificationName_BlockListDidChange object:nil]; } @@ -91,11 +94,12 @@ NS_ASSUME_NONNULL_BEGIN [self updateContacts]; } -- (void)blockedPhoneNumbersDidChange:(id)notification +- (void)blockListDidChange:(NSNotification *)notification { OWSAssertIsOnMainThread(); - self.blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers]; + self.blockedPhoneNumbers = self.blockingManager.blockedPhoneNumbers; + self.blockedGroupIds = self.blockingManager.blockedGroupIds; [self updateContacts]; } @@ -161,6 +165,27 @@ NS_ASSUME_NONNULL_BEGIN return [_blockedPhoneNumbers containsObject:recipientId]; } +- (BOOL)isGroupIdBlocked:(NSData *)groupId +{ + OWSAssertIsOnMainThread(); + + return [self.blockedGroupIds containsObject:groupId]; +} + +- (BOOL)isThreadBlocked:(TSThread *)thread +{ + if ([thread isKindOfClass:[TSContactThread class]]) { + TSContactThread *contactThread = (TSContactThread *)thread; + return [self isRecipientIdBlocked:contactThread.contactIdentifier]; + } else if ([thread isKindOfClass:[TSGroupThread class]]) { + TSGroupThread *groupThread = (TSGroupThread *)thread; + return [self isGroupIdBlocked:groupThread.groupModel.groupId]; + } else { + OWSFail(@"%@ failure: unexpected thread: %@", self.logTag, thread.class); + return NO; + } +} + - (void)updateContacts { OWSAssertIsOnMainThread(); diff --git a/SignalMessaging/utils/ThreadUtil.m b/SignalMessaging/utils/ThreadUtil.m index d256c7a7b..30fc8b826 100644 --- a/SignalMessaging/utils/ThreadUtil.m +++ b/SignalMessaging/utils/ThreadUtil.m @@ -347,6 +347,9 @@ NS_ASSUME_NONNULL_BEGIN shouldHaveAddToContactsOffer = NO; // Only create block offers in 1:1 conversations. shouldHaveBlockOffer = NO; + + // MJK TODO - any conditions under which we'd make a block offer for groups? + // Only create profile whitelist offers in 1:1 conversations. shouldHaveAddToProfileWhitelistOffer = NO; } else { @@ -665,6 +668,10 @@ NS_ASSUME_NONNULL_BEGIN if (![OWSProfileManager.sharedManager hasLocalProfile]) { return NO; } + if ([blockingManager isThreadBlocked:thread]) { + return NO; + } + BOOL hasUnwhitelistedMember = NO; NSArray *blockedPhoneNumbers = [blockingManager blockedPhoneNumbers]; for (NSString *recipientId in thread.recipientIdentifiers) { diff --git a/SignalServiceKit/src/Messages/OWSBlockingManager.h b/SignalServiceKit/src/Messages/OWSBlockingManager.h index 57d7711d7..2c2892580 100644 --- a/SignalServiceKit/src/Messages/OWSBlockingManager.h +++ b/SignalServiceKit/src/Messages/OWSBlockingManager.h @@ -6,7 +6,7 @@ NS_ASSUME_NONNULL_BEGIN @class TSThread; -extern NSString *const kNSNotificationName_BlockedPhoneNumbersDidChange; +extern NSString *const kNSNotificationName_BlockListDidChange; // This class can be safely accessed and used from any thread. @interface OWSBlockingManager : NSObject diff --git a/SignalServiceKit/src/Messages/OWSBlockingManager.m b/SignalServiceKit/src/Messages/OWSBlockingManager.m index 24419fdf4..8ab7aa4c4 100644 --- a/SignalServiceKit/src/Messages/OWSBlockingManager.m +++ b/SignalServiceKit/src/Messages/OWSBlockingManager.m @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN -NSString *const kNSNotificationName_BlockedPhoneNumbersDidChange = @"kNSNotificationName_BlockedPhoneNumbersDidChange"; +NSString *const kNSNotificationName_BlockListDidChange = @"kNSNotificationName_BlockListDidChange"; NSString *const kOWSBlockingManager_BlockListCollection = @"kOWSBlockingManager_BlockedPhoneNumbersCollection"; @@ -298,7 +298,7 @@ NSString *const kOWSBlockingManager_SyncedBlockedGroupIdsKey = @"kOWSBlockingMan [self saveSyncedBlockListWithPhoneNumbers:blockedPhoneNumbers groupIds:blockedGroupIds]; } - [[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_BlockedPhoneNumbersDidChange + [[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_BlockListDidChange object:nil userInfo:nil]; });