Initial commit - realms platform

This commit is contained in:
doomtube 2026-01-05 22:54:27 -05:00
parent c590ab6d18
commit c717c3751c
234 changed files with 74103 additions and 15231 deletions

View file

@ -0,0 +1,184 @@
#include "ChatController.h"
#include "ChatWebSocketController.h"
#include "../services/ChatService.h"
#include "../services/AuthService.h"
#include <json/json.h>
void ChatController::getMessages(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& realmId) {
auto& chatService = services::ChatService::getInstance();
auto limitParam = req->getParameter("limit");
int limit = limitParam.empty() ? 100 : std::stoi(limitParam);
auto beforeParam = req->getParameter("before");
int64_t before = beforeParam.empty() ? 0 : std::stoll(beforeParam);
auto messages = chatService.getRealmMessages(realmId, limit, before);
Json::Value response;
response["messages"] = Json::arrayValue;
for (const auto& msg : messages) {
response["messages"].append(msg.toJson());
}
auto resp = HttpResponse::newHttpJsonResponse(response);
callback(resp);
}
void ChatController::sendMessage(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) {
// This endpoint is mainly for testing; WebSocket is preferred
auto jsonPtr = req->getJsonObject();
if (!jsonPtr) {
Json::Value error;
error["error"] = "Invalid JSON";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
auto& json = *jsonPtr;
std::string realmId = json.get("realmId", "").asString();
std::string content = json.get("content", "").asString();
if (realmId.empty() || content.empty()) {
Json::Value error;
error["error"] = "Missing required fields";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
// Get user from token
auto& authService = services::AuthService::getInstance();
auto token = req->getHeader("Authorization");
if (token.find("Bearer ") == 0) {
token = token.substr(7);
}
auto claims = authService.verifyToken(token);
if (!claims.has_value()) {
Json::Value error;
error["error"] = "Unauthorized";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k401Unauthorized);
callback(resp);
return;
}
auto& chatService = services::ChatService::getInstance();
models::ChatMessage message;
auto result = chatService.sendMessage(
realmId, claims->userId, claims->username, claims->userColor, claims->avatarUrl, content,
false, false, false, message
);
if (result == services::SendMessageResult::SUCCESS) {
auto resp = HttpResponse::newHttpJsonResponse(message.toJson());
callback(resp);
} else {
Json::Value error;
error["error"] = "Failed to send message";
error["reason"] = static_cast<int>(result);
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k400BadRequest);
callback(resp);
}
}
void ChatController::deleteMessage(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& messageId) {
// Verify user is moderator
auto& authService = services::AuthService::getInstance();
auto token = req->getHeader("Authorization");
if (token.find("Bearer ") == 0) {
token = token.substr(7);
}
auto claims = authService.verifyToken(token);
if (!claims.has_value()) {
Json::Value error;
error["error"] = "Unauthorized";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k401Unauthorized);
callback(resp);
return;
}
std::string realmId = req->getParameter("realmId");
auto& chatService = services::ChatService::getInstance();
bool success = chatService.deleteMessage(realmId, messageId, claims->userId);
Json::Value response;
response["success"] = success;
auto resp = HttpResponse::newHttpJsonResponse(response);
callback(resp);
}
void ChatController::getSettings(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& realmId) {
auto& chatService = services::ChatService::getInstance();
auto settings = chatService.getRealmSettings(realmId);
auto resp = HttpResponse::newHttpJsonResponse(settings.toJson());
callback(resp);
}
void ChatController::updateSettings(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& realmId) {
auto jsonPtr = req->getJsonObject();
if (!jsonPtr) {
Json::Value error;
error["error"] = "Invalid JSON";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
// Verify user is realm owner or moderator
auto& authService = services::AuthService::getInstance();
auto token = req->getHeader("Authorization");
if (token.find("Bearer ") == 0) {
token = token.substr(7);
}
auto claims = authService.verifyToken(token);
if (!claims.has_value()) {
Json::Value error;
error["error"] = "Unauthorized";
auto resp = HttpResponse::newHttpJsonResponse(error);
resp->setStatusCode(k401Unauthorized);
callback(resp);
return;
}
auto settings = models::ChatSettings::fromJson(*jsonPtr);
settings.realmId = realmId;
auto& chatService = services::ChatService::getInstance();
bool success = chatService.updateRealmSettings(realmId, settings);
Json::Value response;
response["success"] = success;
response["settings"] = settings.toJson();
auto resp = HttpResponse::newHttpJsonResponse(response);
callback(resp);
}
void ChatController::getRealmStats(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) {
// Get realm stats from WebSocket controller (active connections per realm)
auto stats = ChatWebSocketController::getRealmStats();
auto resp = HttpResponse::newHttpJsonResponse(stats);
callback(resp);
}