Skip to content

Commit

Permalink
[AND-203] Fix system messages not hidden if showSystemMessages=false. (
Browse files Browse the repository at this point in the history
…#5546)

* [AND-203] Fix system messages not hidden if showSystemMessages=false.

* [AND-203] Update CHANGELOG.md.

* [AND-203] Simplify message filtering logic.

---------

Co-authored-by: PetarVelikov <[email protected]>
  • Loading branch information
VelikovPetar and PetarVelikov authored Jan 7, 2025
1 parent 99dd6be commit 02d32d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

## stream-chat-android-ui-common
### 🐞 Fixed
- Fix `MessageListController` not respecting the `showSystemMessages` property when set to `false`. [#5546](https://github.com/GetStream/stream-chat-android/pull/5546)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,18 +981,19 @@ public class MessageListController(
): List<Message> {
val currentUser = user.value

return messages.filter {
val shouldNotShowIfDeleted = when (deletedMessageVisibility) {
DeletedMessageVisibility.ALWAYS_VISIBLE -> true
DeletedMessageVisibility.VISIBLE_FOR_CURRENT_USER -> {
!(it.isDeleted() && it.user.id != currentUser?.id)
return messages.filter { message ->
val isDeletedMessage = message.isDeleted()
val isSystemMessage = message.isSystem() || message.isError()

when {
isDeletedMessage -> when (deletedMessageVisibility) {
DeletedMessageVisibility.ALWAYS_VISIBLE -> true
DeletedMessageVisibility.VISIBLE_FOR_CURRENT_USER -> message.user.id == currentUser?.id
DeletedMessageVisibility.ALWAYS_HIDDEN -> false
}

DeletedMessageVisibility.ALWAYS_HIDDEN -> !it.isDeleted()
isSystemMessage -> showSystemMessages
else -> true
}
val isSystemMessage = it.isSystem() || it.isError()

shouldNotShowIfDeleted || (isSystemMessage && showSystemMessages)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import io.getstream.chat.android.ui.common.state.messages.list.MessageItemState
import io.getstream.chat.android.ui.common.state.messages.list.MessageListState
import io.getstream.chat.android.ui.common.state.messages.list.MessagePosition
import io.getstream.chat.android.ui.common.state.messages.list.Other
import io.getstream.chat.android.ui.common.state.messages.list.SystemMessageItemState
import io.getstream.chat.android.ui.common.state.messages.list.TypingItemState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -608,6 +609,32 @@ internal class MessageListControllerTests {
controller.messageListState.value.newMessageState `should be equal to` Other(newMessage.createdAt?.time)
}

@Test
fun `When showSystemMessages is true, Then system messages should be shown`() = runTest {
val messages = listOf(randomMessage(user = user1, type = MessageType.SYSTEM, deletedAt = null))
val messagesState = MutableStateFlow(messages)
val controller = Fixture()
.givenCurrentUser()
.givenChannelState(messagesState = messagesState)
.get(dateSeparatorHandler = { _, _ -> false }, showSystemMessages = true)

val expectedMessageItems = messages.map(::SystemMessageItemState)
controller.messageListState.value.messageItems `should be equal to` expectedMessageItems
}

@Test
fun `When showSystemMessages is false, Then system messages should be hidden`() = runTest {
val messages = listOf(randomMessage(user = user1, type = MessageType.SYSTEM, deletedAt = null))
val messagesState = MutableStateFlow(messages)
val controller = Fixture()
.givenCurrentUser()
.givenChannelState(messagesState = messagesState)
.get(dateSeparatorHandler = { _, _ -> false }, showSystemMessages = false)

val expectedMessageItems = emptyList<MessageItemState>()
controller.messageListState.value.messageItems `should be equal to` expectedMessageItems
}

private class Fixture(
private val chatClient: ChatClient = mock(),
private val cid: String = CID,
Expand Down Expand Up @@ -693,13 +720,15 @@ internal class MessageListControllerTests {
fun get(
dateSeparatorHandler: DateSeparatorHandler = DateSeparatorHandler.getDefaultDateSeparatorHandler(),
deletedMessageVisibility: DeletedMessageVisibility = DeletedMessageVisibility.ALWAYS_VISIBLE,
showSystemMessages: Boolean = true,
): MessageListController {
return MessageListController(
cid = cid,
chatClient = chatClient,
clipboardHandler = mock(),
dateSeparatorHandler = dateSeparatorHandler,
deletedMessageVisibility = deletedMessageVisibility,
showSystemMessages = showSystemMessages,
threadLoadOrderOlderToNewer = false,
channelState = MutableStateFlow(channelState),
)
Expand Down

0 comments on commit 02d32d8

Please sign in to comment.