-
Notifications
You must be signed in to change notification settings - Fork 317
client: Count thread replies toward the slow mode cooldown #6505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andremion
wants to merge
2
commits into
develop
Choose a base branch
from
feature/and-1230-thread-replies-composer-cooldown
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...m/chat/android/client/internal/state/plugin/state/channel/internal/LastSentMessageDate.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.internal.state.plugin.state.channel.internal | ||
|
|
||
| import io.getstream.chat.android.models.Message | ||
| import java.util.Date | ||
|
|
||
| /** | ||
| * Returns the more recent of two nullable dates, or `null` when both are `null`. | ||
| */ | ||
| internal fun latestOf(first: Date?, second: Date?): Date? = when { | ||
| first == null -> second | ||
| second == null -> first | ||
| else -> maxOf(first, second) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the creation date of this message when it is a thread-only reply sent by [currentUserId], | ||
| * or `null` otherwise. | ||
| * | ||
| * The channel cooldown counts thread replies the same as channel messages, but thread-only replies | ||
| * (`parentId != null && !showInChannel`) are excluded from the channel message list, so the cooldown | ||
| * derivation tracks them separately. Shadowed messages are excluded to match the channel message | ||
| * date derivation. | ||
| */ | ||
| internal fun Message.ownThreadReplyDate(currentUserId: String): Date? = when { | ||
| user.id != currentUserId -> null | ||
| parentId == null || showInChannel -> null | ||
| shadowed -> null | ||
| else -> createdLocallyAt ?: createdAt | ||
| } | ||
|
|
||
| /** | ||
| * Returns the creation date of the most recent thread-only reply sent by [currentUserId] in this | ||
| * collection, or `null` when there is none. | ||
| */ | ||
| internal fun Collection<Message>.latestOwnThreadReplyDate(currentUserId: String?): Date? { | ||
| currentUserId ?: return null | ||
| return asSequence() | ||
| .mapNotNull { it.ownThreadReplyDate(currentUserId) } | ||
| .maxOrNull() | ||
| } |
109 changes: 109 additions & 0 deletions
109
...t/internal/state/plugin/state/channel/internal/ChannelStateImplLastSentMessageDateTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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.internal.state.plugin.state.channel.internal | ||
|
|
||
| import io.getstream.chat.android.randomUser | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertNotNull | ||
| import org.junit.jupiter.api.Assertions.assertNull | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class ChannelStateImplLastSentMessageDateTest : ChannelStateImplTestBase() { | ||
|
|
||
| @Test | ||
| fun `a thread-only reply by the current user updates lastSentMessageDate`() = runTest { | ||
| val threadReply = createMessage(1, parentId = "parent1", showInChannel = false) | ||
|
|
||
| channelState.upsertMessage(threadReply) | ||
|
|
||
| // the reply is excluded from the visible message list, but still drives the cooldown | ||
| assertTrue(channelState.messages.value.isEmpty()) | ||
| assertEquals(threadReply.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a thread-only reply via upsertMessages updates lastSentMessageDate`() = runTest { | ||
| val threadReply = createMessage(1, parentId = "parent1", showInChannel = false) | ||
|
|
||
| channelState.upsertMessages(listOf(threadReply)) | ||
|
|
||
| assertEquals(threadReply.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a thread-only reply from another user does not update lastSentMessageDate`() = runTest { | ||
| val otherUserReply = createMessage(1, user = randomUser(), parentId = "parent1", showInChannel = false) | ||
|
|
||
| channelState.upsertMessage(otherUserReply) | ||
|
|
||
| assertNull(channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a channel message by the current user still updates lastSentMessageDate`() = runTest { | ||
| val channelMessage = createMessage(1) | ||
|
|
||
| channelState.upsertMessage(channelMessage) | ||
|
|
||
| assertEquals(channelMessage.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `lastSentMessageDate is the later of the channel message and the thread reply`() = runTest { | ||
| val olderThreadReply = createMessage(1, parentId = "parent1", showInChannel = false) | ||
| val newerChannelMessage = createMessage(5) | ||
|
|
||
| channelState.upsertMessage(olderThreadReply) | ||
| channelState.upsertMessage(newerChannelMessage) | ||
|
|
||
| assertEquals(newerChannelMessage.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a newer thread reply wins over an older channel message`() = runTest { | ||
| val olderChannelMessage = createMessage(1) | ||
| val newerThreadReply = createMessage(5, parentId = "parent1", showInChannel = false) | ||
|
|
||
| channelState.upsertMessage(olderChannelMessage) | ||
| channelState.upsertMessage(newerThreadReply) | ||
|
|
||
| assertEquals(newerThreadReply.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `the thread reply date survives a later refresh that omits it`() = runTest { | ||
| val threadReply = createMessage(5, parentId = "parent1", showInChannel = false) | ||
| channelState.upsertMessage(threadReply) | ||
|
|
||
| // a server refresh replaces the message list with channel messages only (no thread replies) | ||
| channelState.setMessages(createMessages(count = 2, startIndex = 1)) | ||
|
|
||
| assertEquals(threadReply.createdAt, channelState.lastSentMessageDate.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `destroy clears the thread-reply contribution to lastSentMessageDate`() = runTest { | ||
| channelState.upsertMessage(createMessage(1, parentId = "parent1", showInChannel = false)) | ||
| assertNotNull(channelState.lastSentMessageDate.value) | ||
|
|
||
| channelState.destroy() | ||
|
|
||
| assertNull(channelState.lastSentMessageDate.value) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.