session-ios/Signal/src/ViewControllers/ThreadSettings/ShowGroupMembersViewControl...

466 lines
21 KiB
Mathematica
Raw Normal View History

//
2018-01-30 21:05:04 +01:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "ShowGroupMembersViewController.h"
2017-05-05 18:39:21 +02:00
#import "Signal-Swift.h"
#import "SignalApp.h"
#import "ViewControllerUtils.h"
2017-12-08 17:50:35 +01:00
#import <SignalMessaging/BlockListUIUtils.h>
#import <SignalMessaging/ContactTableViewCell.h>
#import <SignalMessaging/ContactsViewHelper.h>
2017-12-19 03:50:51 +01:00
#import <SignalMessaging/Environment.h>
2017-12-08 17:50:35 +01:00
#import <SignalMessaging/OWSContactsManager.h>
#import <SignalMessaging/UIUtil.h>
2017-04-18 22:08:01 +02:00
#import <SignalServiceKit/OWSBlockingManager.h>
#import <SignalServiceKit/SignalAccount.h>
#import <SignalServiceKit/TSGroupModel.h>
#import <SignalServiceKit/TSGroupThread.h>
@import ContactsUI;
NS_ASSUME_NONNULL_BEGIN
2017-05-09 23:55:18 +02:00
@interface ShowGroupMembersViewController () <ContactsViewHelperDelegate, ContactEditingDelegate>
@property (nonatomic, readonly) TSGroupThread *thread;
@property (nonatomic, readonly) ContactsViewHelper *contactsViewHelper;
@property (nonatomic, nullable) NSSet<NSString *> *memberRecipientIds;
2017-04-18 22:08:01 +02:00
@end
#pragma mark -
@implementation ShowGroupMembersViewController
- (instancetype)init
{
self = [super init];
if (!self) {
return self;
}
2017-04-18 22:08:01 +02:00
[self commonInit];
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) {
return self;
}
2017-04-18 22:08:01 +02:00
[self commonInit];
return self;
}
2017-04-18 22:08:01 +02:00
- (void)commonInit
{
_contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self];
2018-06-15 17:15:21 +02:00
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 60;
[self observeNotifications];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)observeNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(identityStateDidChange:)
name:kNSNotificationName_IdentityStateDidChange
object:nil];
2017-04-18 22:08:01 +02:00
}
- (void)configWithThread:(TSGroupThread *)thread
2017-04-18 22:08:01 +02:00
{
_thread = thread;
2017-04-18 22:08:01 +02:00
OWSAssertDebug(self.thread);
OWSAssertDebug(self.thread.groupModel);
OWSAssertDebug(self.thread.groupModel.groupMemberIds);
2017-04-18 22:08:01 +02:00
self.memberRecipientIds = [NSSet setWithArray:self.thread.groupModel.groupMemberIds];
}
- (void)viewDidLoad
{
[super viewDidLoad];
OWSAssertDebug([self.navigationController isKindOfClass:[OWSNavigationController class]]);
self.title = _thread.groupModel.groupName;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 45;
[self updateTableContents];
}
#pragma mark - Table Contents
- (void)updateTableContents
2017-04-18 22:08:01 +02:00
{
OWSAssertDebug(self.thread);
OWSTableContents *contents = [OWSTableContents new];
2017-04-18 22:08:01 +02:00
__weak ShowGroupMembersViewController *weakSelf = self;
ContactsViewHelper *helper = self.contactsViewHelper;
OWSTableSection *membersSection = [OWSTableSection new];
// Group Members
// If there are "no longer verified" members of the group,
// highlight them in a special section.
NSArray<NSString *> *noLongerVerifiedRecipientIds = [self noLongerVerifiedRecipientIds];
if (noLongerVerifiedRecipientIds.count > 0) {
OWSTableSection *noLongerVerifiedSection = [OWSTableSection new];
noLongerVerifiedSection.headerTitle = NSLocalizedString(@"GROUP_MEMBERS_SECTION_TITLE_NO_LONGER_VERIFIED",
@"Title for the 'no longer verified' section of the 'group members' view.");
membersSection.headerTitle = NSLocalizedString(
@"GROUP_MEMBERS_SECTION_TITLE_MEMBERS", @"Title for the 'members' section of the 'group members' view.");
[noLongerVerifiedSection
addItem:[OWSTableItem disclosureItemWithText:NSLocalizedString(@"GROUP_MEMBERS_RESET_NO_LONGER_VERIFIED",
@"Label for the button that clears all verification "
@"errors in the 'group members' view.")
2018-06-13 22:22:03 +02:00
customRowHeight:UITableViewAutomaticDimension
actionBlock:^{
[weakSelf offerResetAllNoLongerVerified];
}]];
[self addMembers:noLongerVerifiedRecipientIds toSection:noLongerVerifiedSection useVerifyAction:YES];
[contents addSection:noLongerVerifiedSection];
}
NSMutableSet *memberRecipientIds = [self.memberRecipientIds mutableCopy];
[memberRecipientIds removeObject:[helper localNumber]];
[self addMembers:memberRecipientIds.allObjects toSection:membersSection useVerifyAction:NO];
[contents addSection:membersSection];
self.contents = contents;
}
- (void)addMembers:(NSArray<NSString *> *)recipientIds
toSection:(OWSTableSection *)section
useVerifyAction:(BOOL)useVerifyAction
{
OWSAssertDebug(recipientIds);
OWSAssertDebug(section);
__weak ShowGroupMembersViewController *weakSelf = self;
ContactsViewHelper *helper = self.contactsViewHelper;
2018-05-16 22:12:13 +02:00
// Sort the group members using contacts manager.
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];
}];
2018-05-16 22:12:13 +02:00
for (NSString *recipientId in sortedRecipientIds) {
2018-06-13 22:22:03 +02:00
[section addItem:[OWSTableItem
itemWithCustomCellBlock:^{
ShowGroupMembersViewController *strongSelf = weakSelf;
OWSCAssertDebug(strongSelf);
2018-06-13 22:22:03 +02:00
ContactTableViewCell *cell = [ContactTableViewCell new];
OWSVerificationState verificationState =
[[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId];
BOOL isVerified = verificationState == OWSVerificationStateVerified;
BOOL isNoLongerVerified = verificationState == OWSVerificationStateNoLongerVerified;
BOOL isBlocked = [helper isRecipientIdBlocked:recipientId];
if (isNoLongerVerified) {
cell.accessoryMessage = NSLocalizedString(@"CONTACT_CELL_IS_NO_LONGER_VERIFIED",
@"An indicator that a contact is no longer verified.");
} else if (isBlocked) {
cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
}
[cell configureWithRecipientId:recipientId contactsManager:helper.contactsManager];
2018-06-13 22:22:03 +02:00
if (isVerified) {
[cell setAttributedSubtitle:cell.verifiedSubtitle];
2018-06-13 22:22:03 +02:00
} else {
[cell setAttributedSubtitle:nil];
2018-06-13 22:22:03 +02:00
}
return cell;
}
customRowHeight:UITableViewAutomaticDimension
actionBlock:^{
if (useVerifyAction) {
2017-06-21 21:06:48 +02:00
[weakSelf showSafetyNumberView:recipientId];
} else {
[weakSelf didSelectRecipientId:recipientId];
}
}]];
2017-04-18 22:08:01 +02:00
}
}
- (void)offerResetAllNoLongerVerified
{
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
UIAlertController *actionSheetController = [UIAlertController
alertControllerWithTitle:nil
message:NSLocalizedString(@"GROUP_MEMBERS_RESET_NO_LONGER_VERIFIED_ALERT_MESSAGE",
@"Label for the 'reset all no-longer-verified group members' confirmation alert.")
preferredStyle:UIAlertControllerStyleAlert];
__weak ShowGroupMembersViewController *weakSelf = self;
UIAlertAction *verifyAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *_Nonnull action) {
[weakSelf resetAllNoLongerVerified];
}];
[actionSheetController addAction:verifyAction];
[actionSheetController addAction:[OWSAlerts cancelAction]];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)resetAllNoLongerVerified
{
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
OWSIdentityManager *identityManger = [OWSIdentityManager sharedManager];
NSArray<NSString *> *recipientIds = [self noLongerVerifiedRecipientIds];
for (NSString *recipientId in recipientIds) {
2018-01-30 22:41:25 +01:00
OWSVerificationState verificationState = [identityManger verificationStateForRecipientId:recipientId];
if (verificationState == OWSVerificationStateNoLongerVerified) {
2018-01-30 22:41:25 +01:00
NSData *identityKey = [identityManger identityKeyForRecipientId:recipientId];
if (identityKey.length < 1) {
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"Missing identity key for: %@", recipientId);
continue;
}
2018-02-02 20:07:13 +01:00
[identityManger setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES];
}
}
[self updateTableContents];
}
// Returns a collection of the group members who are "no longer verified".
- (NSArray<NSString *> *)noLongerVerifiedRecipientIds
{
NSMutableArray<NSString *> *result = [NSMutableArray new];
for (NSString *recipientId in self.thread.recipientIdentifiers) {
2018-01-30 22:41:25 +01:00
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]
== OWSVerificationStateNoLongerVerified) {
[result addObject:recipientId];
}
}
return [result copy];
2017-04-18 22:08:01 +02:00
}
- (void)didSelectRecipientId:(NSString *)recipientId
{
OWSAssertDebug(recipientId.length > 0);
2017-04-18 22:08:01 +02:00
ContactsViewHelper *helper = self.contactsViewHelper;
SignalAccount *_Nullable signalAccount = [helper fetchSignalAccountForRecipientId:recipientId];
2017-04-18 22:08:01 +02:00
UIAlertController *actionSheetController =
[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
if (self.contactsViewHelper.contactsManager.supportsContactEditing) {
NSString *contactInfoTitle = signalAccount
? NSLocalizedString(@"GROUP_MEMBERS_VIEW_CONTACT_INFO", @"Button label for the 'show contact info' button")
: NSLocalizedString(
@"GROUP_MEMBERS_ADD_CONTACT_INFO", @"Button label to add information to an unknown contact");
[actionSheetController addAction:[UIAlertAction actionWithTitle:contactInfoTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[self
showContactInfoViewForRecipientId:recipientId];
}]];
}
2017-04-18 22:08:01 +02:00
BOOL isBlocked;
2017-05-01 18:51:59 +02:00
if (signalAccount) {
isBlocked = [helper isRecipientIdBlocked:signalAccount.recipientId];
2017-04-18 22:08:01 +02:00
if (isBlocked) {
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON",
@"Button label for the 'unblock' button")
2017-04-19 17:39:13 +02:00
style:UIAlertActionStyleDefault
2017-04-18 22:08:01 +02:00
handler:^(UIAlertAction *_Nonnull action) {
[BlockListUIUtils
2017-05-01 18:51:59 +02:00
showUnblockSignalAccountActionSheet:signalAccount
fromViewController:self
blockingManager:helper.blockingManager
contactsManager:helper.contactsManager
2017-05-02 17:16:34 +02:00
completionBlock:^(BOOL ignore) {
[self updateTableContents];
}];
2017-04-18 22:08:01 +02:00
}]];
} else {
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BLOCK_LIST_BLOCK_BUTTON",
@"Button label for the 'block' button")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *_Nonnull action) {
[BlockListUIUtils
2017-05-01 18:51:59 +02:00
showBlockSignalAccountActionSheet:signalAccount
fromViewController:self
blockingManager:helper.blockingManager
contactsManager:helper.contactsManager
2017-05-02 17:16:34 +02:00
completionBlock:^(BOOL ignore) {
[self updateTableContents];
}];
2017-04-18 22:08:01 +02:00
}]];
}
} else {
isBlocked = [helper isRecipientIdBlocked:recipientId];
2017-04-18 22:08:01 +02:00
if (isBlocked) {
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON",
@"Button label for the 'unblock' button")
2017-04-19 17:39:13 +02:00
style:UIAlertActionStyleDefault
2017-04-18 22:08:01 +02:00
handler:^(UIAlertAction *_Nonnull action) {
[BlockListUIUtils
showUnblockPhoneNumberActionSheet:recipientId
fromViewController:self
blockingManager:helper.blockingManager
contactsManager:helper.contactsManager
2017-05-02 17:16:34 +02:00
completionBlock:^(BOOL ignore) {
[self updateTableContents];
}];
2017-04-18 22:08:01 +02:00
}]];
} else {
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BLOCK_LIST_BLOCK_BUTTON",
@"Button label for the 'block' button")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *_Nonnull action) {
[BlockListUIUtils
showBlockPhoneNumberActionSheet:recipientId
fromViewController:self
blockingManager:helper.blockingManager
contactsManager:helper.contactsManager
2017-05-02 17:16:34 +02:00
completionBlock:^(BOOL ignore) {
[self updateTableContents];
}];
2017-04-18 22:08:01 +02:00
}]];
}
}
if (!isBlocked) {
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"GROUP_MEMBERS_SEND_MESSAGE",
@"Button label for the 'send message to group member' button")
2017-04-19 17:39:13 +02:00
style:UIAlertActionStyleDefault
2017-04-18 22:08:01 +02:00
handler:^(UIAlertAction *_Nonnull action) {
[self showConversationViewForRecipientId:recipientId];
2017-04-18 22:08:01 +02:00
}]];
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"GROUP_MEMBERS_CALL",
@"Button label for the 'call group member' button")
2017-04-19 17:39:13 +02:00
style:UIAlertActionStyleDefault
2017-04-18 22:08:01 +02:00
handler:^(UIAlertAction *_Nonnull action) {
[self callMember:recipientId];
2017-04-18 22:08:01 +02:00
}]];
[actionSheetController
addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"VERIFY_PRIVACY",
@"Label for button or row which allows users to verify the "
@"safety number of another user.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
2017-06-21 21:06:48 +02:00
[self showSafetyNumberView:recipientId];
}]];
2017-04-18 22:08:01 +02:00
}
[actionSheetController addAction:[OWSAlerts cancelAction]];
2017-04-18 22:08:01 +02:00
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)showContactInfoViewForRecipientId:(NSString *)recipientId
2017-04-18 22:08:01 +02:00
{
OWSAssertDebug(recipientId.length > 0);
2017-05-09 23:55:18 +02:00
[self.contactsViewHelper presentContactViewControllerForRecipientId:recipientId
fromViewController:self
editImmediately:NO];
2017-04-18 22:08:01 +02:00
}
- (void)showConversationViewForRecipientId:(NSString *)recipientId
2017-04-18 22:08:01 +02:00
{
OWSAssertDebug(recipientId.length > 0);
Faster conversation presentation. There are multiple places in the codebase we present a conversation. We used to have some very conservative machinery around how this was done, for fear of failing to present the call view controller, which would have left a hidden call in the background. We've since addressed that concern more thoroughly via the separate calling UIWindow. As such, the remaining presentation machinery is overly complex and inflexible for what we need. Sometimes we want to animate-push the conversation. (tap on home, tap on "send message" in contact card/group members) Sometimes we want to dismiss a modal, to reveal the conversation behind it (contact picker, group creation) Sometimes we want to present the conversation with no animation (becoming active from a notification) We also want to ensure that we're never pushing more than one conversation view controller, which was previously a problem since we were "pushing" a newly constructed VC in response to these myriad actions. It turned out there were certain code paths that caused multiple actions to be fired in rapid succession which pushed multiple ConversationVC's. The built-in method: `setViewControllers:animated` easily ensures we only have one ConversationVC on the stack, while being composable enough to faciliate the various more efficient animations we desire. The only thing lost with the complex methods is that the naive `presentViewController:` can fail, e.g. if another view is already presented. E.g. if an alert appears *just* before the user taps compose, the contact picker will fail to present. Since we no longer depend on this for presenting the CallViewController, this isn't catostrophic, and in fact, arguable preferable, since we want the user to read and dismiss any alert explicitly. // FREEBIE
2018-08-18 22:54:35 +02:00
[SignalApp.sharedApp presentConversationForRecipientId:recipientId
action:ConversationViewActionCompose
animated:YES];
2017-04-18 22:08:01 +02:00
}
- (void)callMember:(NSString *)recipientId
2017-04-18 22:08:01 +02:00
{
Faster conversation presentation. There are multiple places in the codebase we present a conversation. We used to have some very conservative machinery around how this was done, for fear of failing to present the call view controller, which would have left a hidden call in the background. We've since addressed that concern more thoroughly via the separate calling UIWindow. As such, the remaining presentation machinery is overly complex and inflexible for what we need. Sometimes we want to animate-push the conversation. (tap on home, tap on "send message" in contact card/group members) Sometimes we want to dismiss a modal, to reveal the conversation behind it (contact picker, group creation) Sometimes we want to present the conversation with no animation (becoming active from a notification) We also want to ensure that we're never pushing more than one conversation view controller, which was previously a problem since we were "pushing" a newly constructed VC in response to these myriad actions. It turned out there were certain code paths that caused multiple actions to be fired in rapid succession which pushed multiple ConversationVC's. The built-in method: `setViewControllers:animated` easily ensures we only have one ConversationVC on the stack, while being composable enough to faciliate the various more efficient animations we desire. The only thing lost with the complex methods is that the naive `presentViewController:` can fail, e.g. if another view is already presented. E.g. if an alert appears *just* before the user taps compose, the contact picker will fail to present. Since we no longer depend on this for presenting the CallViewController, this isn't catostrophic, and in fact, arguable preferable, since we want the user to read and dismiss any alert explicitly. // FREEBIE
2018-08-18 22:54:35 +02:00
[SignalApp.sharedApp presentConversationForRecipientId:recipientId
action:ConversationViewActionAudioCall
animated:YES];
}
2017-06-21 21:06:48 +02:00
- (void)showSafetyNumberView:(NSString *)recipientId
{
OWSAssertDebug(recipientId.length > 0);
[FingerprintViewController presentFromViewController:self recipientId:recipientId];
}
#pragma mark - ContactsViewHelperDelegate
- (void)contactsViewHelperDidUpdateContacts
2017-04-18 22:08:01 +02:00
{
[self updateTableContents];
2017-04-18 22:08:01 +02:00
}
- (BOOL)shouldHideLocalNumber
2017-04-18 22:08:01 +02:00
{
return YES;
2017-04-18 22:08:01 +02:00
}
2017-05-09 23:55:18 +02:00
#pragma mark - ContactEditingDelegate
- (void)didFinishEditingContact
{
OWSLogDebug(@"");
2017-05-09 23:55:18 +02:00
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - CNContactViewControllerDelegate
- (void)contactViewController:(CNContactViewController *)viewController
didCompleteWithContact:(nullable CNContact *)contact
{
OWSLogDebug(@"done editing contact.");
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Notifications
- (void)identityStateDidChange:(NSNotification *)notification
{
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
[self updateTableContents];
}
@end
NS_ASSUME_NONNULL_END