diff --git a/building/main.cpp b/building/main.cpp index a956e9f..128ee14 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -148,11 +148,17 @@ struct CAWebChat { "sqlite3_wrapper.cpp", "login_cookie.cpp", "backend_logic/server_data_interact.cpp", - "backend_logic/when_login.cpp", + "backend_logic/client_server_interact.cpp", "backend_logic/when_list_rooms.cpp", - "backend_logic/when_internalapi_pollevents.cpp", - "backend_logic/when_internalapi_getchatlist.cpp", - "backend_logic/when_internalapi_getchatinfo.cpp", + "backend_logic/when_login.cpp", + "backend_logic/when_chat.cpp", + "backend_logic/when_user.cpp", + "backend_logic/when_api_pollevents.cpp", + "backend_logic/when_api_getchatlist.cpp", + "backend_logic/when_api_getchatinfo.cpp", + "backend_logic/when_api_getchatmemberlist.cpp", + "backend_logic/when_api_getuserinfo.cpp", + "backend_logic/when_api_getmessageinfo.cpp", }; for (std::string& u: T.units) u = "web_chat/iu9_ca_web_chat_lib/" + u; diff --git a/src/http_server/engine_engine_number_9/http_structures/cookies.cpp b/src/http_server/engine_engine_number_9/http_structures/cookies.cpp index 85ef6b9..dee5ba4 100644 --- a/src/http_server/engine_engine_number_9/http_structures/cookies.cpp +++ b/src/http_server/engine_engine_number_9/http_structures/cookies.cpp @@ -95,9 +95,7 @@ namespace een9 { result.reserve(result.size() + new_cookies.size()); result.insert(result.end(), new_cookies.begin(), new_cookies.end()); } - } catch (const std::exception& e) { - printf("!!!findAllClientCookies failure\n"); - } + } catch (const std::exception& e) {} } return result; } @@ -106,7 +104,7 @@ namespace een9 { std::vector>& res_header_lines) { for (const std::pair& cookie : new_cookies) { ASSERT_pl(isCookieName(cookie.first) && isCookieValue(cookie.second)); - res_header_lines.emplace_back("Set-Cookie", cookie.first + "=\"" + cookie.second + "\";SameSite=Strict;Path=/"); + res_header_lines.emplace_back("Set-Cookie", cookie.first + "=" + cookie.second + ";SameSite=Strict;Path=/"); } } } diff --git a/src/http_server/engine_engine_number_9/http_structures/response_gen.cpp b/src/http_server/engine_engine_number_9/http_structures/response_gen.cpp index a1be045..5b9e4aa 100644 --- a/src/http_server/engine_engine_number_9/http_structures/response_gen.cpp +++ b/src/http_server/engine_engine_number_9/http_structures/response_gen.cpp @@ -41,14 +41,14 @@ namespace een9 { }, body); } - std::string form_http_server_response_307(const std::string& Location) { - return form_http_server_response_header_only("307", {{"Location", Location}}); + std::string form_http_server_response_303(const std::string& Location) { + return form_http_server_response_header_only("303", {{"Location", Location}}); } - std::string form_http_server_response_307_spec_head(const std::string &Location, + std::string form_http_server_response_303_spec_head(const std::string &Location, const std::vector>& headers) { std::vector> cp = headers; cp.emplace_back("Location", Location); - return form_http_server_response_header_only("307", cp); + return form_http_server_response_header_only("303", cp); } } diff --git a/src/http_server/engine_engine_number_9/http_structures/response_gen.h b/src/http_server/engine_engine_number_9/http_structures/response_gen.h index 0e9034f..da78bcb 100644 --- a/src/http_server/engine_engine_number_9/http_structures/response_gen.h +++ b/src/http_server/engine_engine_number_9/http_structures/response_gen.h @@ -20,9 +20,9 @@ namespace een9 { std::string form_http_server_response_404(const std::string& Content_Type, const std::string& body); - std::string form_http_server_response_307(const std::string& Location); + std::string form_http_server_response_303(const std::string& Location); - std::string form_http_server_response_307_spec_head(const std::string &Location, + std::string form_http_server_response_303_spec_head(const std::string &Location, const std::vector>& headers); } 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 new file mode 100644 index 0000000..5ca51d3 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.cpp @@ -0,0 +1,78 @@ +#include "client_server_interact.h" +#include + +namespace iu9cawebchat { + void initial_extraction_of_all_the_useful_info_from_cookies( + SqliteConnection& conn, const een9::ClientRequest& req, + std::vector> &ret_cookies, + std::vector &ret_login_cookies, + json::JSON &ret_userinfo, + int64_t& ret_logged_in_user + ) { + ret_cookies = een9::findAllClientCookies(req.headers); + ret_login_cookies = select_login_cookies(ret_cookies); + ret_logged_in_user = -1; /* Negative means that user is not logged in */ + if (!ret_login_cookies.empty()){ + size_t oldest_ind = select_oldest_login_cookie(ret_login_cookies); + LoginCookie& tried = ret_login_cookies[oldest_ind]; + ret_logged_in_user = find_user_by_credentials(conn, tried.nickname, tried.password); + if (ret_logged_in_user >= 0) { + ret_userinfo["uid"] = json::JSON(ret_logged_in_user); + ret_userinfo["nickname"] = json::JSON(tried.nickname); + ret_userinfo["name"] = json::JSON(find_user_name(conn, ret_logged_in_user)); + } + } + } + + std::string RTEE(const std::string& el_name, + const json::JSON& config_presentation, WorkerGuestData& wgd, + const json::JSON& userinfo) { + std::string page = wgd.templater->render(el_name, {&config_presentation, &userinfo}); + return een9::form_http_server_response_200("text/html", page); + } + + /* ========================= API =========================*/ + + + std::string when_internalapi_pollevents(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_pollEvents(*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_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_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_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_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); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..3d31417 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/client_server_interact.h @@ -0,0 +1,70 @@ +#ifndef IU9_CA_WEB_CHAT_LIB_BACKEND_LOGIC_SERVER_DATA_INTERACT_PAGES_H +#define IU9_CA_WEB_CHAT_LIB_BACKEND_LOGIC_SERVER_DATA_INTERACT_PAGES_H + +#include "server_data_interact.h" + +#include "../sqlite3_wrapper.h" +#include +#include +#include "../login_cookie.h" + +#include +#include +#include + +namespace iu9cawebchat { + struct WorkerGuestData { + /* Because templaters use libjsonincpp, they can't be READ by two thread simultaneously */ + std::unique_ptr templater; + std::unique_ptr db; + }; + + void initial_extraction_of_all_the_useful_info_from_cookies( + SqliteConnection& conn, const een9::ClientRequest& req, + std::vector>& ret_cookies, + std::vector& ret_login_cookies, + json::JSON& ret_userinfo, + int64_t& ret_logged_in_user + ); + + std::string RTEE(const std::string& el_name, + const json::JSON& config_presentation, WorkerGuestData& wgd, + const json::JSON& userinfo); + + /* ========================== PAGES ================================== */ + + std::string when_page_list_rooms(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const json::JSON& userinfo); + + std::string when_page_login(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo); + + std::string when_page_chat(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const json::JSON& userinfo); + + std::string when_page_user(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const json::JSON& userinfo); + + + /* ======================== API ============================== */ + + 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_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_getuserinfo(WorkerGuestData& wgd, + const een9::ClientRequest& req, int64_t uid); + + std::string when_internalapi_getmessageinfo(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.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.cpp index f4a604f..75cfd5a 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.cpp @@ -38,7 +38,7 @@ namespace iu9cawebchat { een9_ASSERT_pl(name_col.exist); return name_col.value; } - return ""; + een9_THROW("No such user"); } RowUser_Content lookup_user_content(SqliteConnection &conn, int64_t uid) { @@ -52,14 +52,14 @@ namespace iu9cawebchat { if (status == SQLITE_ROW) { return {std::move(nickname_col.value), std::move(name_col.value)}; } - return {}; + een9_THROW("No such user"); } - RowChat_Content lookup_chat_content(SqliteConnection &conn, int64_t uid) { - een9_ASSERT(uid >= 0, "Are you crazy?"); + RowChat_Content lookup_chat_content(SqliteConnection &conn, int64_t chatId) { + een9_ASSERT(chatId >= 0, "Are you crazy?"); SqliteStatement sql_req(conn, "SELECT `nickname`, `name`, `lastMsgId` FROM `chat` WHERE `id` = ?1", - {{1, uid}}, {}); + {{1, chatId}}, {}); fsql_text8_or_null nickname_col; fsql_text8_or_null name_col; fsql_integer_or_null last_msg_id_col; @@ -68,29 +68,22 @@ namespace iu9cawebchat { return {std::move(nickname_col.value), std::move(name_col.value), last_msg_id_col.exist ? last_msg_id_col.value : -1}; } - return {}; + een9_THROW("No such chat"); } - void initial_extraction_of_all_the_useful_info_from_cookies( - SqliteConnection& conn, const een9::ClientRequest& req, - std::vector> &ret_cookies, - std::vector &ret_login_cookies, - json::JSON &ret_userinfo, - int64_t& ret_logged_in_user - ) { - ret_cookies = een9::findAllClientCookies(req.headers); - ret_login_cookies = select_login_cookies(ret_cookies); - ret_logged_in_user = -1; /* Negative means that user is not logged in */ - if (!ret_login_cookies.empty()){ - size_t oldest_ind = select_oldest_login_cookie(ret_login_cookies); - LoginCookie& tried = ret_login_cookies[oldest_ind]; - ret_logged_in_user = find_user_by_credentials(conn, tried.nickname, tried.password); - if (ret_logged_in_user >= 0) { - ret_userinfo["uid"] = json::JSON(ret_logged_in_user); - ret_userinfo["nickname"] = json::JSON(tried.nickname); - ret_userinfo["name"] = json::JSON(find_user_name(conn, ret_logged_in_user)); - } + RowMessage_Content lookup_message_content(SqliteConnection& conn, int64_t chatId, int64_t msgId) { + SqliteStatement req(conn, + "SELECT `previousId`, `senderUserId`, `exists`, `isSystem`, `text` FROM `message` WHERE " + "`chatId` = ?1 AND `id` = ?2", {{1, chatId}, {2, msgId}}, {}); + fsql_integer_or_null previousId, senderUserId, exists, isSystem; + fsql_text8_or_null msg_text; + int status = sqlite_stmt_step(req, {{0, &previousId}, {1, &senderUserId}, {2, &exists}, {3, &isSystem}}, + {{4, &msg_text}}); + if (status == SQLITE_ROW) { + return {(bool)isSystem.value, msg_text.value, senderUserId.exist ? senderUserId.value : -1, + previousId.exist ? previousId.value : -1}; } + een9_THROW("No such message"); } int64_t get_role_of_user_in_chat(SqliteConnection& conn, int64_t userId, int64_t chatId) { @@ -105,10 +98,5 @@ namespace iu9cawebchat { return user_chat_role_deleted; } - std::string RTEE(const std::string& el_name, - const json::JSON& config_presentation, WorkerGuestData& wgd, - const json::JSON& userinfo) { - std::string page = wgd.templater->render(el_name, {&config_presentation, &userinfo}); - return een9::form_http_server_response_200("text/html", page); - } + /* All the api calls processing is done in dedicated files */ } 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 f39a58f..2f2bdfa 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 @@ -4,12 +4,7 @@ /* This folder covers all code that helps to interact with database and templater, * or dictates the logic of how this web chat functions */ -#include -#include #include "../sqlite3_wrapper.h" -#include "../login_cookie.h" -#include -#include #include namespace iu9cawebchat { @@ -20,13 +15,6 @@ namespace iu9cawebchat { const char* stringify_user_chat_role(int64_t role); - struct WorkerGuestData { - /* Because templaters use libjsonincpp, they can't be READ by two thread simultaneously */ - std::unique_ptr templater; - std::unique_ptr db; - int debug_trans_op; // todo: delete when debug is over - }; - int64_t find_user_by_credentials (SqliteConnection& conn, const std::string& nickname, const std::string& password); std::string find_user_name (SqliteConnection& conn, int64_t uid); @@ -42,40 +30,27 @@ namespace iu9cawebchat { }; RowUser_Content lookup_user_content(SqliteConnection& conn, int64_t uid); - RowChat_Content lookup_chat_content(SqliteConnection& conn, int64_t uid); + RowChat_Content lookup_chat_content(SqliteConnection& conn, int64_t chatId); + struct RowMessage_Content { + bool isSystem; + std::string text; + int64_t senderUserId; + int64_t previous = -1; + }; - void initial_extraction_of_all_the_useful_info_from_cookies( - SqliteConnection& conn, const een9::ClientRequest& req, - std::vector>& ret_cookies, - std::vector& ret_login_cookies, - json::JSON& ret_userinfo, - int64_t& ret_logged_in_user - ); + RowMessage_Content lookup_message_content(SqliteConnection& conn, int64_t chatId, int64_t msgId); int64_t get_role_of_user_in_chat(SqliteConnection& conn, int64_t userId, int64_t chatId); - std::string RTEE(const std::string& el_name, - const json::JSON& config_presentation, WorkerGuestData& wgd, - const json::JSON& userinfo); - - /* ========================== PAGES ================================== */ - std::string when_page_login(WorkerGuestData& wgd, const json::JSON& config_presentation, - const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo); - - std::string when_page_list_rooms(WorkerGuestData& wgd, const json::JSON& config_presentation, - const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo); - + /* ============================= API ====================================*/ json::JSON internalapi_pollEvents(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); - - std::string when_internalapi_pollevents(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); - - json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid); - - std::string when_internalapi_getchatlist(WorkerGuestData& wgd, - const een9::ClientRequest& req, int64_t uid); + 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); + // todo: complete the list } #endif diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatinfo.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatinfo.cpp new file mode 100644 index 0000000..e60939b --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatinfo.cpp @@ -0,0 +1,19 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_getChatInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["id"].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); + RowChat_Content content = lookup_chat_content(conn, chatId); + Recv["name"] = json::JSON(content.name); + Recv["nickname"] = json::JSON(content.nickname); + Recv["lastMsgId"] = json::JSON(content.lastMsgId); + Recv["roleHere"] = json::JSON(stringify_user_chat_role(my_role_here)); + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatlist.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp similarity index 77% rename from src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatlist.cpp rename to src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp index ff441fe..28aba88 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatlist.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatlist.cpp @@ -1,7 +1,5 @@ #include "server_data_interact.h" -#include #include -#include "../str_fields.h" namespace iu9cawebchat { json::JSON internalapi_getChatList(SqliteConnection& conn, int64_t uid) { @@ -32,11 +30,4 @@ namespace iu9cawebchat { } return Recv; } - - 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); - } } diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatmemberlist.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatmemberlist.cpp new file mode 100644 index 0000000..78f39b3 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getchatmemberlist.cpp @@ -0,0 +1,36 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + json::JSON internalapi_getChatMemberList(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["id"].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); + Recv["members"] = json::JSON(json::array); + std::vector& members = Recv["members"].g().asArray(); + SqliteStatement req(conn, + "SELECT `user`.`id`, `user`.`nickname`, `user`.`name`, `user_chat_membership`.`role` FROM " + "`user` RIGHT JOIN `user_chat_membership` ON `user`.`id` = `user_chat_membership`.`userId` " + "WHERE `user_chat_membership`.`chatId` = ?1", + {{1, chatId}}, {}); + while (true) { + fsql_integer_or_null this_user_id; + fsql_text8_or_null this_user_nickname, this_user_name; + fsql_integer_or_null this_users_role; + int status = sqlite_stmt_step(req, {{0, &this_user_id}, {3, &this_users_role}}, + {{1, &this_user_nickname}, {2, &this_user_name}}); + if (status != SQLITE_ROW) + break; + members.emplace_back(); + json::JSON& member = members.back(); + member["id"] = json::JSON(this_user_id.value); + member["content"]["nickname"] = json::JSON(this_user_nickname.value); + member["content"]["name"] = json::JSON(this_user_name.value); + member["content"]["role"] = json::JSON(this_users_role.value); + } + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getmessageinfo.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getmessageinfo.cpp new file mode 100644 index 0000000..3a71a53 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getmessageinfo.cpp @@ -0,0 +1,21 @@ +#include "server_data_interact.h" +#include + +namespace iu9cawebchat { + /* This is literally the most dumb and useless query */ + json::JSON internalapi_getMessageInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t chatId = Sent["chatId"].g().asInteger().get_int(); + int64_t msgId = Sent["id"].g().asInteger().get_int(); + if (get_role_of_user_in_chat(conn, uid, chatId) == user_chat_role_deleted) + een9_THROW("Authentication failure"); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + RowMessage_Content content = lookup_message_content(conn, chatId, msgId); + Recv["text"] = json::JSON(content.text); + Recv["isSystem"] = json::JSON(content.isSystem); + Recv["sender"] = json::JSON(content.senderUserId); + // todo: sync that addition with api documentation + Recv["previous"] = json::JSON(content.previous); + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getuserinfo.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getuserinfo.cpp new file mode 100644 index 0000000..71816d8 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_getuserinfo.cpp @@ -0,0 +1,13 @@ +#include "server_data_interact.h" + +namespace iu9cawebchat { + json::JSON internalapi_getUserInfo(SqliteConnection& conn, int64_t uid, const json::JSON& Sent) { + int64_t otherUserId = Sent["id"].g().asInteger().get_int(); + json::JSON Recv; + Recv["status"] = json::JSON(0l); + RowUser_Content content = lookup_user_content(conn, otherUserId); + Recv["name"] = json::JSON(content.name); + Recv["nickname"] = json::JSON(content.nickname); + return Recv; + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_pollevents.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_pollevents.cpp similarity index 91% rename from src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_pollevents.cpp rename to src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_pollevents.cpp index b6af302..c803860 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_pollevents.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_api_pollevents.cpp @@ -1,8 +1,5 @@ #include "server_data_interact.h" -#include #include -#include "../str_fields.h" -#include namespace iu9cawebchat { int64_t get_current_history_id_of_chat(SqliteConnection& conn, int64_t chatId) { @@ -116,7 +113,7 @@ namespace iu9cawebchat { if (hist_entity_request["type"].g().asString() == "chat") { int64_t chatId = hist_entity_request["chatId"].g().asInteger().get_int(); if (get_role_of_user_in_chat(conn, uid, chatId) == user_chat_role_deleted) - een9_THROW("internalapi: trying to access chat that user does not belong to"); + een9_THROW("internalapi/pollEvents: trying to access chat that user does not belong to"); hist_entity_response["chatId"] = json::JSON(chatId); hist_entity_response["HistoryId"] = json::JSON(get_current_history_id_of_chat(conn, chatId)); /* Two classes of 'real events' can happen to chat: membership table change, message table change */ @@ -132,11 +129,4 @@ namespace iu9cawebchat { } return Recv; } - - std::string when_internalapi_pollevents(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_pollEvents(*wgd.db, uid, Sent), json::print_pretty); - return een9::form_http_server_response_200("text/json", result); - } } diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_chat.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_chat.cpp new file mode 100644 index 0000000..24cd323 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_chat.cpp @@ -0,0 +1,8 @@ +#include "client_server_interact.h" + +namespace iu9cawebchat { + std::string when_page_chat(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const json::JSON& userinfo) { + return RTEE("chat", config_presentation, wgd, userinfo); + } +} \ No newline at end of file diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp deleted file mode 100644 index 23556d7..0000000 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "server_data_interact.h" - -namespace iu9cawebchat { - // todo -} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp index c3b4673..93d4902 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp @@ -1,17 +1,10 @@ -#include "server_data_interact.h" +#include "client_server_interact.h" namespace iu9cawebchat { std::string when_page_list_rooms(WorkerGuestData& wgd, const json::JSON& config_presentation, - const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo) { + const een9::ClientRequest& req, const json::JSON& userinfo) { if (userinfo.isNull()) { - printf("Somebody entered /list-room with %s without being logged in\n", req.method.c_str()); - if (!login_cookies.empty()) { - printf("Login cookies: \n"); - for (auto& c: login_cookies) { - printf("%s as %s\n", c.nickname.c_str(), c.password.c_str()); - } - } - return een9::form_http_server_response_307("/login"); + return een9::form_http_server_response_303("/login"); } return RTEE("list-rooms", config_presentation, wgd, userinfo); } diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_login.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_login.cpp index d69516f..83dddf1 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_login.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_login.cpp @@ -1,4 +1,4 @@ -#include "server_data_interact.h" +#include "client_server_interact.h" #include #include #include "../str_fields.h" @@ -31,7 +31,7 @@ namespace iu9cawebchat { std::vector> response_hlines; LoginCookie new_login_cookie = create_login_cookie(nickname, password); add_set_cookie_headers_to_login(login_cookies, response_hlines, new_login_cookie); - return een9::form_http_server_response_307_spec_head("/", response_hlines); + return een9::form_http_server_response_303_spec_head("/", response_hlines); } return RTEE("login", config_presentation, wgd, userinfo); } diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_user.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_user.cpp new file mode 100644 index 0000000..532a9f0 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_user.cpp @@ -0,0 +1,8 @@ +#include "client_server_interact.h" + +namespace iu9cawebchat { + std::string when_page_user(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const json::JSON& userinfo) { + return RTEE("profile", config_presentation, wgd, userinfo); + } +} 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 3d4d2ab..1863a21 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/run.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/run.cpp @@ -1,4 +1,5 @@ #include "actions.h" + #include #include #include @@ -6,24 +7,7 @@ #include #include #include "str_fields.h" - -// #include -// #include -// #include -// #include -// #include -// #include -// #include -// #include -// #include -// #include "sqlite3_wrapper.h" -// #include "str_fields.h" -// #include "find_db.h" -// #include "login_cookie.h" -// #include -// #include - -#include "backend_logic/server_data_interact.h" +#include "backend_logic/client_server_interact.h" namespace iu9cawebchat { bool termination = false; @@ -83,28 +67,36 @@ namespace iu9cawebchat { initial_extraction_of_all_the_useful_info_from_cookies(*wgd.db, req, cookies, login_cookies, userinfo, logged_in_user); if (req.uri_path == "/" || req.uri_path == "/list-rooms") { - return when_page_list_rooms(wgd, config_presentation, req, login_cookies, userinfo); + return when_page_list_rooms(wgd, config_presentation, req, userinfo); } if (req.uri_path == "/login") { return when_page_login(wgd, config_presentation, req, login_cookies, userinfo); } - if (req.uri_path == "/chat") { - // todo: write it actually - return RTEE("chat", config_presentation, wgd, userinfo); + if (een9::beginsWith(req.uri_path, "/chat")) { + return when_page_chat(wgd, config_presentation, req, userinfo); } - if (req.uri_path == "/profile") { - // todo: write it actually - return RTEE("profile", config_presentation, wgd, userinfo); + if (req.uri_path == "/user") { + return when_page_user(wgd, config_presentation, req, userinfo); } - // if (req.uri_path == "/registration") { - // RTEE("registration", config_presentation, wgd, userinfo); - // } if (req.uri_path == "/internalapi/pollEvents") { return when_internalapi_pollevents(wgd, req, logged_in_user); } if (req.uri_path == "/internalapi/getChatList") { return when_internalapi_getchatlist(wgd, req, logged_in_user); } + if (req.uri_path == "/internalapi/getChatInfo") { + return when_internalapi_getchatinfo(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/getChatMemberList") { + return when_internalapi_getchatmemberlist(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/getUserInfo") { + return when_internalapi_getuserinfo(wgd, req, logged_in_user); + } + if (req.uri_path == "/internalapi/getMessageInfo") { + return when_internalapi_getmessageinfo(wgd, req, logged_in_user); + } + // todo: write all the other interfaces } catch (const std::exception& e) { guard_.rollback = true; throw;