/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Controllers/ChatController.cpp
232 строки
8 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "ChatController.h" #include "Core/Utils/TimeFormat.h" namespace Controllers { ChatController::ChatController(Services::ChatService& chatService) : m_chatService(chatService) {} nlohmann::json ChatController::handleGetChats(const Core::Network::RequestContext& req) { if(const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); auto chats = m_chatService.getUserChats(*req.userId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(!chats.empty()) { response["status"] = "ok"; nlohmann::json arr = nlohmann::json::array(); for(const auto& chat : std::as_const(chats)) { nlohmann::json chatObj; chatObj["id"] = chat.id; chatObj["chat_type"] = Core::Types::chatTypeToString(chat.type); chatObj["title"] = chat.title; if(chat.type == Core::Types::ChatType::Dialog && chat.peerId.has_value()) { chatObj["peer_id"] = *chat.peerId; } if(chat.lastMessage.has_value()) { nlohmann::json lastMessageObj; lastMessageObj["id"] = chat.lastMessage->id; lastMessageObj["chat_id"] = chat.lastMessage->chatId; lastMessageObj["sender_id"] = chat.lastMessage->senderId; lastMessageObj["type"] = Core::Types::messageTypeToString(chat.lastMessage->type); lastMessageObj["text"] = chat.lastMessage->text; lastMessageObj["timestamp"] = Core::Utils::to_qt_timestamp(chat.lastMessage->timestamp); chatObj["lastMessage"] = lastMessageObj; } else { chatObj["lastMessage"] = nullptr; } arr.push_back(chatObj); } response["chats"] = arr; } else { response["status"] = "error"; response["message"] = "Failed to load chats"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json ChatController::handleCreateDialog(const Core::Network::RequestContext& req) { if (const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string targetUserId = json.value("target_user_id", ""); auto chat = m_chatService.createDialog(*req.userId, targetUserId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(chat) { response["status"] = "ok"; nlohmann::json chatObj; chatObj["id"] = chat->id; chatObj["chat_type"] = Core::Types::chatTypeToString(chat->type); chatObj["title"] = chat->title; if(chat->type == Core::Types::ChatType::Dialog && chat->peerId.has_value()) { chatObj["peer_id"] = *chat->peerId; } nlohmann::json lastMessageObj; lastMessageObj["type"] = Core::Types::messageTypeToString(chat->lastMessage->type); lastMessageObj["text"] = chat->lastMessage->text; lastMessageObj["timestamp"] = Core::Utils::to_qt_timestamp(chat->lastMessage->timestamp); chatObj["lastMessage"] = lastMessageObj; response["chat"] = chatObj; } else { response["status"] = "error"; response["message"] = "Failed to create dialog"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json ChatController::handleCreateGroup(const Core::Network::RequestContext& req) { if (const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string groupName = json.value("group_name", ""); std::vector<Data::Models::GroupMemberModel> members; for (const auto& member: std::as_const(json["members"])) { if (!member.contains("id")) continue; Data::Models::GroupMemberModel memberInfo; memberInfo.id = member.value("id", ""); if (member.contains("role") && member["role"].is_string()) { memberInfo.role = Core::Types::memberRoleFromString(member["role"]); } else { memberInfo.role = Core::Types::MemberRole::Member; } members.push_back(std::move(memberInfo)); } auto chat = m_chatService.createGroup(*req.userId, groupName, members); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if (chat) { response["status"] = "ok"; nlohmann::json chatObj; chatObj["chat_id"] = chat->id; chatObj["chat_type"] = Core::Types::chatTypeToString(chat->type); chatObj["title"] = chat->title; nlohmann::json lastMessageObj; lastMessageObj["type"] = Core::Types::messageTypeToString(chat->lastMessage->type); lastMessageObj["text"] = chat->lastMessage->text; lastMessageObj["timestamp"] = Core::Utils::to_qt_timestamp(chat->lastMessage->timestamp); chatObj["lastMessage"] = lastMessageObj; response["chat"] = chatObj; } else { response["status"] = "error"; response["message"] = "Failed to create group"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json ChatController::handleAddMembers(const Core::Network::RequestContext& req) { if(const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); const std::string chatId = json.value("chat_id", ""); std::vector<Data::Models::GroupMemberModel> members; for (const auto& member: std::as_const(json["members"])) { if (!member.contains("id")) continue; Data::Models::GroupMemberModel memberInfo; memberInfo.id = member.value("id", ""); if (member.contains("role") && member["role"].is_string()) { memberInfo.role = Core::Types::memberRoleFromString(member["role"]); } else { memberInfo.role = Core::Types::MemberRole::Member; } members.push_back(std::move(memberInfo)); } auto added = m_chatService.addChatMembers(chatId, members); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if (added) { response["status"] = "ok"; nlohmann::json arr = nlohmann::json::array(); for(const auto& member : *added) { arr.push_back({ {"id", member.id}, {"name", ""}, {"role", Core::Types::memberRoleToString(member.role)} }); } } else { response["status"] = "error"; response["message"] = "Failed to add members"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } } // namespace Controllers