Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import io.getstream.chat.android.client.api2.optimisation.hash.GetPinnedMessages
import io.getstream.chat.android.client.api2.optimisation.hash.GetReactionsHash
import io.getstream.chat.android.client.api2.optimisation.hash.GetRepliesHash
import io.getstream.chat.android.client.api2.optimisation.hash.QueryBanedUsersHash
import io.getstream.chat.android.client.api2.optimisation.hash.QueryGroupedChannelsHash
import io.getstream.chat.android.client.api2.optimisation.hash.QueryMembersHash
import io.getstream.chat.android.models.BannedUser
import io.getstream.chat.android.models.BannedUsersSort
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.FilterObject
import io.getstream.chat.android.models.GroupedChannels
import io.getstream.chat.android.models.GroupedChannelsGroupQuery
import io.getstream.chat.android.models.Member
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.PendingMessage
Expand Down Expand Up @@ -142,6 +145,22 @@ internal class DistinctChatApi(
}
}

override fun queryGroupedChannels(
limit: Int?,
groups: Map<String, GroupedChannelsGroupQuery>?,
watch: Boolean,
presence: Boolean,
): Call<GroupedChannels> {
val uniqueKey = QueryGroupedChannelsHash(limit, groups, watch, presence).hashCode()
StreamLog.d(TAG) {
"[queryGroupedChannels] limit: $limit, groups: $groups, watch: $watch, " +
"presence: $presence, uniqueKey: $uniqueKey"
}
return getOrCreate(uniqueKey) {
Comment thread
gpunto marked this conversation as resolved.
delegate.queryGroupedChannels(limit, groups, watch, presence)
}
}

override fun queryBannedUsers(
filter: FilterObject,
sort: QuerySorter<BannedUsersSort>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import io.getstream.chat.android.models.BannedUser
import io.getstream.chat.android.models.BannedUsersSort
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.FilterObject
import io.getstream.chat.android.models.GroupedChannels
import io.getstream.chat.android.models.GroupedChannelsGroupQuery
import io.getstream.chat.android.models.Member
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.PendingMessage
Expand Down Expand Up @@ -82,6 +84,15 @@ internal class DistinctChatApiEnabler(
return getApi().queryChannels(query)
}

override fun queryGroupedChannels(
limit: Int?,
groups: Map<String, GroupedChannelsGroupQuery>?,
watch: Boolean,
presence: Boolean,
): Call<GroupedChannels> {
return getApi().queryGroupedChannels(limit, groups, watch, presence)
}

override fun queryBannedUsers(
filter: FilterObject,
sort: QuerySorter<BannedUsersSort>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2014-2026 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.client.api2.optimisation.hash

import io.getstream.chat.android.models.GroupedChannelsGroupQuery

internal data class QueryGroupedChannelsHash(
val limit: Int?,
val groups: Map<String, GroupedChannelsGroupQuery>?,
val watch: Boolean,
val presence: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import io.getstream.chat.android.randomString
import kotlinx.coroutines.test.TestScope
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.times
Expand Down Expand Up @@ -98,6 +99,7 @@ internal class DistinctChatApiEnablerTest {
members = members,
)
enabler.queryChannel(channelType, channelId, channelRequest)
enabler.queryGroupedChannels(limit, null, watch = true, presence = false)
// then
verify(distinctApi, times(1)).getRepliesMore(messageId, firstId, limit)
verify(distinctApi, times(1)).getReplies(messageId, limit)
Expand Down Expand Up @@ -133,6 +135,7 @@ internal class DistinctChatApiEnablerTest {
members = members,
)
verify(distinctApi, times(1)).queryChannel(channelType, channelId, channelRequest)
verify(distinctApi, times(1)).queryGroupedChannels(limit, null, watch = true, presence = false)
verifyNoInteractions(api)
}

Expand Down Expand Up @@ -173,6 +176,7 @@ internal class DistinctChatApiEnablerTest {
members = members,
)
enabler.queryChannel(channelType, channelId, channelRequest)
enabler.queryGroupedChannels(limit, null, watch = true, presence = false)

// then
verify(api, times(1)).getRepliesMore(messageId, firstId, limit)
Expand Down Expand Up @@ -203,6 +207,7 @@ internal class DistinctChatApiEnablerTest {
members = members,
)
verify(api, times(1)).queryChannel(channelType, channelId, channelRequest)
verify(api, times(1)).queryGroupedChannels(limit, null, watch = true, presence = false)
verify(distinctApi, times(0)).getRepliesMore(any(), any(), any())
verify(distinctApi, times(0)).getReplies(any(), any())
verify(distinctApi, times(0)).getNewerReplies(any(), any(), any())
Expand All @@ -214,5 +219,6 @@ internal class DistinctChatApiEnablerTest {
verify(distinctApi, times(0)).queryBannedUsers(any(), any(), any(), any(), any(), any(), any(), any())
verify(distinctApi, times(0)).queryMembers(any(), any(), any(), any(), any(), any(), any())
verify(distinctApi, times(0)).queryChannel(any(), any(), any())
verify(distinctApi, times(0)).queryGroupedChannels(anyOrNull(), anyOrNull(), any(), any())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.getstream.chat.android.randomMember
import io.getstream.chat.android.randomString
import io.getstream.chat.android.test.TestCall
import io.getstream.result.Result
import io.getstream.result.call.DistinctCall
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Assert
Expand Down Expand Up @@ -483,6 +484,47 @@ internal class DistinctChatApiTest {
Assert.assertFalse(call1 === call2)
}

@Test
fun `When calling queryGroupedChannels with same arguments, Then same instance of Call is returned`() {
// given
val distinctChatApi = DistinctChatApi(TestScope(), mock())
// when
val call1 = distinctChatApi.queryGroupedChannels(limit = 30, groups = null, watch = true, presence = false)
val call2 = distinctChatApi.queryGroupedChannels(limit = 30, groups = null, watch = true, presence = false)
// then
// verify the override wraps the call and reuses the same instance
Assert.assertTrue(call1 is DistinctCall)
Assert.assertTrue(call1 === call2)
}

@Test
fun `When calling queryGroupedChannels with same arguments and first call finishes, Then different instance of Call is returned`() =
runTest {
// given
val delegateApi = mock<ChatApi>()
whenever(delegateApi.queryGroupedChannels(any(), anyOrNull(), any(), any())).thenReturn(mock())
val distinctChatApi = DistinctChatApi(backgroundScope, delegateApi)
// when
val call1 = distinctChatApi.queryGroupedChannels(limit = 30, groups = null, watch = true, presence = false)
call1.await()
val call2 = distinctChatApi.queryGroupedChannels(limit = 30, groups = null, watch = true, presence = false)
// then
// verify different instance of call is returned
Assert.assertFalse(call1 === call2)
}

@Test
fun `When calling queryGroupedChannels with different arguments, Then different instance of Call is returned`() {
// given
val distinctChatApi = DistinctChatApi(TestScope(), mock())
// when
val call1 = distinctChatApi.queryGroupedChannels(limit = 30, groups = null, watch = true, presence = false)
val call2 = distinctChatApi.queryGroupedChannels(limit = 60, groups = null, watch = true, presence = false)
// then
// verify different instance of call is returned
Assert.assertFalse(call1 === call2)
}

@Test
fun `When calling queryBannedUsers with same arguments, Then same instance of Call is returned`() {
// given
Expand Down
Loading