From fa854cff460446e0e27abb30671337cb6a1723f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Sat, 18 Jul 2026 09:19:47 -0300 Subject: [PATCH 1/3] ui: preserve input text until message send succeeds Prevents input buffer from clearing immediately upon hitting enter, avoiding message loss during silent network drops. The entry field is now cleared only when SendMessageNotify confirms a successful transmission. --- src/uimodel.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/uimodel.cpp b/src/uimodel.cpp index 1847cfdc..5491d097 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -183,7 +183,6 @@ void UiModel::Impl::SendMessage() std::string profileId = m_CurrentChat.first; std::string chatId = m_CurrentChat.second; std::wstring& entryStr = m_EntryStr[profileId][chatId]; - int& entryPos = m_EntryPos[profileId][chatId]; if (entryStr.empty()) return; @@ -201,10 +200,6 @@ void UiModel::Impl::SendMessage() SendProtocolRequest(profileId, sendMessageRequest); - entryStr.clear(); - entryPos = 0; - - UpdateEntry(); ResetMessageOffset(); SetHistoryInteraction(true); } @@ -1853,7 +1848,20 @@ void UiModel::Impl::MessageHandler(std::shared_ptr p_ServiceMess { std::shared_ptr sendMessageNotify = std::static_pointer_cast( p_ServiceMessage); - LOG_TRACE(sendMessageNotify->success ? "send ok" : "send failed"); + + std::string chatId = sendMessageNotify->chatId; + + if (sendMessageNotify->success) + { + LOG_TRACE("send ok"); + m_EntryStr[profileId][chatId].clear(); + m_EntryPos[profileId][chatId] = 0; + UpdateEntry(); + } + else + { + LOG_TRACE("send failed"); + } } break; From 30ce080170f35062633a82b85840bd581a05f822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Sat, 18 Jul 2026 15:12:09 -0300 Subject: [PATCH 2/3] ui: adds a "cooldown" mechanism to avoid sending the same message twice after changing nchat behavior to keep the input text until message send succeeds --- src/uimodel.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/uimodel.cpp b/src/uimodel.cpp index 5491d097..c8c8039d 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -186,6 +186,28 @@ void UiModel::Impl::SendMessage() if (entryStr.empty()) return; + // --- COOLDOWN MECHANISM START --- + using Clock = std::chrono::steady_clock; + static Clock::time_point lastSendTime; + static std::wstring lastEntryStr; + static std::string lastChatId; + + Clock::time_point now = Clock::now(); + auto elapsed = std::chrono::duration_cast(now - lastSendTime).count(); + + // Block only if it's the exact same message to the same chat within 5 seconds + if (chatId == lastChatId && entryStr == lastEntryStr && elapsed < 5) + { + LOG_TRACE("SendMessage: Skipped identical message within 5-second cooldown."); + return; + } + + // Update tracking state for this valid transmission + lastSendTime = now; + lastEntryStr = entryStr; + lastChatId = chatId; + // --- COOLDOWN MECHANISM END --- + std::shared_ptr sendMessageRequest = std::make_shared(); sendMessageRequest->chatId = chatId; sendMessageRequest->chatMessage.text = EntryStrToSendStr(entryStr); From fa7b57b0fc0f65a68c05dc5a4e5022f676b0d2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Tue, 28 Jul 2026 16:06:57 -0300 Subject: [PATCH 3/3] add a check to only clear the input field if the sent message is a text (avoiding clearing the input field if it is a file transfer --- src/uimodel.cpp | 11 ++++++++--- src/uimodel.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/uimodel.cpp b/src/uimodel.cpp index c8c8039d..f5a88790 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -221,6 +221,7 @@ void UiModel::Impl::SendMessage() } SendProtocolRequest(profileId, sendMessageRequest); + m_PendingTextSend[profileId][chatId] = true; ResetMessageOffset(); SetHistoryInteraction(true); @@ -1876,9 +1877,13 @@ void UiModel::Impl::MessageHandler(std::shared_ptr p_ServiceMess if (sendMessageNotify->success) { LOG_TRACE("send ok"); - m_EntryStr[profileId][chatId].clear(); - m_EntryPos[profileId][chatId] = 0; - UpdateEntry(); + if (m_PendingTextSend[profileId][chatId]) + { + m_EntryStr[profileId][chatId].clear(); + m_EntryPos[profileId][chatId] = 0; + UpdateEntry(); + m_PendingTextSend[profileId][chatId] = false; + } } else { diff --git a/src/uimodel.h b/src/uimodel.h index d245d4aa..bd356c50 100644 --- a/src/uimodel.h +++ b/src/uimodel.h @@ -196,6 +196,7 @@ class UiModel static bool IsAttachmentDownloaded(const FileInfo& p_FileInfo); static bool IsAttachmentDownloadable(const FileInfo& p_FileInfo); static void SanitizeEntryStr(std::string& p_Str); + std::map> m_PendingTextSend; private: void SortChats();