From 3a7b359f8787576d1c541aaf7d78a8f65c3e3ff4 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sun, 19 Jul 2026 09:30:51 +0300 Subject: [PATCH] fix(chat_sync): online status for joiner on inviter side not sent to GUI/peers - cs_handle_channel_join: call cs_post_channel_online immediately after adding joiner - member_sync_start with _on_invite_sync_done callback: on MT_OK push online=1 via merkle_sync_push_update - DEBUG per-peer online status in cs_post_channel_online --- tools/chatgui/transport/chat_sync.c | 42 ++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/tools/chatgui/transport/chat_sync.c b/tools/chatgui/transport/chat_sync.c index 11242a7f..fb3b1274 100644 --- a/tools/chatgui/transport/chat_sync.c +++ b/tools/chatgui/transport/chat_sync.c @@ -477,8 +477,12 @@ static void cs_post_channel_online(struct chat_sync* cs, const char* ch_id) { for (int i = 0; i < cs->channel_count; i++) if (strcmp(cs->channels[i].channel_id, ch_id) == 0) { ch = &cs->channels[i]; break; } if (ch) { - for (int j = 0; j < ch->peer_count; j++) - if (cs_is_peer_online(cs->inst, ch->peer_ids[j])) online++; + for (int j = 0; j < ch->peer_count; j++) { + int p_on = cs_is_peer_online(cs->inst, ch->peer_ids[j]); + if (p_on) online++; + DEBUG_DEBUG(DEBUG_CATEGORY_DB_SYNC, "%s: PEERS_ONLINE peer[%d]=0x%016llx online=%d", + CS_ID, j, (unsigned long long)ch->peer_ids[j], p_on); + } } size_t cl = strlen(ch_id); if (cl > 63) cl = 63; @@ -486,7 +490,7 @@ static void cs_post_channel_online(struct chat_sync* cs, const char* ch_id) { memcpy(evt + 1, ch_id, cl); uint16_t oc = (uint16_t)online; memcpy(evt + 1 + cl, &oc, 2); gui_bridge_post(GUI_EVT_CHANNEL_PEERS_ONLINE, evt, 1 + (int)cl + 2); - DEBUG_DEBUG(DEBUG_CATEGORY_DB_SYNC, "%s: PEERS_ONLINE ch=%.*s online=%d", CS_ID, (int)cl, ch_id, online); + DEBUG_DEBUG(DEBUG_CATEGORY_DB_SYNC, "%s: PEERS_ONLINE ch=%.*s total_online=%d", CS_ID, (int)cl, ch_id, online); } /* ── Throttled sync (не чаще CS_SYNC_INTERVAL_MS) ── */ @@ -577,6 +581,26 @@ static void _on_member_sync_done(uint64_t peer, const char* ns, int result, void CS_ID, result == MT_OK ? "OK" : "FAIL", ns, (unsigned long long)peer); } +struct invite_sync_arg { + uint64_t node_id; + char ch_id[64]; +}; + +static void _on_invite_sync_done(uint64_t peer, const char* ns, int result, void* arg) { + struct invite_sync_arg* sa = (struct invite_sync_arg*)arg; + DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "%s: invite_sync %s ns=%s peer=%016llx node=0x%016llx", + CS_ID, result == MT_OK ? "OK" : "FAIL", ns, + (unsigned long long)peer, sa ? (unsigned long long)sa->node_id : 0); + if (result == MT_OK && sa && g_cs) { + cs_post_channel_online(g_cs, sa->ch_id); + uint8_t one = 1; + merkle_sync_push_update(g_cs->inst, sa->ch_id, sa->node_id, 0x01, &one, 1); + DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "%s: invite_sync push_update node=0x%016llx online=1 ch=%s", + CS_ID, (unsigned long long)sa->node_id, sa->ch_id); + } + u_free(sa); +} + static void cs_on_conn_up(struct ETCP_CONN* conn, void* arg) { (void)arg; if (!conn || !g_cs) return; @@ -1239,10 +1263,16 @@ static void cs_handle_channel_join(struct chat_sync* cs, uint64_t peer, { uint8_t evt[65]; uint8_t cl=(uint8_t)strlen(ch_id); evt[0]=cl; memcpy(evt+1,ch_id,cl); gui_bridge_post(GUI_EVT_MEMBERS_CHANGED, evt, 1+cl); } + cs_post_channel_online(cs, ch_id); + /* initiate member sync with the joiner (server side) */ - member_sync_start(cs->inst, node_id, ch_id, NULL, NULL); - DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "%s: JOIN starting member_sync with joiner node=0x%016llx ch=%s", - CS_ID, (unsigned long long)node_id, ch_id); + struct invite_sync_arg* sa = u_malloc(sizeof(struct invite_sync_arg)); + if (sa) { sa->node_id = node_id; + strncpy(sa->ch_id, ch_id, sizeof(sa->ch_id) - 1); sa->ch_id[sizeof(sa->ch_id) - 1] = '\0'; + member_sync_start(cs->inst, node_id, ch_id, _on_invite_sync_done, sa); } + else member_sync_start(cs->inst, node_id, ch_id, NULL, NULL); + DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "%s: JOIN starting member_sync with joiner node=0x%016llx ch=%s done_cb=%s", + CS_ID, (unsigned long long)node_id, ch_id, sa ? "yes" : "no"); DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "%s: JOIN accepted node=0x%016llx ch=%s addrs=%d", CS_ID, (unsigned long long)node_id, ch_id, addr_cnt);