diff --git a/modules/bot/front/include/BotUserContext.hpp b/modules/bot/front/include/BotUserContext.hpp new file mode 100644 index 0000000..bf69058 --- /dev/null +++ b/modules/bot/front/include/BotUserContext.hpp @@ -0,0 +1,55 @@ +#pragma once +#include +#include +#include +#include + +enum class UserState { + MAIN_MENU, // Главное меню + VIEWING_MY_TITLES, // Список моих тайтлов + AWAITING_TITLE_NAME, // Жду название тайтла для поиска + VIEWING_FOUND_TITLES, // Смотрю найденные тайтлы + VIEWING_TITLE_PAGE, // Смотрю страничку тайтла + AWAITING_REVIEW, // Жду ревью на тайтл + VIEWING_REVIEW_LIST, // Смотрю список ревью на тайтл + VIEWING_REVIEW, // Смотрю (конкретное) ревью на тайтл + VIEWING_DESCRIPTION, // Смотрю описание тайтла + ERROR, // Ошибка состояния +}; + +struct NavigationStep { + UserState state; + int64_t payload; // ID тайтла, ревью и т.д. +}; + + +struct UserContext { + int64_t userId; + std::vector history; // Текущее состояние пользователя + история предыдущих состояний +}; + +class BotUserContext { +private: + mutable std::mutex mtx; + std::unordered_map userContexts; + +public: + // Получить копию контекста пользователя (или std::nullopt, если не найден) + std::optional getContext(int64_t userId) const; + + // Установить/обновить контекст пользователя + void setContext(int64_t userId, const UserContext& context); + + // Добавить шаг навигации к существующему контексту пользователя + // Если пользователя нет — создаётся новый контекст + void pushNavigationStep(int64_t userId, const NavigationStep& step); + + // Заменить текущую историю (полезно, например, при сбросе состояния) + void setNavigationHistory(int64_t userId, const std::vector& history); + + // Получить текущий шаг (последний в истории) или std::nullopt, если нет истории + std::optional getCurrentStep(int64_t userId) const; + + // Удалить контекст пользователя (например, при логауте) + void removeContext(int64_t userId); +}; \ No newline at end of file diff --git a/modules/bot/front/src/BotUserContext.cpp b/modules/bot/front/src/BotUserContext.cpp new file mode 100644 index 0000000..04a423c --- /dev/null +++ b/modules/bot/front/src/BotUserContext.cpp @@ -0,0 +1,43 @@ +#include "BotUserContext.hpp" + +std::optional BotUserContext::getContext(int64_t userId) const { + std::lock_guard lock(mtx); + auto it = userContexts.find(userId); + if (it != userContexts.end()) { + return it->second; + } + return std::nullopt; +} + +void BotUserContext::setContext(int64_t userId, const UserContext& context) { + std::lock_guard lock(mtx); + userContexts[userId] = context; +} + +void BotUserContext::pushNavigationStep(int64_t userId, const NavigationStep& step) { + std::lock_guard lock(mtx); + auto& ctx = userContexts[userId]; + ctx.userId = userId; + ctx.history.push_back(step); +} + +void BotUserContext::setNavigationHistory(int64_t userId, const std::vector& history) { + std::lock_guard lock(mtx); + auto& ctx = userContexts[userId]; + ctx.userId = userId; + ctx.history = history; +} + +std::optional BotUserContext::getCurrentStep(int64_t userId) const { + std::lock_guard lock(mtx); + auto it = userContexts.find(userId); + if (it != userContexts.end() && !it->second.history.empty()) { + return it->second.history.back(); + } + return std::nullopt; +} + +void BotUserContext::removeContext(int64_t userId) { + std::lock_guard lock(mtx); + userContexts.erase(userId); +} \ No newline at end of file