From e6842a787a67f59fb8f6dce6e422b87a52bf5945 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 25 Jul 2025 12:28:39 +0200 Subject: [PATCH] pr suggestions --- Riot/Categories/MXRoom.swift | 11 +++- .../Services/RoomContextActionService.swift | 15 +++-- .../MXKRoomMemberDetailsViewController.m | 57 ++++++++----------- .../Members/RoomParticipantsViewController.m | 2 +- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/Riot/Categories/MXRoom.swift b/Riot/Categories/MXRoom.swift index 5446972e2..1ff03e03a 100644 --- a/Riot/Categories/MXRoom.swift +++ b/Riot/Categories/MXRoom.swift @@ -22,8 +22,15 @@ extension MXRoom { return false } - return joinedMembers.contains { member in - member.userId != userID && state.powerLevelOfUser(withUserID: member.userId) >= requiredPowerLevel.rawValue + var areOtherMembers = false + for member in joinedMembers where member.userId != userID { + // User is not the last member in the whole room. + areOtherMembers = true + // If there are other owners/admins the user can leave + if state.powerLevelOfUser(withUserID: member.userId) >= requiredPowerLevel.rawValue { + return false + } } + return areOtherMembers } } diff --git a/Riot/Modules/ContextMenu/Services/RoomContextActionService.swift b/Riot/Modules/ContextMenu/Services/RoomContextActionService.swift index b40cabfa8..fcf0e5d57 100644 --- a/Riot/Modules/ContextMenu/Services/RoomContextActionService.swift +++ b/Riot/Modules/ContextMenu/Services/RoomContextActionService.swift @@ -139,21 +139,20 @@ class RoomContextActionService: NSObject, RoomContextActionServiceProtocol { Task { if try await room.isLastOwner() { - let alertController = await UIAlertController(title: VectorL10n.error, message: VectorL10n.roomParticipantsLeaveNotAllowedForLastOwnerMsg, preferredStyle: .alert) - await alertController.addAction(UIAlertAction(title: VectorL10n.ok, style: .cancel, handler: nil)) await MainActor.run { + let alertController = UIAlertController(title: VectorL10n.error, message: VectorL10n.roomParticipantsLeaveNotAllowedForLastOwnerMsg, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: VectorL10n.ok, style: .cancel, handler: nil)) self.delegate?.roomContextActionService(self, presentAlert: alertController) } } else { let title = room.isDirect ? VectorL10n.roomParticipantsLeavePromptTitleForDm : VectorL10n.roomParticipantsLeavePromptTitle let message = room.isDirect ? VectorL10n.roomParticipantsLeavePromptMsgForDm : VectorL10n.roomParticipantsLeavePromptMsg - - let alertController = await UIAlertController(title: title, message: message, preferredStyle: .alert) - await alertController.addAction(UIAlertAction(title: VectorL10n.cancel, style: .cancel, handler: nil)) - await alertController.addAction(UIAlertAction(title: VectorL10n.leave, style: .default, handler: { action in - self.leaveRoom() - })) await MainActor.run { + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: VectorL10n.cancel, style: .cancel, handler: nil)) + alertController.addAction(UIAlertAction(title: VectorL10n.leave, style: .default, handler: { [weak self] action in + self?.leaveRoom() + })) self.delegate?.roomContextActionService(self, presentAlert: alertController) } } diff --git a/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m b/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m index 908a3e72c..c8cef3e34 100644 --- a/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m +++ b/Riot/Modules/MatrixKit/Controllers/MXKRoomMemberDetailsViewController.m @@ -220,7 +220,7 @@ Please see LICENSE in the repository root for full details. } case MXKRoomMemberDetailsActionLeave: { - __weak typeof(self) weakSelf = self; + MXWeakify(self); [self.mxRoom isLastOwnerWithCompletionHandler:^(BOOL isLastOwner, NSError* error){ if (isLastOwner) { @@ -231,43 +231,34 @@ Please see LICENSE in the repository root for full details. [isLastOwnerPrompt addAction:[UIAlertAction actionWithTitle:[VectorL10n ok] style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { - if (weakSelf) - { - typeof(self) self = weakSelf; - self->currentAlert = nil; - } + MXStrongifyAndReturnIfNil(self); + self->currentAlert = nil; }]]; - if (weakSelf) - { - typeof(self) self = weakSelf; - dispatch_async(dispatch_get_main_queue(), ^{ - [self presentViewController:isLastOwnerPrompt animated:YES completion:nil]; - self->currentAlert = isLastOwnerPrompt; - }); - } + MXStrongifyAndReturnIfNil(self); + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:isLastOwnerPrompt animated:YES completion:nil]; + self->currentAlert = isLastOwnerPrompt; + }); } else { - if (weakSelf) - { - typeof(self) self = weakSelf; - [self addPendingActionMask]; - [self.mxRoom leave:^{ - - [self removePendingActionMask]; - [self withdrawViewControllerAnimated:YES completion:nil]; - - } failure:^(NSError *error) { - - [self removePendingActionMask]; - MXLogDebug(@"[MXKRoomMemberDetailsVC] Leave room %@ failed", self->mxRoom.roomId); - // Notify MatrixKit user - NSString *myUserId = self.mainSession.myUser.userId; - [[NSNotificationCenter defaultCenter] postNotificationName:kMXKErrorNotification object:error userInfo:myUserId ? @{kMXKErrorUserIdKey: myUserId} : nil]; - - }]; - } + MXStrongifyAndReturnIfNil(self); + [self addPendingActionMask]; + [self.mxRoom leave:^{ + + [self removePendingActionMask]; + [self withdrawViewControllerAnimated:YES completion:nil]; + + } failure:^(NSError *error) { + + [self removePendingActionMask]; + MXLogDebug(@"[MXKRoomMemberDetailsVC] Leave room %@ failed", self->mxRoom.roomId); + // Notify MatrixKit user + NSString *myUserId = self.mainSession.myUser.userId; + [[NSNotificationCenter defaultCenter] postNotificationName:kMXKErrorNotification object:error userInfo:myUserId ? @{kMXKErrorUserIdKey: myUserId} : nil]; + + }]; } }]; break; diff --git a/Riot/Modules/Room/Members/RoomParticipantsViewController.m b/Riot/Modules/Room/Members/RoomParticipantsViewController.m index 41a105508..b0a4188fc 100644 --- a/Riot/Modules/Room/Members/RoomParticipantsViewController.m +++ b/Riot/Modules/Room/Members/RoomParticipantsViewController.m @@ -1381,7 +1381,7 @@ Please see LICENSE in the repository root for full details. - (void)leaveRoom { MXWeakify(self); - [self.mxRoom isLastOwnerWithCompletionHandler:^(BOOL isLastOnwer, NSError* error) { + [self.mxRoom isLastOwnerWithCompletionHandler:^(BOOL isLastOwner, NSError* error) { if (isLastOwner) { MXStrongifyAndReturnIfNil(self);