feat: Implement special handling for Saved Messages, including dedicated methods for retrieval, display, and dialog updates
This commit is contained in:
@@ -138,6 +138,32 @@ interface MessageDao {
|
||||
""")
|
||||
suspend fun getMessages(account: String, dialogKey: String, limit: Int, offset: Int): List<MessageEntity>
|
||||
|
||||
/**
|
||||
* 📁 Получить сообщения для Saved Messages (постранично)
|
||||
* Специальный метод для случая когда from_public_key = to_public_key = account
|
||||
* Использует упрощенный запрос без дублирования OR условий
|
||||
*/
|
||||
@Query("""
|
||||
SELECT * FROM messages
|
||||
WHERE account = :account
|
||||
AND from_public_key = :account
|
||||
AND to_public_key = :account
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT :limit OFFSET :offset
|
||||
""")
|
||||
suspend fun getMessagesForSavedDialog(account: String, limit: Int, offset: Int): List<MessageEntity>
|
||||
|
||||
/**
|
||||
* 📁 Получить количество сообщений в Saved Messages
|
||||
*/
|
||||
@Query("""
|
||||
SELECT COUNT(*) FROM messages
|
||||
WHERE account = :account
|
||||
AND from_public_key = :account
|
||||
AND to_public_key = :account
|
||||
""")
|
||||
suspend fun getMessageCountForSavedDialog(account: String): Int
|
||||
|
||||
/**
|
||||
* Получить сообщения диалога как Flow
|
||||
*/
|
||||
@@ -508,4 +534,77 @@ interface DialogDao {
|
||||
)
|
||||
""")
|
||||
suspend fun updateDialogFromMessages(account: String, opponentKey: String)
|
||||
|
||||
/**
|
||||
* 📁 Обновить Saved Messages диалог, пересчитав счетчики из таблицы messages
|
||||
* Специальный метод для случая когда opponentKey == account (saved messages)
|
||||
* Использует упрощенный запрос без дублирования OR условий
|
||||
*
|
||||
* Ключевые отличия от обычного updateDialogFromMessages:
|
||||
* 1. Упрощенные WHERE условия: from_public_key = :account AND to_public_key = :account
|
||||
* 2. unread_count всегда 0 (нельзя иметь непрочитанные от самого себя)
|
||||
* 3. i_have_sent всегда 1 (все сообщения исходящие)
|
||||
*/
|
||||
@Query("""
|
||||
INSERT OR REPLACE INTO dialogs (
|
||||
account,
|
||||
opponent_key,
|
||||
opponent_title,
|
||||
opponent_username,
|
||||
last_message,
|
||||
last_message_timestamp,
|
||||
unread_count,
|
||||
is_online,
|
||||
last_seen,
|
||||
verified,
|
||||
i_have_sent
|
||||
)
|
||||
SELECT
|
||||
:account AS account,
|
||||
:account AS opponent_key,
|
||||
COALESCE(
|
||||
(SELECT opponent_title FROM dialogs WHERE account = :account AND opponent_key = :account),
|
||||
''
|
||||
) AS opponent_title,
|
||||
COALESCE(
|
||||
(SELECT opponent_username FROM dialogs WHERE account = :account AND opponent_key = :account),
|
||||
''
|
||||
) AS opponent_username,
|
||||
COALESCE(
|
||||
(SELECT plain_message FROM messages
|
||||
WHERE account = :account
|
||||
AND from_public_key = :account
|
||||
AND to_public_key = :account
|
||||
ORDER BY timestamp DESC LIMIT 1),
|
||||
''
|
||||
) AS last_message,
|
||||
COALESCE(
|
||||
(SELECT MAX(timestamp) FROM messages
|
||||
WHERE account = :account
|
||||
AND from_public_key = :account
|
||||
AND to_public_key = :account),
|
||||
0
|
||||
) AS last_message_timestamp,
|
||||
0 AS unread_count,
|
||||
COALESCE(
|
||||
(SELECT is_online FROM dialogs WHERE account = :account AND opponent_key = :account),
|
||||
0
|
||||
) AS is_online,
|
||||
COALESCE(
|
||||
(SELECT last_seen FROM dialogs WHERE account = :account AND opponent_key = :account),
|
||||
0
|
||||
) AS last_seen,
|
||||
COALESCE(
|
||||
(SELECT verified FROM dialogs WHERE account = :account AND opponent_key = :account),
|
||||
0
|
||||
) AS verified,
|
||||
1 AS i_have_sent
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM messages
|
||||
WHERE account = :account
|
||||
AND from_public_key = :account
|
||||
AND to_public_key = :account
|
||||
)
|
||||
""")
|
||||
suspend fun updateSavedMessagesDialogFromMessages(account: String)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user