feat: Enhance message status tracking and logging in MessageRepository and ChatsListViewModel for improved clarity and debugging

This commit is contained in:
k1ngsterr1
2026-01-19 00:32:14 +05:00
parent 8bc1eecf10
commit 19a89ea00e
4 changed files with 111 additions and 30 deletions

View File

@@ -3,6 +3,15 @@ package com.rosetta.messenger.database
import androidx.room.*
import kotlinx.coroutines.flow.Flow
/**
* 🔥 Data class для статуса последнего сообщения
*/
data class LastMessageStatus(
@ColumnInfo(name = "from_me") val fromMe: Int,
@ColumnInfo(name = "delivered") val delivered: Int,
@ColumnInfo(name = "read") val read: Int
)
/**
* Entity для сообщений - как в React Native версии
*/
@@ -304,14 +313,40 @@ interface MessageDao {
suspend fun messageExists(account: String, messageId: String): Boolean
/**
* Отметить все исходящие сообщения к собеседнику как прочитанные (delivered=3)
* Отметить все исходящие сообщения к собеседнику как прочитанные
* Используется когда приходит PacketRead от собеседника
* 🔥 ВАЖНО: delivered=3 означает READ (синхронизировано с ChatViewModel)
*/
@Query("""
UPDATE messages SET delivered = 3
WHERE account = :account AND to_public_key = :opponent AND from_me = 1 AND delivered < 3
UPDATE messages SET delivered = 3, read = 1
WHERE account = :account AND to_public_key = :opponent AND from_me = 1
""")
suspend fun markAllAsRead(account: String, opponent: String)
/**
* 🔥 DEBUG: Получить последнее сообщение в диалоге для отладки
*/
@Query("""
SELECT * FROM messages
WHERE account = :account
AND ((from_public_key = :opponent AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponent))
ORDER BY timestamp DESC, id DESC LIMIT 1
""")
suspend fun getLastMessageDebug(account: String, opponent: String): MessageEntity?
/**
* 🔥 Получить статус последнего сообщения (fromMe, delivered, read) для диалога
* Возвращает null если сообщений нет
*/
@Query("""
SELECT from_me, delivered, read FROM messages
WHERE account = :account
AND ((from_public_key = :opponent AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponent))
ORDER BY timestamp DESC, id DESC LIMIT 1
""")
suspend fun getLastMessageStatus(account: String, opponent: String): LastMessageStatus?
}
/**
@@ -543,25 +578,27 @@ interface DialogDao {
WHERE account = :account
AND ((from_public_key = :opponentKey AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponentKey))
ORDER BY timestamp DESC LIMIT 1),
ORDER BY timestamp DESC, id DESC LIMIT 1),
0
) AS last_message_from_me,
COALESCE(
(SELECT delivered FROM messages
(SELECT
CASE WHEN from_me = 1 THEN delivered ELSE 0 END
FROM messages
WHERE account = :account
AND from_me = 1
AND from_public_key = :account
AND to_public_key = :opponentKey
ORDER BY timestamp DESC LIMIT 1),
AND ((from_public_key = :opponentKey AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponentKey))
ORDER BY timestamp DESC, id DESC LIMIT 1),
0
) AS last_message_delivered,
COALESCE(
(SELECT read FROM messages
(SELECT
CASE WHEN from_me = 1 THEN read ELSE 0 END
FROM messages
WHERE account = :account
AND from_me = 1
AND from_public_key = :account
AND to_public_key = :opponentKey
ORDER BY timestamp DESC LIMIT 1),
AND ((from_public_key = :opponentKey AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponentKey))
ORDER BY timestamp DESC, id DESC LIMIT 1),
0
) AS last_message_read
WHERE EXISTS (