feat: Implement user info request and update handling in MessageRepository and ProtocolManager

This commit is contained in:
k1ngsterr1
2026-01-13 23:35:21 +05:00
parent 2c173bda26
commit 3dd83c9cc5
2 changed files with 61 additions and 0 deletions

View File

@@ -102,6 +102,13 @@ class MessageRepository private constructor(private val context: Context) {
dialogDao.getDialogsFlow(publicKey).collect { entities ->
android.util.Log.d("MessageRepository", "📋 MessageRepository dialogsFlow emitted: ${entities.size} dialogs")
_dialogs.value = entities.map { it.toDialog() }
// 🔥 Запрашиваем информацию о пользователях, у которых нет имени
entities.forEach { dialog ->
if (dialog.opponentTitle.isBlank() || dialog.opponentTitle == dialog.opponentKey.take(7)) {
requestUserInfo(dialog.opponentKey)
}
}
}
}
}
@@ -307,6 +314,9 @@ class MessageRepository private constructor(private val context: Context) {
updateDialog(packet.fromPublicKey, plainText, packet.timestamp, incrementUnread = true)
android.util.Log.d("MessageRepository", "✅ updateDialog completed!")
// 🔥 Запрашиваем информацию о пользователе для отображения имени вместо ключа
requestUserInfo(packet.fromPublicKey)
// Обновляем кэш
val message = entity.toMessage()
updateMessageCache(dialogKey, message)
@@ -506,6 +516,38 @@ class MessageRepository private constructor(private val context: Context) {
android.util.Log.d("MessageRepository", "🟢 Updated online status for ${publicKey.take(16)}... isOnline=$isOnline")
}
/**
* Обновить информацию о пользователе в диалоге (имя, username, verified)
* Вызывается когда приходит ответ на PacketSearch
*/
suspend fun updateDialogUserInfo(publicKey: String, title: String, username: String, verified: Int) {
val account = currentAccount ?: return
// Проверяем существует ли диалог с этим пользователем
val existing = dialogDao.getDialog(account, publicKey)
if (existing != null) {
dialogDao.updateOpponentInfo(account, publicKey, title, username, verified)
android.util.Log.d("MessageRepository", "✅ Updated user info for ${publicKey.take(16)}... title=$title")
}
}
/**
* Запросить информацию о пользователе с сервера
*/
fun requestUserInfo(publicKey: String) {
val privateKey = currentPrivateKey ?: return
scope.launch {
val privateKeyHash = CryptoManager.generatePrivateKeyHash(privateKey)
val packet = PacketSearch().apply {
this.privateKey = privateKeyHash
this.search = publicKey
}
ProtocolManager.send(packet)
android.util.Log.d("MessageRepository", "📤 Requested user info for ${publicKey.take(16)}...")
}
}
// Extension functions
private fun MessageEntity.toMessage(): Message {
// 🔓 Расшифровываем plainMessage с использованием приватного ключа

View File

@@ -164,6 +164,25 @@ object ProtocolManager {
_typingUsers.value = _typingUsers.value - typingPacket.fromPublicKey
}
}
// 🔥 Обработчик поиска (0x03) - обновляет информацию о пользователях в диалогах
waitPacket(0x03) { packet ->
val searchPacket = packet as PacketSearch
if (searchPacket.users.isNotEmpty()) {
addLog("📋 Search response: ${searchPacket.users.size} users")
scope.launch {
searchPacket.users.forEach { user ->
addLog(" Updating user info: ${user.publicKey.take(16)}... title=${user.title}, username=${user.username}")
messageRepository?.updateDialogUserInfo(
user.publicKey,
user.title,
user.username,
user.verified
)
}
}
}
}
}
/**