Skip to content

Commit

Permalink
[AND-170] Intercept Members and add them to ChannelState (#5517)
Browse files Browse the repository at this point in the history
* Intercept Members and add them to ChannelState

* Add Tests

* Update CHANGELOG.md
  • Loading branch information
JcMinarro authored Dec 13, 2024
1 parent 8041197 commit 6f5e7e1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
### 🐞 Fixed

### ⬆️ Improved
- The `ChannelState` is updated with new members after querying member with `ChatClient::queryMembers`. [#5517](https://github.com/GetStream/stream-chat-android/pull/5517)

### ✅ Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.getstream.chat.android.client.plugin.listeners.HideChannelListener
import io.getstream.chat.android.client.plugin.listeners.MarkAllReadListener
import io.getstream.chat.android.client.plugin.listeners.QueryChannelListener
import io.getstream.chat.android.client.plugin.listeners.QueryChannelsListener
import io.getstream.chat.android.client.plugin.listeners.QueryMembersListener
import io.getstream.chat.android.client.plugin.listeners.QueryThreadsListener
import io.getstream.chat.android.client.plugin.listeners.SendAttachmentListener
import io.getstream.chat.android.client.plugin.listeners.SendGiphyListener
Expand All @@ -53,6 +54,7 @@ import io.getstream.chat.android.state.plugin.listener.internal.HideChannelListe
import io.getstream.chat.android.state.plugin.listener.internal.MarkAllReadListenerState
import io.getstream.chat.android.state.plugin.listener.internal.QueryChannelListenerState
import io.getstream.chat.android.state.plugin.listener.internal.QueryChannelsListenerState
import io.getstream.chat.android.state.plugin.listener.internal.QueryMembersListenerState
import io.getstream.chat.android.state.plugin.listener.internal.QueryThreadsListenerState
import io.getstream.chat.android.state.plugin.listener.internal.SendAttachmentListenerState
import io.getstream.chat.android.state.plugin.listener.internal.SendGiphyListenerState
Expand Down Expand Up @@ -96,6 +98,7 @@ public class StatePlugin internal constructor(
private val queryingChannelsFree: MutableStateFlow<Boolean>,
private val statePluginConfig: StatePluginConfig,
) : Plugin,
QueryMembersListener by QueryMembersListenerState(logic),
QueryChannelsListener by QueryChannelsListenerState(logic, queryingChannelsFree),
QueryChannelListener by QueryChannelListenerState(logic),
ThreadQueryListener by ThreadQueryListenerState(logic, repositoryFacade),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.chat.android.state.plugin.listener.internal

import io.getstream.chat.android.client.plugin.listeners.QueryMembersListener
import io.getstream.chat.android.models.FilterObject
import io.getstream.chat.android.models.Member
import io.getstream.chat.android.models.querysort.QuerySorter
import io.getstream.chat.android.state.plugin.logic.internal.LogicRegistry
import io.getstream.result.Result

internal class QueryMembersListenerState(private val logic: LogicRegistry) : QueryMembersListener {
override suspend fun onQueryMembersResult(
result: Result<List<Member>>,
channelType: String,
channelId: String,
offset: Int,
limit: Int,
filter: FilterObject,
sort: QuerySorter<Member>,
members: List<Member>,
) {
result.onSuccess { logic.channelState(channelType, channelId).upsertMembers(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.chat.android.state.plugin.listener.internal

import io.getstream.chat.android.randomInt
import io.getstream.chat.android.randomMembers
import io.getstream.chat.android.randomString
import io.getstream.chat.android.state.plugin.logic.channel.internal.ChannelStateLogic
import io.getstream.chat.android.state.plugin.logic.internal.LogicRegistry
import io.getstream.result.Result
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify

internal class QueryMembersListenerStateTest {
private val channelType = randomString()
private val channelId = randomString()
private val channelStateLogic: ChannelStateLogic = mock()
private val logicRegistry: LogicRegistry = mock {
on(
it.channelState(
channelType = eq(channelType),
channelId = eq(channelId),
),
) doReturn channelStateLogic
}
private val queryMembersListenerState = QueryMembersListenerState(logicRegistry)

@Test
fun `when querying members, should call channelStateLogic upsertMembers`() = runTest {
val members = randomMembers()

queryMembersListenerState.onQueryMembersResult(
result = Result.Success(members),
channelType = channelType,
channelId = channelId,
offset = randomInt(),
limit = randomInt(),
filter = mock(),
sort = mock(),
members = randomMembers(),
)

verify(channelStateLogic).upsertMembers(members)
}

@Test
fun `when querying members fails, should not call channelStateLogic upsertMembers`() = runTest {
queryMembersListenerState.onQueryMembersResult(
result = Result.Failure(mock()),
channelType = channelType,
channelId = channelId,
offset = randomInt(),
limit = randomInt(),
filter = mock(),
sort = mock(),
members = randomMembers(),
)

verify(channelStateLogic, never()).upsertMembers(any())
}
}

0 comments on commit 6f5e7e1

Please sign in to comment.