Skip to content

Commit

Permalink
Refactor unread counter
Browse files Browse the repository at this point in the history
  • Loading branch information
JcMinarro committed Feb 12, 2024
1 parent cb46691 commit 95c5b68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,37 @@ public val Channel.lastMessage: Message?

@InternalStreamChatApi
public fun Channel.updateLastMessage(
receivedEventDate: Date,
message: Message,
currentUserId: String,
): Channel {
val createdAt = message.createdAt ?: message.createdLocallyAt
val messageCreatedAt =
checkNotNull(createdAt) { "created at cant be null, be sure to set message.createdAt" }
checkNotNull(createdAt) { "created at cant be null, be sure to set message.createdAt" }

val newMessages = (
messages
.associateBy { it.id } + (message.id to message)
)
.values
.sortedBy { it.createdAt ?: it.createdLocallyAt }

val updateNeeded = message.id == lastMessage?.id
val newLastMessage = lastMessageAt == null || messageCreatedAt.after(lastMessageAt)
val newReads = read.map { read ->
read.takeUnless { it.user.id == currentUserId }
?: read.copy(
lastReceivedEventDate = messageCreatedAt,
unreadMessages = read.unreadMessages + 1,
lastReceivedEventDate = receivedEventDate,
unreadMessages = read.let {
val hasNewUnreadMessage = receivedEventDate.after(it.lastReceivedEventDate) &&
newMessages.size > messages.size &&
newMessages.last().id == message.id
if (hasNewUnreadMessage) it.unreadMessages.inc() else it.unreadMessages
},
)
}
return this.takeUnless { updateNeeded || newLastMessage }
?: copy(
lastMessageAt = messageCreatedAt,
messages = messages + message,
read = newReads,
).syncUnreadCountWithReads()
return this.copy(
lastMessageAt = newMessages.last().let { it.createdAt ?: it.createdLocallyAt },
messages = newMessages,
read = newReads,
).syncUnreadCountWithReads()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.getstream.chat.android.models.User
import io.getstream.chat.android.state.plugin.state.global.GlobalState
import io.getstream.log.StreamLog
import io.getstream.log.taggedLogger
import java.util.Date

/**
* EventBatchUpdate helps you efficiently implement a 4 step batch update process
Expand Down Expand Up @@ -64,10 +65,10 @@ internal class EventBatchUpdate private constructor(
* Adds the message and updates the last message for the given channel.
* Increments the unread count if the right conditions apply.
*/
fun addMessageData(cid: String, message: Message) {
fun addMessageData(receivedEventDate: Date, cid: String, message: Message) {
addMessage(message)
getCurrentChannel(cid)
?.updateLastMessage(message, currentUserId)
?.updateLastMessage(receivedEventDate, message, currentUserId)
?.let(::addChannel)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ internal class EventHandlerSequential(
// note that many of these events should also update user information
is NewMessageEvent -> {
val enrichedMessage = event.message.enrichWithOwnReactions(batch, currentUserId, event.user)
batch.addMessageData(event.cid, enrichedMessage)
batch.addMessageData(event.createdAt, event.cid, enrichedMessage)
batch.getCurrentChannel(event.cid)?.let { channel ->
val updatedChannel = channel.copy(
hidden = false,
Expand All @@ -460,18 +460,20 @@ internal class EventHandlerSequential(
}
is MessageDeletedEvent -> {
batch.addMessageData(
event.createdAt,
event.cid,
event.message.enrichWithOwnReactions(batch, currentUserId, event.user),
)
}
is MessageUpdatedEvent -> {
batch.addMessageData(
event.createdAt,
event.cid,
event.message.enrichWithOwnReactions(batch, currentUserId, event.user),
)
}
is NotificationMessageNewEvent -> {
batch.addMessageData(event.cid, event.message)
batch.addMessageData(event.createdAt, event.cid, event.message)
batch.addChannel(event.channel.copy(hidden = false))
}
is NotificationAddedToChannelEvent -> {
Expand Down

0 comments on commit 95c5b68

Please sign in to comment.