Skip to content

Commit

Permalink
Merge pull request #1933 from nextcloud/rework-can-leave-delete-conve…
Browse files Browse the repository at this point in the history
…rsation

Rework can leave delete conversation
  • Loading branch information
Ivansss authored Jan 10, 2025
2 parents e5d2f55 + 6ddb687 commit 816e876
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 74 deletions.
21 changes: 2 additions & 19 deletions NextcloudTalk/AddParticipantsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ @interface AddParticipantsTableViewController () <UISearchBarDelegate, UISearchC
NSMutableDictionary *_participants;
NSArray *_indexes;
NCRoom *_room;
NSArray *_participantsInRoom;
UISearchController *_searchController;
ResultMultiSelectionTableViewController *_resultTableViewController;
NSMutableArray *_selectedParticipants;
Expand All @@ -43,7 +42,6 @@ - (instancetype)initForRoom:(NCRoom *)room

if (room) {
_room = room;
_participantsInRoom = [room.participants valueForKey:@"self"];
}

_participants = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -286,28 +284,14 @@ - (void)removeAddingParticipantsView

#pragma mark - Participants actions

- (NSMutableArray *)filterContacts:(NSMutableArray *)contacts
{
NSMutableArray *participants = [[NSMutableArray alloc] init];
for (NCUser *user in contacts) {
if (![_participantsInRoom containsObject:user.userId]) {
[participants addObject:user];
} else if (![user.source isEqualToString:kParticipantTypeUser]) {
[participants addObject:user];
}
}
return participants;
}

