Browse Source

Parse PM snapshots and delivery states in the Android repository

master
evgeny 1 week ago
parent
commit
52e38ff5d6
  1. 29
      tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt
  2. 11
      tools/chatgui-android/app/src/main/java/com/utun/chat/data/NativeLib.kt

29
tools/chatgui-android/app/src/main/java/com/utun/chat/data/ChatRepository.kt

@ -65,7 +65,8 @@ data class Message(val id: Long, val author: String, val text: String, val ts: L
val voiceDurationMs: Int = 0, val fileSize: Long = 0,
val imageWidth: Int = 0, val imageHeight: Int = 0,
val videoWidth: Int = 0, val videoHeight: Int = 0, val videoDurationMs: Int = 0,
val downloadState: String = "" /* "", "downloading", "downloaded", "error" */)
val downloadState: String = "", /* "", "downloading", "downloaded", "error" */
val deliveryStatus: String = "")
data class ChatMember(val nodeId: Long, val name: String, val online: Boolean = false,
val isAdmin: Boolean = false, val isSuper: Boolean = false,
@ -537,10 +538,10 @@ class ChatRepository {
fun dmSend(convId: String, text: String) = NativeLib.dmSend(convId, text)
fun getDmConversations(): List<DmConversation> {
fun parseDmConversations(json: String): List<DmConversation> {
val list = mutableListOf<DmConversation>()
try {
val arr = JSONArray(NativeLib.dmListConversations())
val arr = JSONArray(json)
for (i in 0 until arr.length()) {
val obj = arr.getJSONObject(i)
list.add(DmConversation(
@ -554,14 +555,26 @@ class ChatRepository {
return list
}
fun getDmMessages(convId: String, peerName: String = "", limit: Int = 50): List<Message> {
fun parseDmMessages(json: String, peerName: String = ""): List<Message> {
val list = mutableListOf<Message>()
try {
val arr = JSONArray(NativeLib.dmListMessages(convId, limit, 0))
val arr = JSONArray(json)
for (i in 0 until arr.length()) {
val obj = arr.getJSONObject(i)
val dir = obj.optInt("dir", 0)
val outgoing = dir == 1
val file = obj.optString("ct") == "file"
val status = obj.optString("media_status")
val path = obj.optString("media_path")
val delivery = if (file) when {
status == "delivered" -> "Доставлено"
status == "expired" -> "Срок хранения истёк"
outgoing -> "Ожидает доставки"
path.isNotEmpty() -> "Получено"
else -> "Загрузка…"
} else if (outgoing) {
if (obj.optBoolean("pending")) "Ожидает доставки" else "Доставлено"
} else ""
val author = if (outgoing) "Вы" else peerName.ifBlank { "0x%016x".format(obj.optLong("author", 0)) }
list.add(Message(
id = obj.optLong("seq", 0),
@ -569,7 +582,11 @@ class ChatRepository {
text = obj.optString("data", ""),
ts = obj.optLong("ts", 0),
isOutgoing = outgoing,
contentType = obj.optString("ct", "text/plain")
contentType = if (file) "file" else "text/plain",
filePath = path,
fileSize = obj.optLong("media_size"),
downloadState = if (!file) "" else if (path.isNotEmpty()) "downloaded" else if (status == "expired") "error" else "downloading",
deliveryStatus = delivery
))
}
} catch (e: Exception) { Log.w("utun-gui", "getDmMessages error", e) }

11
tools/chatgui-android/app/src/main/java/com/utun/chat/data/NativeLib.kt

@ -184,9 +184,9 @@ object NativeLib {
/* ── DM (личные сообщения) ── */
fun dmStart(nodeId: Long, sourceChId: String, peerName: String) { nativeDmStart(nodeId, sourceChId, peerName) }
fun dmSend(convId: String, text: String) { nativeDmSend(convId, text) }
fun dmListConversations(): String = nativeDmListConversations() ?: "[]"
fun dmListMessages(convId: String, count: Int = 50, offset: Int = 0): String =
nativeDmListMessages(convId, count, offset) ?: "[]"
fun dmRequestConversations() { nativeDmRequestConversations() }
fun dmRequestMessages(convId: String) { nativeDmRequestMessages(convId) }
fun dmSendFile(convId: String, path: String, name: String) { nativeDmSendFile(convId, path, name) }
private external fun nativeInit(dbPath: String, controlPort: Int, config: Any, logCb: Any): Boolean
private external fun nativeStart(configText: String): Boolean
@ -297,6 +297,7 @@ object NativeLib {
/* ── DM JNI ── */
private external fun nativeDmStart(nodeId: Long, sourceChId: String, peerName: String)
private external fun nativeDmSend(convId: String, text: String)
private external fun nativeDmListConversations(): String?
private external fun nativeDmListMessages(convId: String, count: Int, offset: Int): String?
private external fun nativeDmRequestConversations()
private external fun nativeDmRequestMessages(convId: String)
private external fun nativeDmSendFile(convId: String, path: String, name: String)
}

Loading…
Cancel
Save