feat: Parse and utilize attachments from JSON replies in ChatViewModel
This commit is contained in:
@@ -962,6 +962,32 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||
val replyMessageIdFromJson = replyMessage.optString("message_id", "")
|
||||
val replyTimestamp = replyMessage.optLong("timestamp", 0L)
|
||||
|
||||
// 📸 Парсим attachments из JSON reply (как в Desktop)
|
||||
val replyAttachmentsFromJson = mutableListOf<MessageAttachment>()
|
||||
try {
|
||||
val attachmentsArray = replyMessage.optJSONArray("attachments")
|
||||
if (attachmentsArray != null && attachmentsArray.length() > 0) {
|
||||
for (j in 0 until attachmentsArray.length()) {
|
||||
val attJson = attachmentsArray.getJSONObject(j)
|
||||
val attId = attJson.optString("id", "")
|
||||
val attType = AttachmentType.fromInt(attJson.optInt("type", 0))
|
||||
val attPreview = attJson.optString("preview", "")
|
||||
val attBlob = attJson.optString("blob", "")
|
||||
|
||||
if (attId.isNotEmpty()) {
|
||||
replyAttachmentsFromJson.add(MessageAttachment(
|
||||
id = attId,
|
||||
type = attType,
|
||||
preview = attPreview,
|
||||
blob = attBlob
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "📸 Failed to parse reply attachments from JSON: ${e.message}")
|
||||
}
|
||||
|
||||
|
||||
// 🔥 ВАЖНО: message_id из JSON может не совпадать с messageId в Android БД!
|
||||
// Пытаемся найти реальный messageId в текущих сообщениях по тексту и timestamp
|
||||
@@ -984,22 +1010,28 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||
// Определяем, кто автор цитируемого сообщения
|
||||
val isReplyFromMe = replyPublicKey == myPublicKey
|
||||
|
||||
// 🖼️ Загружаем attachments оригинального сообщения для превью в reply
|
||||
val originalAttachments = try {
|
||||
val originalMessage = messageDao.findMessageByContent(
|
||||
account = account,
|
||||
dialogKey = dialogKey,
|
||||
fromPublicKey = replyPublicKey,
|
||||
timestampFrom = replyTimestamp - 5000,
|
||||
timestampTo = replyTimestamp + 5000
|
||||
)
|
||||
if (originalMessage != null && originalMessage.attachments.isNotEmpty()) {
|
||||
parseAllAttachments(originalMessage.attachments)
|
||||
} else {
|
||||
// <EFBFBD> Используем attachments из JSON если есть, иначе загружаем из БД
|
||||
val originalAttachments = if (replyAttachmentsFromJson.isNotEmpty()) {
|
||||
// Используем attachments из JSON reply
|
||||
replyAttachmentsFromJson
|
||||
} else {
|
||||
// Fallback: загружаем из БД
|
||||
try {
|
||||
val originalMessage = messageDao.findMessageByContent(
|
||||
account = account,
|
||||
dialogKey = dialogKey,
|
||||
fromPublicKey = replyPublicKey,
|
||||
timestampFrom = replyTimestamp - 5000,
|
||||
timestampTo = replyTimestamp + 5000
|
||||
)
|
||||
if (originalMessage != null && originalMessage.attachments.isNotEmpty()) {
|
||||
parseAllAttachments(originalMessage.attachments)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val result = ReplyData(
|
||||
@@ -1264,15 +1296,28 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
if (replyMsgsToSend.isNotEmpty()) {
|
||||
|
||||
// Формируем JSON массив с цитируемыми сообщениями (как в RN)
|
||||
// Формируем JSON массив с цитируемыми сообщениями (как в Desktop)
|
||||
val replyJsonArray = JSONArray()
|
||||
replyMsgsToSend.forEach { msg ->
|
||||
// Формируем attachments JSON (как в Desktop)
|
||||
val attachmentsArray = JSONArray()
|
||||
msg.attachments.forEach { att ->
|
||||
attachmentsArray.put(JSONObject().apply {
|
||||
put("id", att.id)
|
||||
put("type", att.type.value)
|
||||
put("preview", att.preview)
|
||||
// Для IMAGE/FILE - blob не включаем (слишком большой)
|
||||
// Для MESSAGES - включаем blob
|
||||
put("blob", if (att.type == AttachmentType.MESSAGES) att.blob else "")
|
||||
})
|
||||
}
|
||||
|
||||
val replyJson = JSONObject().apply {
|
||||
put("message_id", msg.messageId)
|
||||
put("publicKey", msg.publicKey)
|
||||
put("message", msg.text)
|
||||
put("timestamp", msg.timestamp)
|
||||
put("attachments", JSONArray()) // Пустой массив вложений
|
||||
put("attachments", attachmentsArray)
|
||||
}
|
||||
replyJsonArray.put(replyJson)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user