Skip to content
Open
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 @@ -25,6 +25,8 @@ import java.util.Date
data class ChatMessage(
var isGrouped: Boolean = false,

var isGroupedWithNext: Boolean = false,

var isOneToOneConversation: Boolean = false,

var isFormerOneToOneConversation: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ data class ChatMessageUi(
val reactions: List<MessageReactionUi> = emptyList(),
val isEdited: Boolean = false,
val parentMessage: ChatMessageUi? = null,
val replyable: Boolean = false
val replyable: Boolean = false,
val isGrouped: Boolean = false,
val isGroupedWithNext: Boolean = false
)

data class MessageReactionUi(val emoji: String, val amount: Int, val isSelfReaction: Boolean)
Expand Down Expand Up @@ -125,7 +127,9 @@ fun ChatMessage.toUiModel(
lastCommonReadMessageId = 0,
parentMessage = null
),
replyable = replyable
replyable = replyable,
isGrouped = isGrouped,
isGroupedWithNext = isGroupedWithNext
)

private fun ChatMessage.normalizeMessageParameters(): Map<String, Map<String, String>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ import kotlinx.coroutines.withTimeoutOrNull
import retrofit2.HttpException
import java.io.File
import java.io.IOException
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import javax.inject.Inject

@Suppress("TooManyFunctions", "LongParameterList")
Expand Down Expand Up @@ -649,6 +651,7 @@ class ChatViewModel @AssistedInject constructor(
.distinct()

val user = currentUserFlow.value
applyMessageGrouping(messages)
val uiMessages = messages.map { message ->
val parent: ChatMessage? = combinedMap[message.parentMessageId]
message.toUiModel(
Expand Down Expand Up @@ -716,6 +719,34 @@ class ChatViewModel @AssistedInject constructor(
}.asReversed()
}

private fun applyMessageGrouping(messages: List<ChatMessage>) {
messages.forEachIndexed { index, message ->
message.isGrouped = index > 0 && shouldGroupMessage(message, messages[index - 1])
message.isGroupedWithNext = index < messages.size - 1 && shouldGroupMessage(messages[index + 1], message)
}
}

private fun shouldGroupMessage(current: ChatMessage, previous: ChatMessage): Boolean {
val sameMessageKind = current.isSystemMessage == previous.isSystemMessage
val notUnclassifiedBot = current.actorType != "bots" || current.actorId == "changelog"
val sameActor = current.isSystemMessage ||
(current.actorType == previous.actorType && current.actorId == previous.actorId)
val currentDate = Instant.ofEpochMilli(current.timestamp * TIMESTAMP_TO_MILLIS)
.atZone(ZoneId.systemDefault()).toLocalDate()
val previousDate = Instant.ofEpochMilli(previous.timestamp * TIMESTAMP_TO_MILLIS)
.atZone(ZoneId.systemDefault()).toLocalDate()
val timeDifference = kotlin.math.abs(current.timestamp - previous.timestamp)
val neitherEdited = (current.lastEditTimestamp ?: 0L) == 0L || (previous.lastEditTimestamp ?: 0L) == 0L

return sameMessageKind &&
notUnclassifiedBot &&
sameActor &&
currentDate == previousDate &&
current.actorId == previous.actorId &&
timeDifference <= GROUPING_TIME_WINDOW_SECONDS &&
neitherEdited
}

fun onMessageSent() {
oneOrMoreMessagesWereSent = true
}
Expand Down Expand Up @@ -1775,6 +1806,8 @@ class ChatViewModel @AssistedInject constructor(
private const val WEBSOCKET_CONNECT_TIMEOUT_MS = 3000L
private const val WEBSOCKET_POLL_INTERVAL_MS = 50L
private const val ROOM_REFRESH_DEBOUNCE_MS = 500L
private const val GROUPING_TIME_WINDOW_SECONDS = 300L
private const val TIMESTAMP_TO_MILLIS = 1000L
}

sealed class OutOfOfficeUIState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,22 @@ internal fun createLongBaseMessage(content: MessageTypeContent?): ChatMessageUi
content = content,
reactions = previewReactions
)

internal fun createMarkdownMessage(): ChatMessageUi =
ChatMessageUi(
id = 4,
message = "- Item 1\n- Item 2\n\n> A blockquote\n\n```kotlin\nval x = 1\n```",
plainMessage = "- Item 1\n- Item 2\n\n> A blockquote\n\n```kotlin\nval x = 1\n```",
renderMarkdown = true,
actorDisplayName = "Markdown Sender",
isThread = false,
threadTitle = "",
threadReplies = 0,
incoming = true,
isDeleted = false,
avatarUrl = null,
statusIcon = MessageStatusIcon.SENT,
timestamp = System.currentTimeMillis() / 1000,
date = LocalDate.now(),
content = MessageTypeContent.RegularText
)
Loading
Loading