diff --git a/src/uimodel.cpp b/src/uimodel.cpp index 1847cfdc..f5a88790 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -183,10 +183,31 @@ 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; + // --- 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); @@ -200,11 +221,8 @@ void UiModel::Impl::SendMessage() } SendProtocolRequest(profileId, sendMessageRequest); + m_PendingTextSend[profileId][chatId] = true; - entryStr.clear(); - entryPos = 0; - - UpdateEntry(); ResetMessageOffset(); SetHistoryInteraction(true); } @@ -1853,7 +1871,24 @@ 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"); + if (m_PendingTextSend[profileId][chatId]) + { + m_EntryStr[profileId][chatId].clear(); + m_EntryPos[profileId][chatId] = 0; + UpdateEntry(); + m_PendingTextSend[profileId][chatId] = false; + } + } + else + { + LOG_TRACE("send failed"); + } } break; 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();