diff --git a/tools/chatgui/src/mainwindow.cpp b/tools/chatgui/src/mainwindow.cpp index a9c30b8e..450f2069 100644 --- a/tools/chatgui/src/mainwindow.cpp +++ b/tools/chatgui/src/mainwindow.cpp @@ -179,6 +179,11 @@ MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath, m_currentChannelId = channelId; m_messageList->loadChannel(channelId); m_accountList->setChannel(channelId); + + QByteArray cidBA = channelId.toUtf8(); + char* cid = (char*)malloc(cidBA.size() + 1); + if (cid) { memcpy(cid, cidBA.constData(), cidBA.size()); cid[cidBA.size()] = '\0'; + gui_bridge_post_uasync_fn(chat_core_connect_channel_trampoline, cid); } }); connect(m_channelList, &ChannelList::inviteRequested, diff --git a/tools/chatgui/transport/chat_core.c b/tools/chatgui/transport/chat_core.c index e8a5860c..9b783680 100644 --- a/tools/chatgui/transport/chat_core.c +++ b/tools/chatgui/transport/chat_core.c @@ -11,6 +11,7 @@ #include "../../../lib/json_flat.h" #include "topo_node_sqlite.h" #include "topo_group.h" +#include "topo_group_connect.h" #include "member_sync.h" #include "../../../src/utun_instance.h" @@ -755,6 +756,39 @@ void chat_core_create_channel_trampoline(void* arg) { u_free(req); } +/* ─── управление подключением к каналу (выбор в GUI) ─── */ + +void chat_core_connect_channel(const char* ch_id) { + DEBUG_TRACE(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel called ch_id=%s initialized=%d", CC_ID, ch_id ? ch_id : "(null)", g_cc.initialized); + if (!g_cc.initialized || !ch_id || !ch_id[0]) { DEBUG_WARN(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel — skip (not ready)", CC_ID); return; } + if (!g_cc.inst->topo_groups) { DEBUG_ERROR(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel — no topo_groups", CC_ID); return; } + + uint64_t gid = strtoull(ch_id, NULL, 10); + if (gid == 0) { DEBUG_ERROR(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel — invalid ch_id=%s", CC_ID, ch_id); return; } + + chat_core_ensure_channel_ready(ch_id); + + struct TOPO_GROUP* group = topo_groups_find(g_cc.inst->topo_groups, gid); + if (!group) { DEBUG_ERROR(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel — group not found ch=%s gid=%016llx total_groups=%d", CC_ID, ch_id, (unsigned long long)gid, queue_entry_count(g_cc.inst->topo_groups->group_list)); return; } + + if (group->connect) { + DEBUG_INFO(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel — already in progress ch=%s active=%d", + CC_ID, ch_id, topo_group_connect_active_count(group)); + return; + } + + DEBUG_INFO(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel ch=%s gid=%016llx group_type=%d — starting connect_init", CC_ID, ch_id, (unsigned long long)gid, group->group_type); + topo_group_connect_init(group); +} + +void chat_core_connect_channel_trampoline(void* arg) { + char* ch_id = (char*)arg; + DEBUG_TRACE(DEBUG_CATEGORY_CONNECTIVITY, "%s: connect_channel_trampoline ch_id=%s", CC_ID, ch_id ? ch_id : "(null)"); + if (!ch_id) return; + chat_core_connect_channel(ch_id); + u_free(ch_id); +} + /* ─── db_sync helpers ─── */ static void on_msg_inserted(struct DB_SYNC_INSTANCE* si, uint64_t record_ts, const char* data, size_t len, uint64_t author, void* arg) { diff --git a/tools/chatgui/transport/chat_core.h b/tools/chatgui/transport/chat_core.h index bcbf93e0..e823b74b 100644 --- a/tools/chatgui/transport/chat_core.h +++ b/tools/chatgui/transport/chat_core.h @@ -71,6 +71,10 @@ void chat_core_sync_my_addresses(void); void chat_core_save_ui_state(const char* key, const char* value); void chat_core_save_ui_state_trampoline(void* arg); +/* Запуск подключения к узлам выбранного канала (GUI → uasync) */ +void chat_core_connect_channel(const char* ch_id); +void chat_core_connect_channel_trampoline(void* arg); + /* Трамплин для gui_bridge_post_uasync (GUI → uasync) */ void chat_core_submit_trampoline(void* arg); diff --git a/tools/chatgui/transport/utun_node.cpp b/tools/chatgui/transport/utun_node.cpp index ea7ec9fe..19363b11 100644 --- a/tools/chatgui/transport/utun_node.cpp +++ b/tools/chatgui/transport/utun_node.cpp @@ -316,25 +316,6 @@ void UtunNode::runLoop() { chat_core_init(m_instance, QString(m_dbPath + "/chats.db").toUtf8().constData()); chat_sync_init(m_instance, nullptr); - /* create TOPO_GROUP_TYPE_CHAT for all existing channels */ - if (m_instance->topo_groups) { - uint8_t buf[4096]; size_t ch_len = 0; - chat_core_list_channels(buf, sizeof(buf), &ch_len); - if (ch_len >= 2) { - uint16_t cnt; memcpy(&cnt, buf, 2); - const uint8_t* p = buf + 2; size_t rem = ch_len - 2; - for (uint16_t i = 0; i < cnt && rem >= 1; i++) { - uint8_t id_len = *p++; rem--; - if (rem < id_len) break; - char ch_id[64]; memcpy(ch_id, p, id_len < 63 ? id_len : 63); ch_id[id_len < 63 ? id_len : 63] = 0; - p += id_len; rem -= id_len; - uint64_t gid = strtoull(ch_id, NULL, 10); - if (gid != 0 && !topo_groups_find(m_instance->topo_groups, gid)) - topo_groups_create_group(m_instance->topo_groups, gid, TOPO_GROUP_TYPE_CHAT, ch_id); - } - } - } - DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "chat_core + chat_sync initialized"); DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "utun_node: entering poll loop");