feat: Implement user info request and update handling in MessageRepository and ProtocolManager
This commit is contained in:
@@ -102,6 +102,13 @@ class MessageRepository private constructor(private val context: Context) {
|
|||||||
dialogDao.getDialogsFlow(publicKey).collect { entities ->
|
dialogDao.getDialogsFlow(publicKey).collect { entities ->
|
||||||
android.util.Log.d("MessageRepository", "📋 MessageRepository dialogsFlow emitted: ${entities.size} dialogs")
|
android.util.Log.d("MessageRepository", "📋 MessageRepository dialogsFlow emitted: ${entities.size} dialogs")
|
||||||
_dialogs.value = entities.map { it.toDialog() }
|
_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)
|
updateDialog(packet.fromPublicKey, plainText, packet.timestamp, incrementUnread = true)
|
||||||
android.util.Log.d("MessageRepository", "✅ updateDialog completed!")
|
android.util.Log.d("MessageRepository", "✅ updateDialog completed!")
|
||||||
|
|
||||||
|
// 🔥 Запрашиваем информацию о пользователе для отображения имени вместо ключа
|
||||||
|
requestUserInfo(packet.fromPublicKey)
|
||||||
|
|
||||||
// Обновляем кэш
|
// Обновляем кэш
|
||||||
val message = entity.toMessage()
|
val message = entity.toMessage()
|
||||||
updateMessageCache(dialogKey, message)
|
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")
|
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
|
// Extension functions
|
||||||
private fun MessageEntity.toMessage(): Message {
|
private fun MessageEntity.toMessage(): Message {
|
||||||
// 🔓 Расшифровываем plainMessage с использованием приватного ключа
|
// 🔓 Расшифровываем plainMessage с использованием приватного ключа
|
||||||
|
|||||||
@@ -164,6 +164,25 @@ object ProtocolManager {
|
|||||||
_typingUsers.value = _typingUsers.value - typingPacket.fromPublicKey
|
_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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user