Skip to content
Merged
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
96 changes: 51 additions & 45 deletions src/controllers/GetChatsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#include <cstring>
#include <stdexcept>

ChatsController::ChatsController(ITgClient& client)
: client_(client)
, last_cache_update_(0)
, last_error_("") {}
ChatsController::ChatsController(ITgClient& client) : client_(client)
, last_cache_update_(0)
, last_error_("") {}

std::vector<ITgClient::Chat> ChatsController::get_chats(int limit) {
clear_error();

if (!cached_chats_.empty() && !should_refresh_cache()) {
int return_count = std::min(static_cast<int>(cached_chats_.size()), limit);
auto result = std::vector<ITgClient::Chat>(
Expand All @@ -23,17 +22,19 @@ std::vector<ITgClient::Chat> ChatsController::get_chats(int limit) {
reverse(result.begin(), result.end());
return result;
}

try {
std::vector<ITgClient::Chat> chats = client_.get_chats(limit);

update_cache(chats);

std::cout << "[ChatsController] Retrieved " << chats.size()
<< " chats\n";

#ifndef NDEBUG
std::cout << "[ChatsController] Retrieved " << chats.size()
<< " chats\n";
#endif

reverse(chats.begin(), chats.end());
return chats;

} catch (const std::exception& e) {
handle_error(std::string("Failed to get chats: ") + e.what());
return {};
Expand All @@ -42,106 +43,109 @@ std::vector<ITgClient::Chat> ChatsController::get_chats(int limit) {

void ChatsController::refresh_chats() {
clear_error();

try {
std::vector<ITgClient::Chat> chats = client_.get_chats(100);

update_cache(chats);

std::cout << "[ChatsController] Cache refreshed with "
<< chats.size() << " chats\n";



#ifndef NDEBUG
std::cout << "[ChatsController] Cache refreshed with "
<< chats.size() << " chats\n";
#endif
} catch (const std::exception& e) {
handle_error(std::string("Failed to refresh chats: ") + e.what());
}
}



void ChatsController::update_cache(const std::vector<ITgClient::Chat>& chats) {
cached_chats_ = chats;

chat_titles_.clear();
for (const auto& chat : chats) {
chat_titles_[chat.chatId] = chat.title;
}

last_cache_update_ = std::time(nullptr);
}

bool ChatsController::should_refresh_cache() const {
const int CACHE_TTL_SECONDS = 5 * 60;

if (cached_chats_.empty()) {
return true;
}

time_t now = std::time(nullptr);
return (now - last_cache_update_) > CACHE_TTL_SECONDS;
}

std::vector<ITgClient::Chat> ChatsController::search_chats(
const std::string& query,
int limit) {

clear_error();

if (query.empty()) {
return get_chats(limit);
}

if (cached_chats_.empty()) {
refresh_chats();
}

std::vector<ITgClient::Chat> result;
std::string query_lower = query;
std::transform(query_lower.begin(), query_lower.end(),

std::transform(query_lower.begin(), query_lower.end(),
query_lower.begin(), ::tolower);

for (const auto& chat : cached_chats_) {
if (result.size() >= static_cast<size_t>(limit)) {
break;
}

std::string title_lower = chat.title;
std::transform(title_lower.begin(), title_lower.end(),
title_lower.begin(), ::tolower);
title_lower.begin(), ::tolower);

if (title_lower.find(query_lower) != std::string::npos) {
result.push_back(chat);
}
}

std::cout << "[ChatsController] Found " << result.size()
<< " chats matching '" << query << "'\n";



#ifndef NDEBUG
std::cout << "[ChatsController] Found " << result.size()
<< " chats matching '" << query << "'\n";
#endif

return result;
}

ITgClient::Chat ChatsController::get_chat_info(const std::string& chat_id) {
clear_error();

if (chat_id.empty()) {
handle_error("Chat ID cannot be empty");
return ITgClient::Chat{"", ""};
}

for (const auto& chat : cached_chats_) {
if (chat.chatId == chat_id) {
return chat;
}
}

refresh_chats();

for (const auto& chat : cached_chats_) {
if (chat.chatId == chat_id) {
return chat;
}
}

handle_error("Chat not found: " + chat_id);
return ITgClient::Chat{"", ""};
}
Expand All @@ -150,21 +154,24 @@ bool ChatsController::chat_exists(const std::string& chat_id) {
if (chat_id.empty()) {
return false;
}

for (const auto& chat : cached_chats_) {
if (chat.chatId == chat_id) {
return true;
}
}

return false;
}

void ChatsController::clear_cache() {
cached_chats_.clear();
chat_titles_.clear();
last_cache_update_ = 0;

#ifndef NDEBUG
std::cout << "[ChatsController] Cache cleared\n";
#endif
}

const std::string& ChatsController::get_last_error() const {
Expand All @@ -182,7 +189,6 @@ void ChatsController::clear_error() {
}

std::string ChatsController::get_chat_title(const std::string& chat_id) {

if (chat_id.empty()) {
return "";
}
Expand All @@ -209,4 +215,4 @@ std::string ChatsController::get_chat_title(const std::string& chat_id) {
}
}
return "Unknown chat";
}
}
4 changes: 4 additions & 0 deletions src/controllers/SendMessageController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ bool MessageController::send_message(const std::string& chat_id,

try {
client_.send_message(chat_id, text);

#ifndef NDEBUG
std::cout << "[MessageController] Message sent to chat " << chat_id << "\n";
#endif

return true;

} catch (const std::exception& e) {
Expand Down
Loading