Skip to content

Commit

Permalink
[PBE-5799] Keep members updated on QueryChannelsState (#5382)
Browse files Browse the repository at this point in the history
* Keep members updated on QueryChannelsState

* Update CHANGELOG.md

---------

Co-authored-by: Kanat Kiialbaev <[email protected]>
  • Loading branch information
JcMinarro and kanat authored Aug 29, 2024
1 parent b0dde02 commit 7852d8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Fixed `ChannelState.watchers` not being updated when a watcher gets deleted. [#5378](https://github.com/GetStream/stream-chat-android/pull/5378)

### ⬆️ Improved
- Keep members updated on QueryChannelsState. [5382](https://github.com/GetStream/stream-chat-android/pull/5382)

### ✅ Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.getstream.log.taggedLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async

@Suppress("TooManyFunctions")
internal class QueryChannelsStateLogic(
private val mutableState: QueryChannelsMutableState,
private val stateRegistry: StateRegistry,
Expand Down Expand Up @@ -145,7 +146,10 @@ internal class QueryChannelsStateLogic(
val existingChannels = mutableState.rawChannels ?: emptyMap()
mutableState.setChannels(
existingChannels +
channels.map { it.cid to it.joinMessages(existingChannels[it.cid]) },
channels.map {
it.cid to it.joinMessages(existingChannels[it.cid])
.joinMembers(existingChannels[it.cid])
},
)
channels.map { channel ->
coroutineScope.async {
Expand All @@ -166,6 +170,24 @@ internal class QueryChannelsStateLogic(
.distinctBy { it.id },
)

/**
* The list of members is merged with the existing list of members but only used if it is smaller than the
* number of members in the channel.
*
* @param existingChannel Channel? The existing channel.
*
* @return Channel The channel with the members list updated.
*/
private fun Channel.joinMembers(existingChannel: Channel?): Channel =
existingChannel?.let { oldChannel ->
val members = (members.associateBy { it.getUserId() } + (oldChannel.members.associateBy { it.getUserId() }))
.takeUnless { it.size > memberCount }
?.values
?.toList()
?: members
copy(members = members)
} ?: this

/**
* Remove channels to state.
*/
Expand Down

0 comments on commit 7852d8d

Please sign in to comment.