pica/server/session.cpp

172 lines
4.7 KiB
C++
Raw Normal View History

2023-12-30 22:42:11 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
2023-12-23 20:23:38 +00:00
#include "session.h"
2023-12-28 20:26:08 +00:00
#include "handler/poll.h"
2024-01-03 01:11:56 +00:00
Session::Session(
std::weak_ptr<TM::Scheduler> scheduler,
unsigned int id,
unsigned int owner,
2024-01-03 01:11:56 +00:00
const std::string& access,
2024-01-09 17:02:56 +00:00
const std::string& renew,
unsigned int timeout
2024-01-03 01:11:56 +00:00
):
2023-12-23 20:23:38 +00:00
id(id),
owner(owner),
scheduler(scheduler),
2023-12-23 20:23:38 +00:00
access(access),
2023-12-28 20:26:08 +00:00
renew(renew),
polling(nullptr),
2024-01-09 17:02:56 +00:00
timeoutId(TM::Scheduler::none),
2024-01-13 23:57:42 +00:00
timeout(timeout),
mtx(),
2024-01-17 21:56:53 +00:00
cache({
{"system", {
{"invalidate", true}
}}
})
2023-12-23 20:23:38 +00:00
{}
Session::~Session () {
if (timeoutId != TM::Scheduler::none) {
if (std::shared_ptr<TM::Scheduler> sch = scheduler.lock())
sch->cancel(timeoutId);
}
}
2023-12-23 20:23:38 +00:00
std::string Session::getAccessToken() const {
return access;
}
std::string Session::getRenewToken() const {
return renew;
}
2023-12-28 20:26:08 +00:00
void Session::accept(std::unique_ptr<Request> request) {
2024-01-13 23:57:42 +00:00
std::lock_guard lock(mtx);
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
2023-12-28 20:26:08 +00:00
if (polling) {
if (timeoutId != TM::Scheduler::none) {
if (sch)
sch->cancel(timeoutId);
timeoutId = TM::Scheduler::none;
}
2024-01-09 17:02:56 +00:00
Handler::Poll::error(*polling.get(), Handler::Poll::Result::replace, Response::Status::ok);
polling.reset();
2024-01-03 01:11:56 +00:00
}
2023-12-28 20:26:08 +00:00
2024-01-13 23:57:42 +00:00
std::map form = request->getForm();
auto clear = form.find("clearCache");
if (clear != form.end() && clear->second == "all")
cache.clear();
if (!cache.empty())
return sendUpdates(std::move(request));
2024-01-03 01:11:56 +00:00
if (!sch) {
std::cerr << "Was unable to schedule polling timeout, replying with an error" << std::endl;
Handler::Poll::error(*request.get(), Handler::Poll::Result::unknownError, Response::Status::internalError);
return;
2023-12-28 20:26:08 +00:00
}
2024-01-13 23:57:42 +00:00
2024-01-09 17:02:56 +00:00
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), timeout);
2023-12-28 20:26:08 +00:00
polling = std::move(request);
2023-12-30 22:42:11 +00:00
}
2024-01-13 23:57:42 +00:00
void Session::sendUpdates (std::unique_ptr<Request> request) {
Response& res = request->createResponse(Response::Status::ok);
nlohmann::json body = nlohmann::json::object();
body["result"] = Handler::Poll::Result::success;
nlohmann::json& data = body["data"] = nlohmann::json::object();
for (const auto& category : cache) {
nlohmann::json& cat = data[category.first] = nlohmann::json::object();
for (const auto& entry : category.second)
cat[entry.first] = entry.second;
}
res.setBody(body);
res.send();
}
void Session::onTimeout () {
2024-01-13 23:57:42 +00:00
std::lock_guard lock(mtx);
timeoutId = TM::Scheduler::none;
Handler::Poll::error(*polling.get(), Handler::Poll::Result::timeout, Response::Status::ok);
polling.reset();
}
2024-01-13 23:57:42 +00:00
void Session::assetAdded (const DB::Asset& asset) {
std::lock_guard lock(mtx);
2024-01-21 19:23:48 +00:00
std::map<std::string, nlohmann::json>& assets = cache["assets"];
2024-01-13 23:57:42 +00:00
auto addedItr = assets.find("added");
if (addedItr == assets.end())
addedItr = assets.emplace("added", nlohmann::json::array()).first;
addedItr->second.push_back(asset.toJSON());
checkUpdates();
}
2024-04-07 23:03:10 +00:00
void Session::assetChanged (const DB::Asset& asset) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& assets = cache["assets"];
auto itr = assets.find("changed");
if (itr == assets.end())
itr = assets.emplace("changed", nlohmann::json::array()).first;
removeByID(itr->second, asset.id);
itr->second.push_back(asset.toJSON());
checkUpdates();
}
2024-01-21 19:23:48 +00:00
void Session::assetRemoved (unsigned int assetId) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& assets = cache["assets"];
2024-04-07 23:03:10 +00:00
auto itr = assets.find("added");
if (itr != assets.end())
removeByID(itr->second, assetId);
else {
itr = assets.find("removed");
if (itr == assets.end())
itr = assets.emplace("removed", nlohmann::json::array()).first;
itr->second.push_back(assetId);
}
itr = assets.find("changed");
if (itr != assets.end())
removeByID(itr->second, assetId);
2024-01-21 19:23:48 +00:00
checkUpdates();
}
2024-04-07 23:03:10 +00:00
void Session::removeByID(nlohmann::json& array, unsigned int id) {
array.erase(
std::remove_if(
array.begin(),
array.end(),
[id](const nlohmann::json& item) {
return item["id"].get<unsigned int>() == id;
}
),
array.end()
);
}
2024-01-13 23:57:42 +00:00
void Session::checkUpdates () {
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
if (polling) {
if (timeoutId != TM::Scheduler::none) {
if (sch)
sch->cancel(timeoutId);
timeoutId = TM::Scheduler::none;
}
sendUpdates(std::move(polling));
}
}