feat: Load user information for requests; enhance dialog title handling and improve data retrieval

This commit is contained in:
2026-01-17 01:17:07 +05:00
parent f71209f3c6
commit 97eac22879
4 changed files with 62 additions and 29 deletions

View File

@@ -7,6 +7,7 @@ import com.rosetta.messenger.crypto.CryptoManager
import com.rosetta.messenger.database.DialogEntity
import com.rosetta.messenger.database.RosettaDatabase
import com.rosetta.messenger.network.PacketOnlineSubscribe
import com.rosetta.messenger.network.PacketSearch
import com.rosetta.messenger.network.ProtocolManager
import com.rosetta.messenger.network.SearchUser
import kotlinx.coroutines.Dispatchers
@@ -113,10 +114,17 @@ class ChatsListViewModel(application: Application) : AndroidViewModel(applicatio
// 📬 Подписываемся на requests (запросы от новых пользователей)
viewModelScope.launch {
android.util.Log.d("ChatsVM", "📬 Subscribing to requests for publicKey=${publicKey.take(16)}...")
dialogDao.getRequestsFlow(publicKey)
.flowOn(Dispatchers.IO)
.map { requestsList ->
android.util.Log.d("ChatsVM", "📬 Received ${requestsList.size} requests from DB")
requestsList.map { dialog ->
// 🔥 Загружаем информацию о пользователе если её нет
if (dialog.opponentTitle.isEmpty() || dialog.opponentTitle == dialog.opponentKey) {
loadUserInfoForRequest(dialog.opponentKey)
}
val decryptedLastMessage = try {
if (privateKey.isNotEmpty() && dialog.lastMessage.isNotEmpty()) {
CryptoManager.decryptWithPassword(dialog.lastMessage, privateKey)
@@ -132,7 +140,7 @@ class ChatsListViewModel(application: Application) : AndroidViewModel(applicatio
id = dialog.id,
account = dialog.account,
opponentKey = dialog.opponentKey,
opponentTitle = dialog.opponentTitle,
opponentTitle = dialog.opponentTitle, // 🔥 Показываем имя как в обычных чатах
opponentUsername = dialog.opponentUsername,
lastMessage = decryptedLastMessage,
lastMessageTimestamp = dialog.lastMessageTimestamp,
@@ -294,6 +302,34 @@ class ChatsListViewModel(application: Application) : AndroidViewModel(applicatio
}
}
/**
* 📬 Загрузить информацию о пользователе для request
*/
private fun loadUserInfoForRequest(publicKey: String) {
viewModelScope.launch(Dispatchers.IO) {
try {
val sharedPrefs = getApplication<Application>().getSharedPreferences("rosetta", Application.MODE_PRIVATE)
val currentUserPrivateKey = sharedPrefs.getString("private_key", "") ?: ""
if (currentUserPrivateKey.isEmpty()) return@launch
// 🔥 ВАЖНО: Используем хеш ключа, как в MessageRepository.requestUserInfo
val privateKeyHash = CryptoManager.generatePrivateKeyHash(currentUserPrivateKey)
android.util.Log.d("ChatsVM", "📬 Requesting user info for: ${publicKey.take(16)}...")
// Запрашиваем информацию о пользователе с сервера
val packet = PacketSearch().apply {
this.privateKey = privateKeyHash
this.search = publicKey
}
ProtocolManager.send(packet)
} catch (e: Exception) {
android.util.Log.e("ChatsVM", "📬 Error loading user info: ${e.message}")
}
}
}
/**
* Проверить заблокирован ли пользователь
*/