Skip to content

Commit 4cce90f

Browse files
authored
Fix unsetProperties not having any effect in CurrentUserController.updateUserData() (#3650)
* Fix not using `unsetProperties` in `CurrentUserController.updateUserData()` * Do not provide default values in current user updater + use existing types in teamsRole * Update CHANGELOG.md * Use UserRole in the DemoApp * Update CHANGELOG.md
1 parent 14e8077 commit 4cce90f

File tree

9 files changed

+40
-25
lines changed

9 files changed

+40
-25
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
### 🐞 Fixed
88
- Fix `FilterKey.id` not returning any channels in `ChannelListQuery` [#3643](https://github.com/GetStream/stream-chat-swift/pull/3643)
99
- Fix incorrect channel list sorting when sorted by `.hasUnread` [#3646](https://github.com/GetStream/stream-chat-swift/pull/3646)
10+
- Fix `unsetProperties` not having any effect in `CurrentUserController.updateUserData()` [#3650](https://github.com/GetStream/stream-chat-swift/pull/3650)
11+
### 🔄 Changed
12+
- Change the `teamsRole` parameter from `[String: String]` to `[TeamId: UserRole]` [#3650](https://github.com/GetStream/stream-chat-swift/pull/3650)
1013

1114
## StreamChatUI
1215
### 🐞 Fixed

DemoApp/StreamChat/Components/DemoChatChannelListRouter.swift

+5-10
Original file line numberDiff line numberDiff line change
@@ -651,23 +651,18 @@ final class DemoChatChannelListRouter: ChatChannelListRouter {
651651
}
652652
}),
653653
.init(title: "Reset User Image", handler: { [unowned self] _ in
654-
do {
655-
let connectedUser = try self.rootViewController.controller.client.makeConnectedUser()
656-
Task {
657-
do {
658-
try await connectedUser.update(unset: ["image"])
659-
} catch {
654+
channelController.client.currentUserController()
655+
.updateUserData(unsetProperties: ["image"]) { [unowned self] error in
656+
if let error {
660657
self.rootViewController.presentAlert(title: error.localizedDescription)
661658
}
662659
}
663-
} catch {
664-
self.rootViewController.presentAlert(title: error.localizedDescription)
665-
}
666660
}),
667661
.init(title: "Add a team role for the current user", isEnabled: true, handler: { [unowned self] _ in
668662
self.rootViewController.presentAlert(title: "Enter the team role", textFieldPlaceholder: "Enter role") { role in
669663
if let role, !role.isEmpty {
670-
client.currentUserController().updateUserData(teamsRole: ["ios": role]) { error in
664+
let userRole = UserRole(rawValue: role)
665+
client.currentUserController().updateUserData(teamsRole: ["ios": userRole]) { error in
671666
if let error {
672667
log.error("Couldn't add role to custom team for the current user: \(error)")
673668
}

Sources/StreamChat/APIClient/Endpoints/Payloads/UserPayloads.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ struct UserUpdateRequestBody: Encodable {
173173
let privacySettings: UserPrivacySettingsPayload?
174174
let role: UserRole?
175175
let extraData: [String: RawJSON]?
176-
let teamsRole: [String: String]?
176+
let teamsRole: [TeamId: UserRole]?
177177

178178
init(
179179
name: String?,
180180
imageURL: URL?,
181181
privacySettings: UserPrivacySettingsPayload?,
182182
role: UserRole?,
183-
teamsRole: [String: String]?,
183+
teamsRole: [TeamId: UserRole]?,
184184
extraData: [String: RawJSON]?
185185
) {
186186
self.name = name

Sources/StreamChat/Controllers/CurrentUserController/CurrentUserController.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public extension CurrentChatUserController {
185185
///
186186
/// By default all data is `nil`, and it won't be updated unless a value is provided.
187187
///
188-
/// - Note: This operation does a partial user update which keeps existing data if not modified. Use ``unset`` for clearing the existing state.
188+
/// - Note: This operation does a partial user update which keeps existing data if not modified. Use ``unsetProperties`` for clearing the existing state.
189189
///
190190
/// - Parameters:
191191
/// - name: Optionally provide a new name to be updated.
@@ -194,14 +194,14 @@ public extension CurrentChatUserController {
194194
/// - role: The role for the user.
195195
/// - teamsRole: The role for the user in a specific team. Example: `["teamId": "role"]`.
196196
/// - userExtraData: Optionally provide new user extra data to be updated.
197-
/// - unset: Existing values for specified properties are removed. For example, `image` or `name`.
197+
/// - unsetProperties: Remove existing properties from the user. For example, `image` or `name`.
198198
/// - completion: Called when user is successfuly updated, or with error.
199199
func updateUserData(
200200
name: String? = nil,
201201
imageURL: URL? = nil,
202202
privacySettings: UserPrivacySettings? = nil,
203203
role: UserRole? = nil,
204-
teamsRole: [String: String]? = nil,
204+
teamsRole: [TeamId: UserRole]? = nil,
205205
userExtraData: [String: RawJSON] = [:],
206206
unsetProperties: Set<String> = [],
207207
completion: ((Error?) -> Void)? = nil
@@ -218,7 +218,8 @@ public extension CurrentChatUserController {
218218
privacySettings: privacySettings,
219219
role: role,
220220
teamsRole: teamsRole,
221-
userExtraData: userExtraData
221+
userExtraData: userExtraData,
222+
unset: unsetProperties
222223
) { error in
223224
self.callback {
224225
completion?(error)
@@ -232,7 +233,7 @@ public extension CurrentChatUserController {
232233
///
233234
/// - Parameters:
234235
/// - extraData: The additional data to be added to the member object.
235-
/// - unsetProperties: The custom properties to be removed from the member object.
236+
/// - unsetProperties: The properties to be removed from the member object.
236237
/// - channelId: The channel where the member data is updated.
237238
/// - completion: Returns the updated member object or an error if the update fails.
238239
func updateMemberData(

Sources/StreamChat/StateLayer/ConnectedUser.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class ConnectedUser {
5555
imageURL: URL? = nil,
5656
privacySettings: UserPrivacySettings? = nil,
5757
role: UserRole? = nil,
58-
teamRoles: [String: String]? = nil,
58+
teamRoles: [TeamId: UserRole]? = nil,
5959
extraData: [String: RawJSON] = [:],
6060
unset: Set<String> = []
6161
) async throws {

Sources/StreamChat/Workers/CurrentUserUpdater.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class CurrentUserUpdater: Worker {
2525
imageURL: URL?,
2626
privacySettings: UserPrivacySettings?,
2727
role: UserRole?,
28-
teamsRole: [String: String]? = nil,
28+
teamsRole: [TeamId: UserRole]?,
2929
userExtraData: [String: RawJSON]?,
30-
unset: Set<String> = [],
30+
unset: Set<String>,
3131
completion: ((Error?) -> Void)? = nil
3232
) {
3333
let params: [Any?] = [name, imageURL, userExtraData]
@@ -290,7 +290,7 @@ extension CurrentUserUpdater {
290290
imageURL: URL?,
291291
privacySettings: UserPrivacySettings?,
292292
role: UserRole?,
293-
teamsRole: [String: String]?,
293+
teamsRole: [TeamId: UserRole]?,
294294
userExtraData: [String: RawJSON]?,
295295
unset: Set<String>
296296
) async throws {

TestTools/StreamChatTestTools/Mocks/StreamChat/Workers/CurrentUserUpdater_Mock.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class CurrentUserUpdater_Mock: CurrentUserUpdater {
1313
@Atomic var updateUserData_userExtraData: [String: RawJSON]?
1414
@Atomic var updateUserData_privacySettings: UserPrivacySettings?
1515
@Atomic var updateUserData_unset: Set<String>?
16+
@Atomic var updateUserData_teamsRole: [TeamId: UserRole]?
1617
@Atomic var updateUserData_completion: ((Error?) -> Void)?
1718

1819
@Atomic var addDevice_id: DeviceId?
@@ -40,7 +41,7 @@ final class CurrentUserUpdater_Mock: CurrentUserUpdater {
4041
imageURL: URL?,
4142
privacySettings: UserPrivacySettings?,
4243
role: UserRole?,
43-
teamsRole: [String: String]?,
44+
teamsRole: [TeamId: UserRole]?,
4445
userExtraData: [String: RawJSON]?,
4546
unset: Set<String>,
4647
completion: ((Error?) -> Void)? = nil
@@ -51,6 +52,7 @@ final class CurrentUserUpdater_Mock: CurrentUserUpdater {
5152
updateUserData_userExtraData = userExtraData
5253
updateUserData_privacySettings = privacySettings
5354
updateUserData_unset = unset
55+
updateUserData_teamsRole = teamsRole
5456
updateUserData_completion = completion
5557
}
5658

Tests/StreamChatTests/Controllers/CurrentUserController/CurrentUserController_Tests.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,18 @@ final class CurrentUserController_Tests: XCTestCase {
299299
privacySettings: .init(
300300
typingIndicators: .init(enabled: true), readReceipts: .init(enabled: true)
301301
),
302-
userExtraData: [:]
302+
teamsRole: ["teamId": "role"],
303+
userExtraData: [:],
304+
unsetProperties: ["image"]
303305
)
304306

305307
// Assert udpater is called with correct data
306308
XCTAssertEqual(env.currentUserUpdater.updateUserData_name, expectedName)
307309
XCTAssertEqual(env.currentUserUpdater.updateUserData_imageURL, expectedImageUrl)
308310
XCTAssertEqual(env.currentUserUpdater.updateUserData_privacySettings?.typingIndicators?.enabled, true)
309311
XCTAssertEqual(env.currentUserUpdater.updateUserData_privacySettings?.readReceipts?.enabled, true)
312+
XCTAssertEqual(env.currentUserUpdater.updateUserData_unset, ["image"])
313+
XCTAssertEqual(env.currentUserUpdater.updateUserData_teamsRole, ["teamId": "role"])
310314
XCTAssertNotNil(env.currentUserUpdater.updateUserData_completion)
311315
}
312316

Tests/StreamChatTests/Workers/CurrentUserUpdater_Tests.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ final class CurrentUserUpdater_Tests: XCTestCase {
6969
role: expectedRole,
7070
teamsRole: ["ios": "guest"],
7171
userExtraData: nil,
72+
unset: ["image"],
7273
completion: { error in
7374
XCTAssertNil(error)
7475
}
@@ -104,7 +105,7 @@ final class CurrentUserUpdater_Tests: XCTestCase {
104105
teamsRole: ["ios": "guest"],
105106
extraData: [:]
106107
),
107-
unset: []
108+
unset: ["image"]
108109
)
109110
XCTAssertEqual(apiClient.request_endpoint, AnyEndpoint(expectedEndpoint))
110111
}
@@ -122,6 +123,7 @@ final class CurrentUserUpdater_Tests: XCTestCase {
122123
imageURL: nil,
123124
privacySettings: nil,
124125
role: nil,
126+
teamsRole: nil,
125127
userExtraData: nil,
126128
unset: ["image"],
127129
completion: { _ in }
@@ -167,7 +169,9 @@ final class CurrentUserUpdater_Tests: XCTestCase {
167169
readReceipts: .init(enabled: false)
168170
),
169171
role: expectedRole,
172+
teamsRole: nil,
170173
userExtraData: nil,
174+
unset: [],
171175
completion: { _ in
172176
completionCalled = true
173177
}
@@ -218,7 +222,9 @@ final class CurrentUserUpdater_Tests: XCTestCase {
218222
imageURL: nil,
219223
privacySettings: nil,
220224
role: nil,
221-
userExtraData: [:],
225+
teamsRole: nil,
226+
userExtraData: nil,
227+
unset: [],
222228
completion: { error in
223229
completionError = error
224230
}
@@ -251,7 +257,9 @@ final class CurrentUserUpdater_Tests: XCTestCase {
251257
imageURL: nil,
252258
privacySettings: nil,
253259
role: nil,
260+
teamsRole: nil,
254261
userExtraData: nil,
262+
unset: [],
255263
completion: $0
256264
)
257265
}
@@ -279,7 +287,9 @@ final class CurrentUserUpdater_Tests: XCTestCase {
279287
imageURL: nil,
280288
privacySettings: nil,
281289
role: nil,
290+
teamsRole: nil,
282291
userExtraData: nil,
292+
unset: [],
283293
completion: { error in
284294
completionError = error
285295
}

0 commit comments

Comments
 (0)