Skip to content

Commit

Permalink
A public room may become private (element-hq#71)
Browse files Browse the repository at this point in the history
* A public room may become private

- The encryption is then enabled by default
- The new members can access only on invite

We improve here the UI. We prompt the user as soon as he switches the toggle button. The action is not delayed to the "Save" action. We remove the room from the publics as soon as the user decides the change.
  • Loading branch information
giomfo authored Oct 8, 2018
1 parent 8d68af5 commit 331449f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 97 deletions.
2 changes: 2 additions & 0 deletions Tchap/Assets/Localizations/fr.lproj/Tchap.strings
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
////////////////////////////////////////////////////////////////////////////////
// MARK: Room Settings
"room_settings_tab_title" = "Paramètres";
"room_settings_remove_from_rooms_directory" = "Retirer ce salon de la liste des salons publics";
"room_settings_remove_from_rooms_directory_prompt" = "Cette action est irréversible.\nVoulez-vous vraiment retirer ce salon des salons publics ?";

////////////////////////////////////////////////////////////////////////////////
// MARK: Public rooms
Expand Down
4 changes: 4 additions & 0 deletions Tchap/Constants/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ internal enum TchapL10n {
internal static let roomMemberDetailsActionChat = TchapL10n.tr("Tchap", "room_member_details_action_chat")
/// Membres
internal static let roomMembersTabTitle = TchapL10n.tr("Tchap", "room_members_tab_title")
/// Retirer ce salon de la liste des salons publics
internal static let roomSettingsRemoveFromRoomsDirectory = TchapL10n.tr("Tchap", "room_settings_remove_from_rooms_directory")
/// Cette action est irréversible.\nVoulez-vous vraiment retirer ce salon des salons publics ?
internal static let roomSettingsRemoveFromRoomsDirectoryPrompt = TchapL10n.tr("Tchap", "room_settings_remove_from_rooms_directory_prompt")
/// Paramètres
internal static let roomSettingsTabTitle = TchapL10n.tr("Tchap", "room_settings_tab_title")
/// Aucun résultat
Expand Down
205 changes: 108 additions & 97 deletions Tchap/Modules/Room/Settings/RoomSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -966,74 +966,6 @@ - (IBAction)onSave:(id)sender
}
return;
}

// Room directory visibility
MXRoomDirectoryVisibility directoryVisibility = [updatedItemsDict objectForKey:kRoomSettingsDirectoryKey];
if (directoryVisibility)
{
// Sanity check: for the moment, the user is only allowed to remove a public room from the room directory
if (![directoryVisibility isEqualToString:kMXRoomDirectoryVisibilityPrivate]) {
[self->updatedItemsDict removeObjectForKey:kRoomSettingsDirectoryKey];
[self onSave:nil];
return;
}

void (^failure)(NSError *error) = ^(NSError *error){

NSLog(@"[RoomSettingsViewController] Update room directory visibility failed");
MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;

dispatch_async(dispatch_get_main_queue(), ^{

NSString* message = error.localizedDescription;
if (!message.length)
{
message = NSLocalizedStringFromTable(@"room_details_fail_to_update_room_directory_visibility", @"Vector", nil);
}
[self onSaveFailed:message withKeys:@[kRoomSettingsDirectoryKey]];

});

};

// Update first the joinrule to INVITE
pendingOperation = [mxRoom setJoinRule:kMXRoomJoinRuleInvite success:^{

MXStrongifyAndReturnIfNil(self);
// Turn on the encryption in this room
MXWeakify(self);
self->pendingOperation = [self->mxRoom enableEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm success:^{

MXStrongifyAndReturnIfNil(self);
// Remove the room from the room directory
MXWeakify(self);
self->pendingOperation = [self->mxRoom setDirectoryVisibility:directoryVisibility success:^{

MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;
[self->updatedItemsDict removeObjectForKey:kRoomSettingsDirectoryKey];
[self onSave:nil];

} failure:failure];

} failure:^(NSError *error) {

NSLog(@"[RoomSettingsViewController] Enabling encrytion failed. Error: %@", error);
failure(error);

}];


} failure:^(NSError *error) {

NSLog(@"[RoomSettingsViewController] Update join rule for a public room failed");
failure(error);

}];

return;
}
}

[self getNavigationItem].rightBarButtonItem.enabled = NO;
Expand Down Expand Up @@ -1304,23 +1236,13 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
else if (indexPath.row == directoryVisibilityIndex)
{
MXKTableViewCellWithLabelAndSwitch *directoryToggleCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath];

directoryToggleCell.mxkLabel.text = NSLocalizedStringFromTable(@"room_details_access_section_directory_toggle", @"Vector", nil);

[directoryToggleCell.mxkSwitch addTarget:self action:@selector(toggleDirectoryVisibility:) forControlEvents:UIControlEventValueChanged];
MXKTableViewCellWithLabelAndSwitch *removeFromDirectoryCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath];

