From 30a0561251769417693965e8dd9032635b323cc0 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Tue, 23 Jan 2024 12:05:30 +0100 Subject: [PATCH] App now prevents redacting own messages if there is no permission (#2368) --- .../Mocks/Generated/GeneratedMocks.swift | 21 +++++++++++++++++++ .../RoomScreenInteractionHandler.swift | 9 +++++++- .../Sources/Services/Room/RoomProxy.swift | 11 +++++++++- .../Services/Room/RoomProxyProtocol.swift | 2 ++ changelog.d/pr-2368.bugfix | 1 + 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 changelog.d/pr-2368.bugfix diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index 0a2a73cd8d..9a81861ddd 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -2180,6 +2180,27 @@ class RoomProxyMock: RoomProxyProtocol { return canUserRedactOtherUserIDReturnValue } } + //MARK: - canUserRedactOwn + + var canUserRedactOwnUserIDCallsCount = 0 + var canUserRedactOwnUserIDCalled: Bool { + return canUserRedactOwnUserIDCallsCount > 0 + } + var canUserRedactOwnUserIDReceivedUserID: String? + var canUserRedactOwnUserIDReceivedInvocations: [String] = [] + var canUserRedactOwnUserIDReturnValue: Result! + var canUserRedactOwnUserIDClosure: ((String) async -> Result)? + + func canUserRedactOwn(userID: String) async -> Result { + canUserRedactOwnUserIDCallsCount += 1 + canUserRedactOwnUserIDReceivedUserID = userID + canUserRedactOwnUserIDReceivedInvocations.append(userID) + if let canUserRedactOwnUserIDClosure = canUserRedactOwnUserIDClosure { + return await canUserRedactOwnUserIDClosure(userID) + } else { + return canUserRedactOwnUserIDReturnValue + } + } //MARK: - canUserTriggerRoomNotification var canUserTriggerRoomNotificationUserIDCallsCount = 0 diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenInteractionHandler.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenInteractionHandler.swift index 0889cd0949..024d05011f 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenInteractionHandler.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenInteractionHandler.swift @@ -52,6 +52,7 @@ class RoomScreenInteractionHandler { private var voiceMessageRecorderObserver: AnyCancellable? private var canCurrentUserRedactOthers = false + private var canCurrentUserRedactSelf = false private var resumeVoiceMessagePlaybackAfterScrubbing = false init(roomProxy: RoomProxyProtocol, @@ -86,6 +87,12 @@ class RoomScreenInteractionHandler { } else { canCurrentUserRedactOthers = false } + + if case let .success(value) = await roomProxy.canUserRedactOwn(userID: roomProxy.ownUserID) { + canCurrentUserRedactSelf = value + } else { + canCurrentUserRedactSelf = false + } guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID), let eventTimelineItem = timelineItem as? EventBasedTimelineItemProtocol else { @@ -604,7 +611,7 @@ class RoomScreenInteractionHandler { // MARK: - Private private func canRedactItem(_ item: EventBasedTimelineItemProtocol) -> Bool { - item.isOutgoing || (canCurrentUserRedactOthers && !roomProxy.isDirect) + item.isOutgoing ? canCurrentUserRedactSelf : canCurrentUserRedactOthers && !roomProxy.isDirect } private func buildReplyInfo(for item: EventBasedTimelineItemProtocol) -> ReplyInfo { diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index f2e3fa2f32..e73c9b66d9 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -332,7 +332,16 @@ class RoomProxy: RoomProxyProtocol { do { return try await .success(room.canUserRedactOther(userId: userID)) } catch { - MXLog.error("Failed checking if the user can redact with error: \(error)") + MXLog.error("Failed checking if the user can redact others with error: \(error)") + return .failure(.failedCheckingPermission) + } + } + + func canUserRedactOwn(userID: String) async -> Result { + do { + return try await .success(room.canUserRedactOwn(userId: userID)) + } catch { + MXLog.error("Failed checking if the user can redact self with error: \(error)") return .failure(.failedCheckingPermission) } } diff --git a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift index 2c68b7cf46..c91b38c918 100644 --- a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift +++ b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift @@ -99,6 +99,8 @@ protocol RoomProxyProtocol { func canUserRedactOther(userID: String) async -> Result + func canUserRedactOwn(userID: String) async -> Result + func canUserTriggerRoomNotification(userID: String) async -> Result func canUserJoinCall(userID: String) async -> Result diff --git a/changelog.d/pr-2368.bugfix b/changelog.d/pr-2368.bugfix new file mode 100644 index 0000000000..1d51ab9b6d --- /dev/null +++ b/changelog.d/pr-2368.bugfix @@ -0,0 +1 @@ +You can't redact a message of your own if the room does not allow it. \ No newline at end of file