Request DM persistence acknowledgement and send canonical UTF-8

This commit is contained in:
evgeny committed 2026-10-29 13:34:25 +01:00
1 parent 6b971d0386
commit 8cb80fd7be
3 files changed
+43 -22

No files matched your search

@@ -360,7 +360,9 @@ object NativeLib {
/* ── DM (личные сообщения) ── */
fun dmStart(nodeId: Long, sourceChId: String, peerName: String) { nativeDmStart(nodeId, sourceChId, peerName) }
fun dmSend(convId: String, text: String, generation: Long) { nativeDmSend(convId, text, generation) }
/** UTF-8 без Modified UTF-8: true подтверждает очередь, CHAT_EVT_MSG_SUBMITTED — запись истории/outbox. */
fun dmSend(convId: String, text: String, generation: Long, requestId: Long): Boolean =
nativeDmSend(convId, text.toByteArray(Charsets.UTF_8), generation, requestId)
fun dmInvite(requestId: Long, peer: Long, source: String, channelId: Long, name: String, cancel: Boolean = false): Boolean =
nativeDmInvite(requestId, peer, source, channelId, name, cancel)
fun isChannelMember(channelId: String, nodeId: Long): Boolean = nativeIsChannelMember(channelId, nodeId)
@@ -500,7 +502,7 @@ object NativeLib {
/* ── DM JNI ── */
private external fun nativeDmDelete(convId: String, generation: Long, request: Long): Boolean
private external fun nativeDmStart(nodeId: Long, sourceChId: String, peerName: String)
private external fun nativeDmSend(convId: String, text: String, generation: Long)
private external fun nativeDmSend(convId: String, text: ByteArray, generation: Long, requestId: Long): Boolean
private external fun nativeDmInvite(requestId: Long, peer: Long, source: String, channelId: Long, name: String, cancel: Boolean): Boolean
private external fun nativeIsChannelMember(channelId: String, nodeId: Long): Boolean
private external fun nativeDmRequestConversations()
@@ -688,21 +688,30 @@ void utun_bridge_dm_start(uint64_t node_id, const char* source_ch_id, const char
uasync_post(ua, dm_start_trampoline, req);
}
void utun_bridge_dm_send(const char* conv_id, const char* text, uint64_t generation) {
if (!conv_id || !conv_id[0] || !text) { bridge_log(BLEV_ERROR, "dm_send: invalid args"); return; }
int utun_bridge_dm_send(const char* conv_id, const uint8_t* text, size_t length, uint64_t generation, uint64_t request_id) {
if (!conv_id || !conv_id[0] || strlen(conv_id) >= sizeof(((struct dm_send_req*)0)->conv_id) || !text ||
!length || length > DM_DATA_MAX || memchr(text, 0, length) || !generation || !request_id) {
bridge_log(BLEV_ERROR, "dm_send: invalid target/text length=%zu generation=%llu request=%llu", length,
(unsigned long long)generation, (unsigned long long)request_id); return -1;
}
struct UASYNC* ua = instance_lite_get_uasync();
if (!uasync_is_running(ua)) { bridge_log(BLEV_ERROR, "dm_send: uasync not running"); return; }
size_t len = strlen(text);
struct dm_send_req* req = u_calloc(1, sizeof(struct dm_send_req) + len);
if (!req) { bridge_log(BLEV_ERROR, "dm_send: OOM"); return; }
req->inst = instance_lite_get_instance();
strncpy(req->conv_id, conv_id, sizeof(req->conv_id) - 1);
req->generation = generation;
req->data_len = (uint32_t)len;
struct UTUN_INSTANCE* inst = instance_lite_get_instance();
if (!inst || !uasync_is_running(ua)) { bridge_log(BLEV_ERROR, "dm_send: core unavailable request=%llu",
(unsigned long long)request_id); return -1; }
struct dm_send_req* req = u_calloc(1, sizeof(*req) + length);
struct posted_task* post = u_calloc(1, sizeof(*post));
if (!req || !post) { bridge_log(BLEV_ERROR, "dm_send: allocation failed request=%llu", (unsigned long long)request_id);
u_free(req); u_free(post); return -1; }
req->inst = inst;
strcpy(req->conv_id, conv_id);
req->generation = generation; req->request_id = request_id;
req->data_len = (uint32_t)length;
req->data = (uint8_t*)(req + 1);
memcpy(req->data, text, len);
bridge_log(BLEV_DEBUG, "dm_send queued conv=%s len=%zu", conv_id, len);
uasync_post(ua, dm_send_trampoline, req);
memcpy(req->data, text, length);
post->callback = dm_send_trampoline; post->arg = req;
uasync_post_reserved(ua, post);
bridge_log(BLEV_DEBUG, "dm_send queued conv=%s bytes=%zu request=%llu", conv_id, length, (unsigned long long)request_id);
return 0;
}
void utun_bridge_dm_request_conversations(void) {
@@ -2323,14 +2332,22 @@ JNIEXPORT void JNICALL Java_com_utun_chat_data_NativeLib_nativeDmStart(
if (ch) (*env)->ReleaseStringUTFChars(env, sourceChId, ch);
}
JNIEXPORT void JNICALL Java_com_utun_chat_data_NativeLib_nativeDmSend(
JNIEnv* env, jobject thiz, jstring convId, jstring text, jlong generation) {
JNIEXPORT jboolean JNICALL Java_com_utun_chat_data_NativeLib_nativeDmSend(
JNIEnv* env, jobject thiz, jstring convId, jbyteArray text, jlong generation, jlong request) {
(void)thiz;
if (!convId || !text) { bridge_log(BLEV_ERROR, "dm_send JNI: missing target/text"); return JNI_FALSE; }
jsize length = (*env)->GetArrayLength(env, text);
if (length <= 0 || length > DM_DATA_MAX) {
bridge_log(BLEV_ERROR, "dm_send JNI: invalid text bytes=%d", length); return JNI_FALSE;
}
uint8_t data[DM_DATA_MAX];
(*env)->GetByteArrayRegion(env, text, 0, length, (jbyte*)data);
if ((*env)->ExceptionCheck(env)) { bridge_log(BLEV_ERROR, "dm_send JNI: text copy failed"); return JNI_FALSE; }
const char* c = (*env)->GetStringUTFChars(env, convId, NULL);
const char* t = (*env)->GetStringUTFChars(env, text, NULL);
utun_bridge_dm_send(c, t, (uint64_t)generation);
if (t) (*env)->ReleaseStringUTFChars(env, text, t);
if (c) (*env)->ReleaseStringUTFChars(env, convId, c);
if (!c) { bridge_log(BLEV_ERROR, "dm_send JNI: target copy failed"); return JNI_FALSE; }
int rc = utun_bridge_dm_send(c, data, (size_t)length, (uint64_t)generation, (uint64_t)request);
(*env)->ReleaseStringUTFChars(env, convId, c);
return rc == 0 ? JNI_TRUE : JNI_FALSE;
}
/* UI получает подтверждение commit через CHAT_EVT_DM_DELETE_RESULT, а не через return JNI. */
@@ -282,7 +282,9 @@ void utun_bridge_collect_conn_metrics(uint64_t peer_node_id);
* Результат dm_start — CHAT_EVT_DM_CONV_UPDATED; source_ch_id/peer_name могут быть NULL.
* generation в send/history/file относится к поколению беседы, не к instance. */
void utun_bridge_dm_start(uint64_t node_id, const char* source_ch_id, const char* peer_name);
void utun_bridge_dm_send(const char* conv_id, const char* text, uint64_t generation);
/* Копирует 1..DM_DATA_MAX UTF-8 байт без NUL в очередь; generation/request_id ненулевые.
* 0 — запрос принят, результат локальной записи придёт CHAT_EVT_MSG_SUBMITTED; -1 — отказ без события. */
int utun_bridge_dm_send(const char* conv_id, const uint8_t* text, size_t length, uint64_t generation, uint64_t request_id);
/* Запросить CHAT_EVT_DM_CONVERSATIONS. */
void utun_bridge_dm_request_conversations(void);
/* Снимок видимой истории указанного поколения; запоздалый запрос отбрасывается в uasync. */