feat: Add avatar attachment for first message to new users

This commit is contained in:
k1ngsterr1
2026-01-24 02:18:58 +05:00
parent ebfec3d0ba
commit fa40f8a535
2 changed files with 56 additions and 0 deletions

View File

@@ -201,6 +201,18 @@ interface MessageDao {
""")
suspend fun getMessageCount(account: String, dialogKey: String): Int
/**
* 📸 Получить количество сообщений между двумя пользователями
* (для проверки первого сообщения при отправке аватара)
*/
@Query("""
SELECT COUNT(*) FROM messages
WHERE account = :account
AND ((from_public_key = :sender AND to_public_key = :recipient)
OR (from_public_key = :recipient AND to_public_key = :sender))
""")
suspend fun getMessageCount(account: String, sender: String, recipient: String): Int
/**
* Получить последние N сообщений диалога
*/

View File

@@ -1,6 +1,7 @@
package com.rosetta.messenger.ui.chats
import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.rosetta.messenger.crypto.CryptoManager
@@ -1136,6 +1137,49 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
val messageAttachments = mutableListOf<MessageAttachment>()
var replyBlobForDatabase = "" // Зашифрованный blob для БД (приватным ключом)
// 📸 Проверяем - это первое сообщение этому пользователю?
// Если да - добавляем свой аватар (как в desktop)
val isFirstMessage = messageDao.getMessageCount(sender, sender, recipient) == 0
if (isFirstMessage) {
try {
Log.d(TAG, "📸 First message to user - checking for avatar...")
// Получаем свой аватар из AvatarRepository
val avatarDao = database.avatarDao()
val myAvatar = avatarDao.getLatestAvatar(sender)
if (myAvatar != null) {
Log.d(TAG, "📸 Found my avatar, path: ${myAvatar.avatar}")
// Читаем и расшифровываем аватар
val avatarBlob = com.rosetta.messenger.utils.AvatarFileManager.readAvatar(
getApplication(),
myAvatar.avatar
)
if (avatarBlob != null && avatarBlob.isNotEmpty()) {
Log.d(TAG, "📸 Avatar blob read successfully, length: ${avatarBlob.length}")
// Шифруем аватар с ChaCha ключом для отправки
val encryptedAvatarBlob = MessageCrypto.encryptReplyBlob(avatarBlob, plainKeyAndNonce)
Log.d(TAG, "📸 Avatar encrypted, length: ${encryptedAvatarBlob.length}")
val avatarAttachmentId = "avatar_${timestamp}"
messageAttachments.add(MessageAttachment(
id = avatarAttachmentId,
blob = encryptedAvatarBlob,
type = AttachmentType.AVATAR,
preview = ""
))
Log.d(TAG, "📸 ✅ Avatar attached to first message!")
} else {
Log.w(TAG, "📸 Avatar blob is null or empty!")
}
} else {
Log.d(TAG, "📸 No avatar found for current user")
}
} catch (e: Exception) {
Log.e(TAG, "📸 Failed to attach avatar to first message", e)
}
}
if (replyMsgsToSend.isNotEmpty()) {
// Формируем JSON массив с цитируемыми сообщениями (как в RN)