feat(tgbot-front): start creating thread-safe user context
This commit is contained in:
parent
a7b47c564a
commit
b1c035ae35
2 changed files with 98 additions and 0 deletions
43
modules/bot/front/src/BotUserContext.cpp
Normal file
43
modules/bot/front/src/BotUserContext.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include "BotUserContext.hpp"
|
||||
|
||||
std::optional<UserContext> BotUserContext::getContext(int64_t userId) const {
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mtx);
|
||||
userContexts[userId] = context;
|
||||
}
|
||||
|
||||
void BotUserContext::pushNavigationStep(int64_t userId, const NavigationStep& step) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
auto& ctx = userContexts[userId];
|
||||
ctx.userId = userId;
|
||||
ctx.history.push_back(step);
|
||||
}
|
||||
|
||||
void BotUserContext::setNavigationHistory(int64_t userId, const std::vector<NavigationStep>& history) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
auto& ctx = userContexts[userId];
|
||||
ctx.userId = userId;
|
||||
ctx.history = history;
|
||||
}
|
||||
|
||||
std::optional<NavigationStep> BotUserContext::getCurrentStep(int64_t userId) const {
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mtx);
|
||||
userContexts.erase(userId);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue