fix: reconcile realtime mention counters locally
fix: update unread mention badges from websocket events fix: clear stale notifications when active chats receive messages
This commit is contained in:
@@ -136,11 +136,15 @@ interface ChatDao {
|
|||||||
SET unread_count = CASE
|
SET unread_count = CASE
|
||||||
WHEN :incrementBy > 0 THEN unread_count + :incrementBy
|
WHEN :incrementBy > 0 THEN unread_count + :incrementBy
|
||||||
ELSE unread_count
|
ELSE unread_count
|
||||||
|
END,
|
||||||
|
unread_mentions_count = CASE
|
||||||
|
WHEN :mentionIncrementBy > 0 THEN unread_mentions_count + :mentionIncrementBy
|
||||||
|
ELSE unread_mentions_count
|
||||||
END
|
END
|
||||||
WHERE id = :chatId
|
WHERE id = :chatId
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
suspend fun incrementUnread(chatId: Long, incrementBy: Int = 1)
|
suspend fun incrementUnread(chatId: Long, incrementBy: Int = 1, mentionIncrementBy: Int = 0)
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -54,6 +54,21 @@ class HandleRealtimeEventsUseCase @Inject constructor(
|
|||||||
val activeChatId = activeChatTracker.activeChatId.value
|
val activeChatId = activeChatTracker.activeChatId.value
|
||||||
val activeUserId = tokenRepository.getActiveUserId()
|
val activeUserId = tokenRepository.getActiveUserId()
|
||||||
val isOwnMessage = activeUserId != null && event.senderId == activeUserId
|
val isOwnMessage = activeUserId != null && event.senderId == activeUserId
|
||||||
|
val myUsername = activeUserId?.let { userId ->
|
||||||
|
tokenRepository.getAccounts()
|
||||||
|
.firstOrNull { it.userId == userId }
|
||||||
|
?.username
|
||||||
|
?.trim()
|
||||||
|
?.removePrefix("@")
|
||||||
|
?.lowercase()
|
||||||
|
}
|
||||||
|
val isMentionByText = if (myUsername.isNullOrBlank()) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
Regex("(^|\\W)@${Regex.escape(myUsername)}(\\W|$)", RegexOption.IGNORE_CASE)
|
||||||
|
.containsMatchIn(event.text.orEmpty())
|
||||||
|
}
|
||||||
|
val isMention = event.isMention || isMentionByText
|
||||||
val lastMessagePreview = event.text?.takeIf { it.isNotBlank() }
|
val lastMessagePreview = event.text?.takeIf { it.isNotBlank() }
|
||||||
?: fallbackMessagePreview(event.type)
|
?: fallbackMessagePreview(event.type)
|
||||||
messageDao.upsertMessages(
|
messageDao.upsertMessages(
|
||||||
@@ -89,25 +104,15 @@ class HandleRealtimeEventsUseCase @Inject constructor(
|
|||||||
if (activeChatId == event.chatId) {
|
if (activeChatId == event.chatId) {
|
||||||
chatDao.markChatRead(chatId = event.chatId)
|
chatDao.markChatRead(chatId = event.chatId)
|
||||||
messageRepository.syncRecentMessages(chatId = event.chatId)
|
messageRepository.syncRecentMessages(chatId = event.chatId)
|
||||||
|
notificationDispatcher.clearChatNotifications(event.chatId)
|
||||||
} else if (!isOwnMessage) {
|
} else if (!isOwnMessage) {
|
||||||
chatDao.incrementUnread(chatId = event.chatId)
|
chatDao.incrementUnread(
|
||||||
|
chatId = event.chatId,
|
||||||
|
incrementBy = 1,
|
||||||
|
mentionIncrementBy = if (isMention) 1 else 0,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
chatRepository.refreshChat(chatId = event.chatId)
|
chatRepository.refreshChat(chatId = event.chatId)
|
||||||
val myUsername = activeUserId?.let { userId ->
|
|
||||||
tokenRepository.getAccounts()
|
|
||||||
.firstOrNull { it.userId == userId }
|
|
||||||
?.username
|
|
||||||
?.trim()
|
|
||||||
?.removePrefix("@")
|
|
||||||
?.lowercase()
|
|
||||||
}
|
|
||||||
val isMentionByText = if (myUsername.isNullOrBlank()) {
|
|
||||||
false
|
|
||||||
} else {
|
|
||||||
Regex("(^|\\W)@${Regex.escape(myUsername)}(\\W|$)", RegexOption.IGNORE_CASE)
|
|
||||||
.containsMatchIn(event.text.orEmpty())
|
|
||||||
}
|
|
||||||
val isMention = event.isMention || isMentionByText
|
|
||||||
val muted = chatDao.isChatMuted(event.chatId) == true
|
val muted = chatDao.isChatMuted(event.chatId) == true
|
||||||
val shouldNotify = shouldShowMessageNotificationUseCase(
|
val shouldNotify = shouldShowMessageNotificationUseCase(
|
||||||
chatId = event.chatId,
|
chatId = event.chatId,
|
||||||
|
|||||||
Reference in New Issue
Block a user