Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/uimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::seconds>(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> sendMessageRequest = std::make_shared<SendMessageRequest>();
sendMessageRequest->chatId = chatId;
sendMessageRequest->chatMessage.text = EntryStrToSendStr(entryStr);
Expand All @@ -200,11 +221,8 @@ void UiModel::Impl::SendMessage()
}

SendProtocolRequest(profileId, sendMessageRequest);
m_PendingTextSend[profileId][chatId] = true;

entryStr.clear();
entryPos = 0;

UpdateEntry();
ResetMessageOffset();
SetHistoryInteraction(true);
}
Expand Down Expand Up @@ -1853,7 +1871,24 @@ void UiModel::Impl::MessageHandler(std::shared_ptr<ServiceMessage> p_ServiceMess
{
std::shared_ptr<SendMessageNotify> sendMessageNotify = std::static_pointer_cast<SendMessageNotify>(
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;

Expand Down
1 change: 1 addition & 0 deletions src/uimodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::map<std::string, bool>> m_PendingTextSend;

private:
void SortChats();
Expand Down