if ([updatedItemsDict objectForKey:kRoomSettingsDirectoryKey])
{
directoryToggleCell.mxkSwitch.on = ((NSNumber*)[updatedItemsDict objectForKey:kRoomSettingsDirectoryKey]).boolValue;
}
else
{
// Use the last retrieved value if any
directoryToggleCell.mxkSwitch.on = actualDirectoryVisibility ? [actualDirectoryVisibility isEqualToString:kMXRoomDirectoryVisibilityPublic] : NO;
}
removeFromDirectoryCell.mxkLabel.text = NSLocalizedStringFromTable(@"room_settings_remove_from_rooms_directory", @"Tchap", nil);
removeFromDirectoryCell.mxkSwitch.on = NO;
[removeFromDirectoryCell.mxkSwitch addTarget:self action:@selector(toggleRemoveFromDirectory:) forControlEvents:UIControlEventValueChanged];

cell = directoryToggleCell;
cell = removeFromDirectoryCell;
}
}
else if (indexPath.section == ROOM_SETTINGS_BANNED_USERS_SECTION_INDEX)
Expand Down Expand Up @@ -1536,23 +1458,112 @@ - (void)toggleRoomNotification:(UISwitch*)theSwitch
[self getNavigationItem].rightBarButtonItem.enabled = (updatedItemsDict.count != 0);
}

- (void)toggleDirectoryVisibility:(UISwitch*)theSwitch
- (void)toggleRemoveFromDirectory:(UISwitch*)theSwitch
{
MXRoomDirectoryVisibility visibility = theSwitch.on ? kMXRoomDirectoryVisibilityPublic : kMXRoomDirectoryVisibilityPrivate;
// Prompt the user before removing the room from the rooms directory
MXWeakify(self);
[currentAlert dismissViewControllerAnimated:NO completion:nil];

// Check whether the actual settings has been retrieved
if (actualDirectoryVisibility)
{
if ([visibility isEqualToString:actualDirectoryVisibility])
{
[updatedItemsDict removeObjectForKey:kRoomSettingsDirectoryKey];
}
else
{
[updatedItemsDict setObject:visibility forKey:kRoomSettingsDirectoryKey];
}
currentAlert = [UIAlertController alertControllerWithTitle:NSLocalizedStringFromTable(@"warning", @"Vector", nil)
message:NSLocalizedStringFromTable(@"room_settings_remove_from_rooms_directory_prompt", @"Tchap", nil)
preferredStyle:UIAlertControllerStyleAlert];

[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"yes"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {

MXStrongifyAndReturnIfNil(self);
self->currentAlert = nil;
[self removeFromRoomsDirectory];

}]];


[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"]
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {

MXStrongifyAndReturnIfNil(self);
self->currentAlert = nil;
// Reset the switch change
theSwitch.on = NO;

}]];

[currentAlert mxk_setAccessibilityIdentifier:@"RoomSettingsVCLeaveAlert"];
[self presentViewController:currentAlert animated:YES completion:nil];
}

- (void)removeFromRoomsDirectory
{
[self startActivityIndicator];
MXWeakify(self);

void (^failure)(NSError *error) = ^(NSError *error){

[self getNavigationItem].rightBarButtonItem.enabled = (updatedItemsDict.count != 0);
NSLog(@"[RoomSettingsViewController] Update room directory visibility failed");
MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;
[self stopActivityIndicator];
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
[self refreshRoomSettings];

};

// Set the joinRule to INVITE if this is not already the case
if (![mxRoomState.joinRule isEqualToString:kMXRoomJoinRuleInvite])
{
NSLog(@"[RoomSettingsViewController] Update join rule for a public room");
pendingOperation = [mxRoom setJoinRule:kMXRoomJoinRuleInvite success:^{

MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;
dispatch_async(dispatch_get_main_queue(), ^{
// Pursue the operation
[self removeFromRoomsDirectory];

});

} failure:^(NSError *error) {

NSLog(@"[RoomSettingsViewController] Update join rule for a public room failed");
failure(error);

}];
}
else if (!self->mxRoom.summary.isEncrypted)
{
// Turn on the encryption if it is not already enabled
NSLog(@"[RoomSettingsViewController] Enable encrytion");
self->pendingOperation = [self->mxRoom enableEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm success:^{

MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;
dispatch_async(dispatch_get_main_queue(), ^{
// Pursue the operation
[self removeFromRoomsDirectory];

});

} failure:^(NSError *error) {

NSLog(@"[RoomSettingsViewController] Enabling encrytion failed. Error: %@", error);
failure(error);

}];
}
else
{
// Remove the room from the rooms directory
self->pendingOperation = [self->mxRoom setDirectoryVisibility:kMXRoomDirectoryVisibilityPrivate success:^{

MXStrongifyAndReturnIfNil(self);
self->pendingOperation = nil;
[self stopActivityIndicator];
[self refreshRoomSettings];

} failure:failure];
}
}

Expand Down

0 comments on commit 331449f

Please sign in to comment.