Skip to content

Commit b1a685a

Browse files
Only show "Add Users" button in ChannelInfoView if user has permissions (#651)
* Channel Info: Add Check for `update-channel-members` permission before showing "Add Users" button (#650) * Update CHANGELOG.md * Fix swiftformat * Fix test on channel info view --------- Co-authored-by: Ben Pollman <[email protected]>
1 parent 7fef385 commit b1a685a

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
### 🔄 Changed
77
- Message composer now uses `.uploadFile` capability when showing attachment picker icon [#646](https://github.com/GetStream/stream-chat-swiftui/pull/646)
8+
- `ChannelInfoView` now uses `.updateChannelMembers` capability to show "Add Users" button [#651](https://github.com/GetStream/stream-chat-swiftui/pull/651)
89

910
# [4.66.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.66.0)
1011
_November 06, 2024_

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public struct ChatChannelInfoView: View, KeyboardReadable {
137137
}
138138

139139
ToolbarItem(placement: .navigationBarTrailing) {
140-
viewModel.channel.isDirectMessageChannel ? nil :
140+
if viewModel.shouldShowAddUserButton {
141141
Button {
142142
viewModel.addUsersShown = true
143143
} label: {
@@ -148,6 +148,7 @@ public struct ChatChannelInfoView: View, KeyboardReadable {
148148
.background(colors.tintColor)
149149
.clipShape(Circle())
150150
}
151+
}
151152
}
152153
}
153154
.onReceive(keyboardWillChangePublisher) { visible in

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoViewModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public class ChatChannelInfoViewModel: ObservableObject, ChatChannelControllerDe
4848
channel.ownCapabilities.contains(.updateChannel)
4949
}
5050

51+
public var shouldShowAddUserButton: Bool {
52+
if channel.isDirectMessageChannel {
53+
return false
54+
} else {
55+
return channel.ownCapabilities.contains(.updateChannelMembers)
56+
}
57+
}
58+
5159
var channelController: ChatChannelController!
5260
private var memberListController: ChatChannelMemberListController!
5361
private var loadingUsers = false

StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoViewModel_Tests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,42 @@ class ChatChannelInfoViewModel_Tests: StreamChatTestCase {
262262
XCTAssert(leaveButton == false)
263263
}
264264

265+
func test_chatChannelInfoVM_addUserButtonShownInGroup() {
266+
// Given
267+
let channel = mockGroup(with: 5)
268+
let viewModel = ChatChannelInfoViewModel(channel: channel)
269+
270+
// When
271+
let leaveButton = viewModel.shouldShowAddUserButton
272+
273+
// Then
274+
XCTAssert(leaveButton == true)
275+
}
276+
277+
func test_chatChannelInfoVM_addUserButtonHiddenInGroup() {
278+
// Given
279+
let channel = mockGroup(with: 5, updateCapabilities: false)
280+
let viewModel = ChatChannelInfoViewModel(channel: channel)
281+
282+
// When
283+
let leaveButton = viewModel.shouldShowAddUserButton
284+
285+
// Then
286+
XCTAssert(leaveButton == false)
287+
}
288+
289+
func test_chatChannelInfoVM_addUserButtonHiddenInDM() {
290+
// Given
291+
let channel = ChatChannel.mockDMChannel()
292+
let viewModel = ChatChannelInfoViewModel(channel: channel)
293+
294+
// When
295+
let leaveButton = viewModel.shouldShowAddUserButton
296+
297+
// Then
298+
XCTAssert(leaveButton == false)
299+
}
300+
265301
// MARK: - private
266302

267303
private func mockGroup(with memberCount: Int, updateCapabilities: Bool = true) -> ChatChannel {
@@ -275,6 +311,7 @@ class ChatChannelInfoViewModel_Tests: StreamChatTestCase {
275311
capabilities.insert(.updateChannel)
276312
capabilities.insert(.deleteChannel)
277313
capabilities.insert(.leaveChannel)
314+
capabilities.insert(.updateChannelMembers)
278315
}
279316
let channel = ChatChannel.mock(
280317
cid: cid,

StreamChatSwiftUITests/Tests/ChatChannel/ChannelInfo/ChatChannelInfoView_Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ChatChannelInfoView_Tests: StreamChatTestCase {
151151
let group = ChatChannel.mock(
152152
cid: .unique,
153153
name: "Test Group",
154-
ownCapabilities: [.updateChannel, .leaveChannel],
154+
ownCapabilities: [.updateChannel, .leaveChannel, .updateChannelMembers],
155155
lastActiveMembers: members,
156156
memberCount: members.count
157157
)

0 commit comments

Comments
 (0)