- (void)getPossibleParticipants
{
TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[[NCAPIController sharedInstance] getContactsForAccount:activeAccount forRoom:_room.token groupRoom:YES withSearchParam:nil andCompletionBlock:^(NSArray *indexes, NSMutableDictionary *contacts, NSMutableArray *contactList, NSError *error) {
if (!error) {
NSMutableArray *storedContacts = [NCContact contactsForAccountId:activeAccount.accountId contains:nil];
NSMutableArray *combinedContactList = [NCUser combineUsersArray:storedContacts withUsersArray:contactList];
NSMutableArray *filteredParticipants = [self filterContacts:combinedContactList];
NSMutableDictionary *participants = [NCUser indexedUsersFromUsersArray:filteredParticipants];
NSMutableDictionary *participants = [NCUser indexedUsersFromUsersArray:combinedContactList];
self->_participants = participants;
self->_indexes = [[participants allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[self->_participantsBackgroundView.loadingView stopAnimating];
Expand All @@ -328,8 +312,7 @@ - (void)searchForParticipantsWithString:(NSString *)searchString
if (!error) {
NSMutableArray *storedContacts = [NCContact contactsForAccountId:activeAccount.accountId contains:searchString];
NSMutableArray *combinedContactList = [NCUser combineUsersArray:storedContacts withUsersArray:contactList];
NSMutableArray *filteredParticipants = [self filterContacts:combinedContactList];
NSMutableDictionary *participants = [NCUser indexedUsersFromUsersArray:filteredParticipants];
NSMutableDictionary *participants = [NCUser indexedUsersFromUsersArray:combinedContactList];
NSArray *sortedIndexes = [[participants allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[self->_resultTableViewController setSearchResultContacts:participants withIndexes:sortedIndexes];
} else {
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/NCAPIController.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ - (SDWebImageDownloaderRequestModifier *)getRequestModifierForAccount:(TalkAccou

- (NSInteger)conversationAPIVersionForAccount:(TalkAccount *)account
{
NSInteger conversationAPIVersion = APIv1;
NSInteger conversationAPIVersion = APIv2;
if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityChatReadStatus forAccountId:account.accountId]) {
conversationAPIVersion = APIv3;
}
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/NCDatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ extern NSString * const kCapabilityEditMessagesNoteToSelf;
extern NSString * const kCapabilityChatSummary;
extern NSString * const kCapabilityArchivedConversationsV2;
extern NSString * const kCapabilityCallNotificationState;
extern NSString * const kCapabilityCallForceMute;

extern NSString * const kNotificationsCapabilityExists;
extern NSString * const kNotificationsCapabilityTestPush;
Expand Down
5 changes: 3 additions & 2 deletions NextcloudTalk/NCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk";
NSString *const kTalkDatabaseFileName = @"talk.realm";
uint64_t const kTalkDatabaseSchemaVersion = 71;
uint64_t const kTalkDatabaseSchemaVersion = 72;

NSString * const kCapabilitySystemMessages = @"system-messages";
NSString * const kCapabilityNotificationLevels = @"notification-levels";
Expand Down Expand Up @@ -78,11 +78,12 @@
NSString * const kCapabilityChatSummary = @"chat-summary-api";
NSString * const kCapabilityArchivedConversationsV2 = @"archived-conversations-v2";
NSString * const kCapabilityCallNotificationState = @"call-notification-state-api";
NSString * const kCapabilityForceMute = @"force-mute";

NSString * const kNotificationsCapabilityExists = @"exists";
NSString * const kNotificationsCapabilityTestPush = @"test-push";

NSString * const kMinimumRequiredTalkCapability = kCapabilitySystemMessages; // Talk 4.0 is the minimum required version
NSString * const kMinimumRequiredTalkCapability = kCapabilityForceMute; // Talk 9.0 is the minimum required version

NSString * const NCDatabaseManagerPendingFederationInvitationsDidChange = @"NCDatabaseManagerPendingFederationInvitationsDidChange";
NSString * const NCDatabaseManagerRoomCapabilitiesChangedNotification = @"NCDatabaseManagerRoomCapabilitiesChangedNotification";
Expand Down
1 change: 0 additions & 1 deletion NextcloudTalk/NCRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ extern NSString * const NCRoomObjectTypeRoom;
@property (nonatomic, assign) NSInteger unreadMessages;
@property (nonatomic, assign) BOOL unreadMention;
@property (nonatomic, assign) BOOL unreadMentionDirect;
@property (nonatomic, strong) RLMArray<RLMString> *participants;
@property (nonatomic, assign) NSInteger lastActivity;
@property (nonatomic, copy, nullable) NSString *lastMessageId;
@property (nonatomic, copy) NSString *lastMessageProxiedJSONString;
Expand Down
6 changes: 0 additions & 6 deletions NextcloudTalk/NCRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ + (instancetype)roomWithDictionary:(NSDictionary *)roomDict andAccountId:(NSStri
} else {
room.displayName = [displayName stringValue];
}

id participants = [roomDict objectForKey:@"participants"];
if ([participants isKindOfClass:[NSDictionary class]]) {
room.participants = (RLMArray<RLMString> *)[participants allKeys];
}

// Optional attribute
id status = [roomDict objectForKey:@"status"];
Expand Down Expand Up @@ -146,7 +141,6 @@ + (void)updateRoom:(NCRoom *)managedRoom withRoom:(NCRoom *)room
managedRoom.unreadMessages = room.unreadMessages;
managedRoom.unreadMention = room.unreadMention;
managedRoom.unreadMentionDirect = room.unreadMentionDirect;
managedRoom.participants = room.participants;
managedRoom.lastActivity = room.lastActivity;
managedRoom.lastMessageId = room.lastMessageId;
managedRoom.lastMessageProxiedJSONString = room.lastMessageProxiedJSONString;
Expand Down
17 changes: 1 addition & 16 deletions NextcloudTalk/NCRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ import Realm
return lockedOneToOne || lockedOther
}

public var isLeavable: Bool {
// Allow users to leave when there are no moderators in the room
// (No need to check room type because in one2one rooms users will always be moderators)
// or when in a group call and there are other participants.
// We can also check "canLeaveConversation" since v2

if self.type != .oneToOne && self.type != .formerOneToOne && self.participants.count > 1 {
return true
}

return self.canLeaveConversation || self.canModerate
}

public var userCanStartCall: Bool {
if NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityStartCallFlag) && !self.canStartCall {
return false
Expand Down Expand Up @@ -162,10 +149,8 @@ import Realm
public var deletionMessage: String {
var message = NSLocalizedString("Do you really want to delete this conversation?", comment: "")

if self.type == .oneToOne || self.type == .formerOneToOne {
if self.type == .oneToOne {
message = String(format: NSLocalizedString("If you delete the conversation, it will also be deleted for %@", comment: ""), self.displayName)
} else if self.participants.count > 1 {
message = NSLocalizedString("If you delete the conversation, it will also be deleted for all other participants.", comment: "")
}

return message
Expand Down
4 changes: 1 addition & 3 deletions NextcloudTalk/NCRoomsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,7 @@ - (void)joinOrCreateChatWithUser:(NSString *)userId usingAccountId:(NSString *)a
NSArray *accountRooms = [[NCRoomsManager sharedInstance] roomsForAccountId:accountId withRealm:nil];

for (NCRoom *room in accountRooms) {
NSArray *participantsInRoom = [room.participants valueForKey:@"self"];

if (room.type == kNCRoomTypeOneToOne && [participantsInRoom containsObject:userId]) {
if (room.type == kNCRoomTypeOneToOne && [room.name isEqualToString:userId]) {
// Room already exists -> join the room
[self startChatWithRoomToken:room.token];

Expand Down
17 changes: 9 additions & 8 deletions NextcloudTalk/RoomInfoTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,17 @@ - (NSArray *)getRoomDestructiveActions
{
NSMutableArray *actions = [[NSMutableArray alloc] init];
// Leave room
if (_room.isLeavable && _room.type != kNCRoomTypeNoteToSelf) {
if (_room.canLeaveConversation) {
[actions addObject:[NSNumber numberWithInt:kDestructiveActionLeave]];
}
// Clear history
if ((_room.canModerate || _room.type == kNCRoomTypeNoteToSelf) &&
[[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityClearHistory]) {
[actions addObject:[NSNumber numberWithInt:kDestructiveActionClearHistory]];
}
// Delete room
if (_room.canModerate || _room.type == kNCRoomTypeNoteToSelf) {

if (_room.canDeleteConversation) {
// Clear history
if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityClearHistory]) {
[actions addObject:[NSNumber numberWithInt:kDestructiveActionClearHistory]];
}

// Delete room
[actions addObject:[NSNumber numberWithInt:kDestructiveActionDelete]];
}
return [NSArray arrayWithArray:actions];
Expand Down
38 changes: 23 additions & 15 deletions NextcloudTalk/RoomsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwip
}];
deleteAction.image = [UIImage systemImageNamed:@"trash"];

if (room.isLeavable && room.type != kNCRoomTypeNoteToSelf) {
if (room.canLeaveConversation) {
deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:nil
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self leaveRoom:room];
Expand Down Expand Up @@ -1722,27 +1722,35 @@ - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuCo

[actions addObject:roomInfoAction];

UIAction *deleteAction;
NSMutableArray *destructiveActions = [[NSMutableArray alloc] init];

if (room.isLeavable && room.type != kNCRoomTypeNoteToSelf) {
deleteAction = [UIAction actionWithTitle:NSLocalizedString(@"Leave conversation", nil) image:[UIImage systemImageNamed:@"arrow.right.square"] identifier:nil handler:^(UIAction *action) {
if (room.canLeaveConversation) {
UIAction *leaveAction = [UIAction actionWithTitle:NSLocalizedString(@"Leave conversation", nil) image:[UIImage systemImageNamed:@"arrow.right.square"] identifier:nil handler:^(UIAction *action) {
[weakSelf leaveRoom:room];
}];
} else {
deleteAction = [UIAction actionWithTitle:NSLocalizedString(@"Delete conversation", nil) image:[UIImage systemImageNamed:@"trash"] identifier:nil handler:^(UIAction *action) {
[weakSelf deleteRoom:room];
}];

leaveAction.attributes = UIMenuElementAttributesDestructive;
[destructiveActions addObject:leaveAction];
}

deleteAction.attributes = UIMenuElementAttributesDestructive;
if (room.canDeleteConversation) {
UIAction *deleteAction = [UIAction actionWithTitle:NSLocalizedString(@"Delete conversation", nil) image:[UIImage systemImageNamed:@"trash"] identifier:nil handler:^(UIAction *action) {
[weakSelf deleteRoom:room];
}];

UIMenu *deleteMenu = [UIMenu menuWithTitle:@""
image:nil
identifier:nil
options:UIMenuOptionsDisplayInline
children:@[deleteAction]];
deleteAction.attributes = UIMenuElementAttributesDestructive;
[destructiveActions addObject:deleteAction];
}

[actions addObject:deleteMenu];
if (destructiveActions.count > 0) {
UIMenu *deleteMenu = [UIMenu menuWithTitle:@""
image:nil
identifier:nil
options:UIMenuOptionsDisplayInline
children:destructiveActions];

[actions addObject:deleteMenu];
}

UIMenu *menu = [UIMenu menuWithTitle:@"" children:actions];

Expand Down
3 changes: 0 additions & 3 deletions NextcloudTalk/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,6 @@
/* No comment provided by engineer. */
"If you delete the conversation, it will also be deleted for %@" = "If you delete the conversation, it will also be deleted for %@";

/* No comment provided by engineer. */
"If you delete the conversation, it will also be deleted for all other participants." = "If you delete the conversation, it will also be deleted for all other participants.";

/* No comment provided by engineer. */
"If you enable your camera, this call will be interrupted for a few seconds." = "If you enable your camera, this call will be interrupted for a few seconds.";

Expand Down

0 comments on commit 816e876

Please sign in to comment.