refactor(tgbot-front): replaced the context usage with a new thread-safe one

This commit is contained in:
Kirill 2025-12-06 05:02:34 +03:00
parent b1c035ae35
commit a6848bb4d7
5 changed files with 55 additions and 56 deletions

View file

@ -40,4 +40,19 @@ std::optional<NavigationStep> BotUserContext::getCurrentStep(int64_t userId) con
void BotUserContext::removeContext(int64_t userId) {
std::lock_guard<std::mutex> lock(mtx);
userContexts.erase(userId);
}
bool BotUserContext::popStep(int64_t userId) {
std::lock_guard<std::mutex> lock(mtx);
auto it = userContexts.find(userId);
if (it != userContexts.end() && (it->second.history.size() > 1)) {
it->second.history.pop_back();
return true;
}
return false;
}
void BotUserContext::createInitContext(int64_t userId) {
NavigationStep initStep = {UserState::MAIN_MENU, BotConstants::NULL_PAYLOAD};
setContext(userId, {userId, {initStep}});
}

View file

@ -13,7 +13,7 @@ void AnimeBot::setupHandlers() {
bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) {
sendMainMenu(message->chat->id);
//TODO: производить инициализацию контекста только после авторизации
handler.createInitContext(message->chat->id);
handler.initUser(message->from->id);
});
bot.getEvents().onCallbackQuery([this](TgBot::CallbackQuery::Ptr query) {

View file

@ -3,13 +3,15 @@
#include "structs.hpp"
#include "constants.hpp"
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
const auto& current = ctx.history.back(); // текущий экран
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query) {
//const auto& current = ctx.history.back(); // текущий экран
const std::string& data = query->data;
int64_t userId = query->from->id;
int64_t chatId = query->message->chat->id;
int64_t messageId = query->message->messageId;
// Пагинация (в списках)
/* Временно отключаем, все равно не функционирует :)
if ((data == BotConstants::Callback::LIST_PREV || data == BotConstants::Callback::LIST_NEXT)
&& (current.state == UserState::VIEWING_MY_TITLES || current.state == UserState::VIEWING_REVIEW_LIST ||
current.state == UserState::VIEWING_FOUND_TITLES)) {
@ -32,35 +34,46 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
renderCurrent(query, ctx);
return;
}
}*/
// Обработка back по интерфейсу
if (data == BotConstants::Callback::NAV_BACK) {
if (!popState(ctx)) {
if (!contextManager.popStep(userId)) {
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
return;
}
renderCurrent(query, ctx);
renderCurrent(query);
return;
}
// Переходы вперёд (push)
auto newStepOpt = computeNextStep(query, current);
std::optional<NavigationStep> currentStep = contextManager.getCurrentStep(userId);
if(!currentStep.has_value()) {
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
return;
}
auto newStepOpt = computeNextStep(query, currentStep.value());
if (!newStepOpt.has_value()) {
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
return;
}
ctx.history.push_back(*newStepOpt);
renderCurrent(query, ctx);
contextManager.pushNavigationStep(userId, newStepOpt.value());
renderCurrent(query);
}
void BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query, const UserContext& ctx) {
const auto& step = ctx.history.back();
//int64_t userId = query->from->id;
void BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query) {
int64_t userId = query->from->id;
int64_t chatId = query->message->chat->id;
int64_t messageId = query->message->messageId;
switch (step.state) {
auto step = contextManager.getCurrentStep(userId);
if(!step.has_value()) {;
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
return;
}
switch (step.value().state) {
case UserState::MAIN_MENU:
editMessage(chatId, messageId, showMainMenu());
return;