Skip to content

Commit a3d82b5

Browse files
committed
Mark channel as delivered on query a single channel
1 parent 219b622 commit a3d82b5

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/plugin/MessageDeliveredPlugin.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.getstream.chat.android.client.plugin
1818

1919
import io.getstream.chat.android.client.ChatClient
20+
import io.getstream.chat.android.client.api.models.QueryChannelRequest
2021
import io.getstream.chat.android.client.api.models.QueryChannelsRequest
2122
import io.getstream.chat.android.client.plugin.factory.PluginFactory
2223
import io.getstream.chat.android.client.receipts.MessageReceiptManager
@@ -31,11 +32,25 @@ internal class MessageDeliveredPlugin(
3132
chatClient: ChatClient = ChatClient.instance(),
3233
private val messageReceiptManager: MessageReceiptManager = chatClient.messageReceiptManager,
3334
) : Plugin {
35+
3436
override suspend fun onQueryChannelsResult(result: Result<List<Channel>>, request: QueryChannelsRequest) {
3537
result.onSuccess { channels ->
3638
messageReceiptManager.markChannelsAsDelivered(channels)
3739
}
3840
}
41+
42+
override suspend fun onQueryChannelResult(
43+
result: Result<Channel>,
44+
channelType: String,
45+
channelId: String,
46+
request: QueryChannelRequest,
47+
) {
48+
result.onSuccess { channel ->
49+
if (request.pagination() == null) { // only mark as delivered on initial load
50+
messageReceiptManager.markChannelsAsDelivered(channels = listOf(channel))
51+
}
52+
}
53+
}
3954
}
4055

4156
internal object MessageDeliveredPluginFactory : PluginFactory {

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/plugin/MessageDeliveredPluginTest.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.getstream.result.Result
2323
import kotlinx.coroutines.test.runTest
2424
import org.junit.Test
2525
import org.mockito.kotlin.any
26+
import org.mockito.kotlin.doReturn
2627
import org.mockito.kotlin.mock
2728
import org.mockito.kotlin.never
2829
import org.mockito.kotlin.times
@@ -52,6 +53,55 @@ internal class MessageDeliveredPluginTest {
5253
fixture.verifyMarkChannelsAsDeliveredCalled(never())
5354
}
5455

56+
@Test
57+
fun `on query channel with successful result and null pagination, should mark channel as delivered`() = runTest {
58+
val channel = randomChannel()
59+
val fixture = Fixture()
60+
val sut = fixture.get()
61+
62+
sut.onQueryChannelResult(
63+
result = Result.Success(channel),
64+
channelType = channel.type,
65+
channelId = channel.id,
66+
request = mock { on { pagination() } doReturn null },
67+
)
68+
69+
fixture.verifyMarkChannelsAsDeliveredCalled(channels = listOf(channel))
70+
}
71+
72+
@Test
73+
fun `on query channel with successful result and non-null pagination, should not mark channel as delivered`() =
74+
runTest {
75+
val channel = randomChannel()
76+
val fixture = Fixture()
77+
val sut = fixture.get()
78+
79+
sut.onQueryChannelResult(
80+
result = Result.Success(channel),
81+
channelType = channel.type,
82+
channelId = channel.id,
83+
request = mock { on { pagination() } doReturn mock() },
84+
)
85+
86+
fixture.verifyMarkChannelsAsDeliveredCalled(never())
87+
}
88+
89+
@Test
90+
fun `on query channel with failure result, should not mark channel as delivered`() = runTest {
91+
val channel = randomChannel()
92+
val fixture = Fixture()
93+
val sut = fixture.get()
94+
95+
sut.onQueryChannelResult(
96+
result = Result.Failure(mock()),
97+
channelType = channel.type,
98+
channelId = channel.id,
99+
request = mock(),
100+
)
101+
102+
fixture.verifyMarkChannelsAsDeliveredCalled(never())
103+
}
104+
55105
private class Fixture {
56106
private val mockMessageReceiptManager = mock<MessageReceiptManager>()
57107

0 commit comments

Comments
 (0)