From 5a07cadd4ae76d9bf7f62e831ccdb386d13d78ea Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Mon, 26 Aug 2024 10:38:38 +0300 Subject: [PATCH] Skeleton for internalapi response completed --- building/main.cpp | 6 ++ .../backend_logic/client_server_interact.cpp | 77 +++++++++++-------- .../backend_logic/client_server_interact.h | 32 +++++--- .../backend_logic/server_data_interact.h | 10 ++- .../when_api_addmembertochat.cpp | 15 ++++ .../backend_logic/when_api_createchat.cpp | 11 +++ .../backend_logic/when_api_deletemessage.cpp | 15 ++++ .../backend_logic/when_api_getchatlist.cpp | 2 +- .../backend_logic/when_api_removechat.cpp | 11 +++ .../when_api_removememberfromchat.cpp | 15 ++++ .../backend_logic/when_api_sendmessage.cpp | 15 ++++ src/web_chat/iu9_ca_web_chat_lib/run.cpp | 25 +++++- 12 files changed, 185 insertions(+), 49 deletions(-) create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_addmembertochat.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_createchat.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_deletemessage.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removechat.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removememberfromchat.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_sendmessage.cpp diff --git a/building/main.cpp b/building/main.cpp index 163f3e8..a5d78c7 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -160,6 +160,12 @@ struct CAWebChat { "backend_logic/when_api_getuserinfo.cpp", "backend_logic/when_api_getmessageinfo.cpp", "backend_logic/when_api_getmessageneighbours.cpp", + "backend_logic/when_api_sendmessage.cpp", + "backend_logic/when_api_deletemessage.cpp", + "backend_logic/when_api_addmembertochat.cpp", + "backend_logic/when_api_removememberfromchat.cpp", + "backend_logic/when_api_createchat.cpp", + "backend_logic/when_api_removechat.cpp", }; for (std::string& u: T.units) u = "web_chat/iu9_ca_web_chat_lib/" + u; diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.cpp index 5ca51d3..5708bf5 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.cpp @@ -1,5 +1,6 @@ #include "client_server_interact.h" #include +#include namespace iu9cawebchat { void initial_extraction_of_all_the_useful_info_from_cookies( @@ -33,46 +34,62 @@ namespace iu9cawebchat { /* ========================= API =========================*/ - - std::string when_internalapi_pollevents(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { + std::string when_internalapi(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid, + const std::function& F) { const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_pollEvents(*wgd.db, uid, Sent), json::print_pretty); + std::string result = json::generate_str(F(*wgd.db, uid, Sent), json::print_pretty); return een9::form_http_server_response_200("text/json", result); } - std::string when_internalapi_getchatlist(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { - const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_getChatList(*wgd.db, uid), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); + std::string when_internalapi_pollevents(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_pollEvents); } - std::string when_internalapi_getchatinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { - const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_getChatInfo(*wgd.db, uid, Sent), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); + std::string when_internalapi_getchatlist(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getChatList); } - std::string when_internalapi_getchatmemberlist(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { - const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_getChatMemberList(*wgd.db, uid, Sent), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); + std::string when_internalapi_getchatinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getChatInfo); } - std::string when_internalapi_getuserinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { - const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_getUserInfo(*wgd.db, uid, Sent), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); + std::string when_internalapi_getchatmemberlist(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getChatMemberList); } - std::string when_internalapi_getmessageinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid) { - const json::JSON& Sent = json::parse_str_flawless(req.body); - std::string result = json::generate_str(internalapi_getMessageInfo(*wgd.db, uid, Sent), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); + std::string when_internalapi_getuserinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getUserInfo); } -} \ No newline at end of file + + std::string when_internalapi_getmessageinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getMessageInfo); + } + + std::string when_internalapi_getmessageneighbours(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_getMessageNeighbours); + } + + std::string when_internalapi_sendmessage(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_sendMessage); + } + + std::string when_internalapi_deletemessage(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_deleteMessage); + } + + std::string when_internalapi_addmembertochat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_addMemberToChat); + } + + std::string when_internalapi_removememberfromchat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_removeMemberFromChat); + } + + std::string when_internalapi_createchat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_createChat); + } + + std::string when_internalapi_removechat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid) { + return when_internalapi(wgd, req, uid, internalapi_removeChat); + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.h b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.h index 3d31417..3cb4e0b 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.h +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.h @@ -48,23 +48,31 @@ namespace iu9cawebchat { /* ======================== API ============================== */ - std::string when_internalapi_pollevents(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_pollevents(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); - std::string when_internalapi_getchatlist(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_getchatlist(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); - std::string when_internalapi_getchatinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_getchatinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); - std::string when_internalapi_getchatmemberlist(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_getchatmemberlist(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); - std::string when_internalapi_getuserinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_getuserinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); - std::string when_internalapi_getmessageinfo(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + std::string when_internalapi_getmessageinfo(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); + + std::string when_internalapi_getmessageneighbours(WorkerGuestData& wgd, const een9::ClientRequest& req, int64_t uid); + + std::string when_internalapi_sendmessage(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); + + std::string when_internalapi_deletemessage(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); + + std::string when_internalapi_addmembertochat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); + + std::string when_internalapi_removememberfromchat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); + + std::string when_internalapi_createchat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); + + std::string when_internalapi_removechat(WorkerGuestData &wgd, const een9::ClientRequest &req, int64_t uid); } #endif diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h index 9dad7f5..1e19a73 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h @@ -48,13 +48,19 @@ namespace iu9cawebchat { /* ============================= API ====================================*/ json::JSON internalapi_pollEvents(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); - json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid); + json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); json::JSON internalapi_getChatInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); json::JSON internalapi_getChatMemberList(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); json::JSON internalapi_getUserInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); json::JSON internalapi_getMessageInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); json::JSON internalapi_getMessageNeighbours(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); - // todo: complete the list + // todo: write implementations of those new cool interfaces + json::JSON internalapi_sendMessage(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); + json::JSON internalapi_deleteMessage(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); + json::JSON internalapi_addMemberToChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); + json::JSON internalapi_removeMemberFromChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); + json::JSON internalapi_createChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); + json::JSON internalapi_removeChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); } #endif diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_addmembertochat.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_addmembertochat.cpp new file mode 100644 index 0000000..b000e19 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_addmembertochat.cpp @@ -0,0 +1,15 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_addMemberToChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["chatId"].g().asInteger().get_int(); + int64_t my_role_here = get_role_of_user_in_chat(conn, uid, chatId); + if (my_role_here == user_chat_role_deleted) + een9_THROW("Unauthorized user tries to access internalapi_getChatInfo"); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_createchat.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_createchat.cpp new file mode 100644 index 0000000..efaea56 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_createchat.cpp @@ -0,0 +1,11 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_createChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_deletemessage.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_deletemessage.cpp new file mode 100644 index 0000000..fed1c2c --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_deletemessage.cpp @@ -0,0 +1,15 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_deleteMessage(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["chatId"].g().asInteger().get_int(); + int64_t my_role_here = get_role_of_user_in_chat(conn, uid, chatId); + if (my_role_here == user_chat_role_deleted) + een9_THROW("Unauthorized user tries to access internalapi_getChatInfo"); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp index 28aba88..f5e13da 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp @@ -2,7 +2,7 @@ #include namespace iu9cawebchat { - json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid) { + json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { json::JSON Recv; Recv["status"] = json::JSON(0l); Recv["chats"] = json::JSON(json::array); diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removechat.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removechat.cpp new file mode 100644 index 0000000..5d00493 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removechat.cpp @@ -0,0 +1,11 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_removeChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removememberfromchat.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removememberfromchat.cpp new file mode 100644 index 0000000..2c452f9 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_removememberfromchat.cpp @@ -0,0 +1,15 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_removeMemberFromChat(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["chatId"].g().asInteger().get_int(); + int64_t my_role_here = get_role_of_user_in_chat(conn, uid, chatId); + if (my_role_here == user_chat_role_deleted) + een9_THROW("Unauthorized user tries to access internalapi_getChatInfo"); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_sendmessage.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_sendmessage.cpp new file mode 100644 index 0000000..1b7b39c --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_sendmessage.cpp @@ -0,0 +1,15 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_sendMessage(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["chatId"].g().asInteger().get_int(); + int64_t my_role_here = get_role_of_user_in_chat(conn, uid, chatId); + if (my_role_here == user_chat_role_deleted) + een9_THROW("Unauthorized user tries to access internalapi_getChatInfo"); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + // todo: WRITE THIS MORBID THING + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/run.cpp b/src/web_chat/iu9_ca_web_chat_lib/run.cpp index f8f77d6..b0a2cd7 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/run.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/run.cpp @@ -96,10 +96,27 @@ namespace iu9cawebchat { if (req.uri_path == "/internalapi/getMessageInfo") { return when_internalapi_getmessageinfo(wgd, req, logged_in_user); } - // if (req.uri_path == "/internalapi/getMessageNeighbours") { - // return when - // } - // todo: write all the other interfaces + if (req.uri_path == "/internalapi/getMessageNeighbours") { + return when_internalapi_getmessageneighbours(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/sendMessage") { + return when_internalapi_sendmessage(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/deleteMessage") { + return when_internalapi_deletemessage(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/addMemberToChat") { + return when_internalapi_addmembertochat(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/removeMemberFromChat") { + return when_internalapi_removememberfromchat(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/createChat") { + return when_internalapi_createchat(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/removeChat") { + return when_internalapi_removechat(wgd, req, logged_in_user); + } } catch (const std::exception& e) { guard_.rollback = true; throw;