Оптимизация
This commit is contained in:
@@ -8,6 +8,7 @@ import com.rosetta.messenger.network.*
|
||||
import com.rosetta.messenger.utils.AttachmentFileManager
|
||||
import com.rosetta.messenger.utils.AvatarFileManager
|
||||
import com.rosetta.messenger.utils.MessageLogger
|
||||
import java.util.Locale
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -51,6 +52,7 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
private val avatarDao = database.avatarDao()
|
||||
private val syncTimeDao = database.syncTimeDao()
|
||||
private val groupDao = database.groupDao()
|
||||
private val searchIndexDao = database.messageSearchIndexDao()
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
@@ -214,6 +216,25 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
|
||||
if (inserted == -1L) return
|
||||
|
||||
val insertedMessage =
|
||||
MessageEntity(
|
||||
account = account,
|
||||
fromPublicKey = SYSTEM_SAFE_PUBLIC_KEY,
|
||||
toPublicKey = account,
|
||||
content = "",
|
||||
timestamp = timestamp,
|
||||
chachaKey = "",
|
||||
read = 0,
|
||||
fromMe = 0,
|
||||
delivered = DeliveryStatus.DELIVERED.value,
|
||||
messageId = messageId,
|
||||
plainMessage = encryptedPlainMessage,
|
||||
attachments = "[]",
|
||||
primaryAttachmentType = -1,
|
||||
dialogKey = dialogKey
|
||||
)
|
||||
upsertSearchIndex(account, insertedMessage, messageText)
|
||||
|
||||
val existing = dialogDao.getDialog(account, SYSTEM_SAFE_PUBLIC_KEY)
|
||||
dialogDao.insertDialog(
|
||||
DialogEntity(
|
||||
@@ -274,6 +295,25 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
|
||||
if (inserted == -1L) return null
|
||||
|
||||
val insertedMessage =
|
||||
MessageEntity(
|
||||
account = account,
|
||||
fromPublicKey = SYSTEM_UPDATES_PUBLIC_KEY,
|
||||
toPublicKey = account,
|
||||
content = "",
|
||||
timestamp = timestamp,
|
||||
chachaKey = "",
|
||||
read = 0,
|
||||
fromMe = 0,
|
||||
delivered = DeliveryStatus.DELIVERED.value,
|
||||
messageId = messageId,
|
||||
plainMessage = encryptedPlainMessage,
|
||||
attachments = "[]",
|
||||
primaryAttachmentType = -1,
|
||||
dialogKey = dialogKey
|
||||
)
|
||||
upsertSearchIndex(account, insertedMessage, messageText)
|
||||
|
||||
val existing = dialogDao.getDialog(account, SYSTEM_UPDATES_PUBLIC_KEY)
|
||||
dialogDao.insertDialog(
|
||||
DialogEntity(
|
||||
@@ -536,6 +576,7 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
dialogKey = dialogKey
|
||||
)
|
||||
messageDao.insertMessage(entity)
|
||||
upsertSearchIndex(account, entity, text.trim())
|
||||
|
||||
// 📝 LOG: Сохранено в БД
|
||||
MessageLogger.logDbSave(messageId, dialogKey, isNew = true)
|
||||
@@ -563,6 +604,17 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
lastSeen = existing?.lastSeen ?: 0,
|
||||
verified = existing?.verified ?: 0,
|
||||
iHaveSent = 1,
|
||||
hasContent =
|
||||
if (
|
||||
encryptedPlainMessage.isNotBlank() ||
|
||||
attachments.isNotEmpty()
|
||||
) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
},
|
||||
lastMessageAttachmentType = resolvePrimaryAttachmentType(attachments),
|
||||
lastSenderKey = account,
|
||||
lastMessageFromMe = 1,
|
||||
lastMessageDelivered = 1,
|
||||
lastMessageRead = 1,
|
||||
@@ -875,6 +927,7 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
if (!stillExists) {
|
||||
// Сохраняем в БД только если сообщения нет
|
||||
messageDao.insertMessage(entity)
|
||||
upsertSearchIndex(account, entity, plainText)
|
||||
MessageLogger.logDbSave(messageId, dialogKey, isNew = true)
|
||||
} else {
|
||||
MessageLogger.logDbSave(messageId, dialogKey, isNew = false)
|
||||
@@ -1411,7 +1464,8 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
opponentKey = opponentKey,
|
||||
lastMessage = encryptedLastMessage,
|
||||
lastMessageTimestamp = timestamp,
|
||||
unreadCount = unreadCount
|
||||
unreadCount = unreadCount,
|
||||
hasContent = if (encryptedLastMessage.isNotBlank()) 1 else 0
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -1649,6 +1703,26 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
return attachments.first().type.value
|
||||
}
|
||||
|
||||
private suspend fun upsertSearchIndex(account: String, entity: MessageEntity, plainText: String) {
|
||||
val opponentKey =
|
||||
if (entity.fromMe == 1) entity.toPublicKey.trim() else entity.fromPublicKey.trim()
|
||||
val normalized = plainText.lowercase(Locale.ROOT)
|
||||
searchIndexDao.upsert(
|
||||
listOf(
|
||||
MessageSearchIndexEntity(
|
||||
account = account,
|
||||
messageId = entity.messageId,
|
||||
dialogKey = entity.dialogKey,
|
||||
opponentKey = opponentKey,
|
||||
timestamp = entity.timestamp,
|
||||
fromMe = entity.fromMe,
|
||||
plainText = plainText,
|
||||
plainTextNormalized = normalized
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 📸 Обработка AVATAR attachments - сохранение аватара отправителя в кэш Как в desktop: при
|
||||
* получении attachment с типом AVATAR - сохраняем в avatar_cache
|
||||
|
||||
Reference in New Issue
Block a user