From 264dda0ab370e795666511c0f56cbd52aa4f11fd Mon Sep 17 00:00:00 2001 From: Ben Pollman Date: Tue, 29 Oct 2024 14:56:51 +1100 Subject: [PATCH 1/6] Channel Actions - Only show Leave Group if user has "leave-channel" permission --- .../ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift | 7 +++++-- .../ChatChannelList/DefaultChannelActions.swift | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift index b3deb4d66..7e3bd71c9 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift @@ -37,8 +37,11 @@ public class ChatChannelInfoViewModel: ObservableObject, ChatChannelControllerDe @Published public var addUsersShown = false public var shouldShowLeaveConversationButton: Bool { - channel.ownCapabilities.contains(.deleteChannel) - || !channel.isDirectMessageChannel + if channel.isDirectMessageChannel { + return channel.ownCapabilities.contains(.deleteChannel) + } else { + return channel.ownCapabilities.contains(.leaveChannel) + } } public var canRenameChannel: Bool { diff --git a/Sources/StreamChatSwiftUI/ChatChannelList/DefaultChannelActions.swift b/Sources/StreamChatSwiftUI/ChatChannelList/DefaultChannelActions.swift index f1523e182..51c587ce2 100644 --- a/Sources/StreamChatSwiftUI/ChatChannelList/DefaultChannelActions.swift +++ b/Sources/StreamChatSwiftUI/ChatChannelList/DefaultChannelActions.swift @@ -25,7 +25,7 @@ extension ChannelAction { actions.append(viewInfo) - if !channel.isDirectMessageChannel, let userId = chatClient.currentUserId { + if !channel.isDirectMessageChannel, channel.ownCapabilities.contains(.leaveChannel), let userId = chatClient.currentUserId { let leaveGroup = leaveGroup( for: channel, chatClient: chatClient, From 9d5fecce9d70858624ded05e21fbe827ff1e4967 Mon Sep 17 00:00:00 2001 From: Ben Pollman Date: Thu, 31 Oct 2024 09:41:51 +1100 Subject: [PATCH 2/6] Channel Actions - Update tests for leave button --- .../ChatChannelInfoViewModel_Tests.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift index 0c59a4e4b..10aab329e 100644 --- a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift +++ b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift @@ -222,10 +222,21 @@ class ChatChannelInfoViewModel_Tests: StreamChatTestCase { XCTAssert(leaveButton == true) } + func test_chatChannelInfoVM_leaveButtonHiddenInGroup() { + // Given + let channel = mockGroup(with: 5, updateCapabilities: false) + let viewModel = ChatChannelInfoViewModel(channel: channel) + + // When + let leaveButton = viewModel.shouldShowLeaveConversationButton + + // Then + XCTAssert(leaveButton == false) + } + func test_chatChannelInfoVM_leaveButtonShownInDM() { // Given - let channel = ChatChannel.mock( - cid: .unique, + let channel = ChatChannel.mockDMChannel( name: "Test", ownCapabilities: [.deleteChannel] ) @@ -262,6 +273,7 @@ class ChatChannelInfoViewModel_Tests: StreamChatTestCase { if updateCapabilities { capabilities.insert(.updateChannel) capabilities.insert(.deleteChannel) + capabilities.insert(.leaveChannel) } let channel = ChatChannel.mock( cid: cid, From c941df33165d2248db60a5a82bdab3b9542ce8f8 Mon Sep 17 00:00:00 2001 From: Ben Pollman Date: Thu, 31 Oct 2024 09:42:46 +1100 Subject: [PATCH 3/6] Update Package to point to PR branch with Mock update --- StreamChatSwiftUI.xcodeproj/project.pbxproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/StreamChatSwiftUI.xcodeproj/project.pbxproj b/StreamChatSwiftUI.xcodeproj/project.pbxproj index a8307d909..97e942798 100644 --- a/StreamChatSwiftUI.xcodeproj/project.pbxproj +++ b/StreamChatSwiftUI.xcodeproj/project.pbxproj @@ -3818,10 +3818,10 @@ }; 84E95A75284A486600699FD3 /* XCRemoteSwiftPackageReference "stream-chat-swift" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/GetStream/stream-chat-swift.git"; + repositoryURL = "https://github.com/bpollman/stream-chat-swift.git"; requirement = { - kind = upToNextMajorVersion; - minimumVersion = 4.65.0; + branch = "update-dm-mocks"; + kind = branch; }; }; E3A1C01A282BAC66002D1E26 /* XCRemoteSwiftPackageReference "sentry-cocoa" */ = { From f998a7f001e8936f93c1c745670fac2289f3068e Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 31 Oct 2024 12:29:13 +0000 Subject: [PATCH 4/6] Fix unit tests --- StreamChatSwiftUI.xcodeproj/project.pbxproj | 2 +- .../ChannelInfo/ChatChannelInfoViewModel_Tests.swift | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/StreamChatSwiftUI.xcodeproj/project.pbxproj b/StreamChatSwiftUI.xcodeproj/project.pbxproj index 97e942798..f4be98d11 100644 --- a/StreamChatSwiftUI.xcodeproj/project.pbxproj +++ b/StreamChatSwiftUI.xcodeproj/project.pbxproj @@ -3820,7 +3820,7 @@ isa = XCRemoteSwiftPackageReference; repositoryURL = "https://github.com/bpollman/stream-chat-swift.git"; requirement = { - branch = "update-dm-mocks"; + branch = develop; kind = branch; }; }; diff --git a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift index 10aab329e..f11ea200e 100644 --- a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift +++ b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift @@ -236,8 +236,9 @@ class ChatChannelInfoViewModel_Tests: StreamChatTestCase { func test_chatChannelInfoVM_leaveButtonShownInDM() { // Given - let channel = ChatChannel.mockDMChannel( - name: "Test", + let cidDM = ChannelId(type: .messaging, id: "!members" + .newUniqueId) + let channel = ChatChannel.mock( + cid: cidDM, ownCapabilities: [.deleteChannel] ) let viewModel = ChatChannelInfoViewModel(channel: channel) From 9b6d615c13976ffa12519278e2db749d7de74293 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 31 Oct 2024 12:42:48 +0000 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5f00b728..bc07d6004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Add support for Channel Search in the Channel List [#628](https://github.com/GetStream/stream-chat-swiftui/pull/628) ### 🐞 Fixed - Fix crash when opening message overlay in iPad with a TabBar [#627](https://github.com/GetStream/stream-chat-swiftui/pull/627) +- Only show Leave Group option if the user has leave-channel permission [#633](https://github.com/GetStream/stream-chat-swiftui/pull/633) # [4.65.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.65.0) _October 18, 2024_ From 620032372ec389188780438c9357a95f59f62461 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 31 Oct 2024 13:22:12 +0000 Subject: [PATCH 6/6] Fix tests --- .../ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift index ea3661a9c..cd581ac97 100644 --- a/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift +++ b/StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift @@ -103,7 +103,7 @@ class ChatChannelInfoView_Tests: StreamChatTestCase { let group = ChatChannel.mock( cid: .unique, name: "Test Group", - ownCapabilities: [.deleteChannel, .updateChannel], + ownCapabilities: [.leaveChannel, .updateChannel], lastActiveMembers: members, memberCount: members.count ) @@ -151,7 +151,7 @@ class ChatChannelInfoView_Tests: StreamChatTestCase { let group = ChatChannel.mock( cid: .unique, name: "Test Group", - ownCapabilities: [.deleteChannel, .updateChannel], + ownCapabilities: [.updateChannel, .leaveChannel], lastActiveMembers: members, memberCount: members.count )