now redirections use 303, small refactoring, added skeleton for response logic, API responses up to getMessageInfo
This commit is contained in:
parent
90f8289bcd
commit
785cbff2a7
@ -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;
|
||||
|
@ -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<std::pair<std::string, std::string>>& res_header_lines) {
|
||||
for (const std::pair<std::string, std::string>& 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=/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<std::pair<std::string, std::string>>& headers) {
|
||||
std::vector<std::pair<std::string, std::string>> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<std::pair<std::string, std::string>>& headers);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,78 @@
|
||||
#include "client_server_interact.h"
|
||||
#include <engine_engine_number_9/http_structures/cookies.h>
|
||||
|
||||
namespace iu9cawebchat {
|
||||
void initial_extraction_of_all_the_useful_info_from_cookies(
|
||||
SqliteConnection& conn, const een9::ClientRequest& req,
|
||||
std::vector<std::pair<std::string, std::string>> &ret_cookies,
|
||||
std::vector<LoginCookie> &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);
|
||||
}
|
||||
}
|
@ -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 <engine_engine_number_9/http_structures/client_request.h>
|
||||
#include <engine_engine_number_9/http_structures/response_gen.h>
|
||||
#include "../login_cookie.h"
|
||||
|
||||
#include <jsonincpp/jsonobj.h>
|
||||
#include <new_york_transit_line/templater.h>
|
||||
#include <memory>
|
||||
|
||||
namespace iu9cawebchat {
|
||||
struct WorkerGuestData {
|
||||
/* Because templaters use libjsonincpp, they can't be READ by two thread simultaneously */
|
||||
std::unique_ptr<nytl::Templater> templater;
|
||||
std::unique_ptr<SqliteConnection> db;
|
||||
};
|
||||
|
||||
void initial_extraction_of_all_the_useful_info_from_cookies(
|
||||
SqliteConnection& conn, const een9::ClientRequest& req,
|
||||
std::vector<std::pair<std::string, std::string>>& ret_cookies,
|
||||
std::vector<LoginCookie>& 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<LoginCookie>& 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
|
@ -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<std::pair<std::string, std::string>> &ret_cookies,
|
||||
std::vector<LoginCookie> &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 */
|
||||
}
|
||||
|
@ -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 <memory>
|
||||
#include <new_york_transit_line/templater.h>
|
||||
#include "../sqlite3_wrapper.h"
|
||||
#include "../login_cookie.h"
|
||||
#include <engine_engine_number_9/http_structures/client_request.h>
|
||||
#include <engine_engine_number_9/http_structures/response_gen.h>
|
||||
#include <jsonincpp/string_representation.h>
|
||||
|
||||
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<nytl::Templater> templater;
|
||||
std::unique_ptr<SqliteConnection> 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<std::pair<std::string, std::string>>& ret_cookies,
|
||||
std::vector<LoginCookie>& 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<LoginCookie>& 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<LoginCookie>& 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
|
||||
|
@ -0,0 +1,19 @@
|
||||
#include "server_data_interact.h"
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
#include "server_data_interact.h"
|
||||
#include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
#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);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
#include "server_data_interact.h"
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
|
||||
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<json::JSON>& 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#include "server_data_interact.h"
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,8 +1,5 @@
|
||||
#include "server_data_interact.h"
|
||||
#include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
#include "../str_fields.h"
|
||||
#include <engine_engine_number_9/http_structures/response_gen.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#include "server_data_interact.h"
|
||||
|
||||
namespace iu9cawebchat {
|
||||
// todo
|
||||
}
|
@ -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<LoginCookie>& 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);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "server_data_interact.h"
|
||||
#include "client_server_interact.h"
|
||||
#include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
#include "../str_fields.h"
|
||||
@ -31,7 +31,7 @@ namespace iu9cawebchat {
|
||||
std::vector<std::pair<std::string, std::string>> 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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include "actions.h"
|
||||
|
||||
#include <engine_engine_number_9/baza_throw.h>
|
||||
#include <engine_engine_number_9/os_utils.h>
|
||||
#include <engine_engine_number_9/connecting_assets/static_asset_manager.h>
|
||||
@ -6,24 +7,7 @@
|
||||
#include <engine_engine_number_9/running_mainloop.h>
|
||||
#include <signal.h>
|
||||
#include "str_fields.h"
|
||||
|
||||
// #include <engine_engine_number_9/baza_throw.h>
|
||||
// #include <engine_engine_number_9/running_mainloop.h>
|
||||
// #include <engine_engine_number_9/http_structures/response_gen.h>
|
||||
// #include <engine_engine_number_9/connecting_assets/static_asset_manager.h>
|
||||
// #include <assert.h>
|
||||
// #include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
|
||||
// #include <new_york_transit_line/templater.h>
|
||||
// #include <sqlite3.h>
|
||||
// #include <engine_engine_number_9/socket_address.h>
|
||||
// #include "sqlite3_wrapper.h"
|
||||
// #include "str_fields.h"
|
||||
// #include "find_db.h"
|
||||
// #include "login_cookie.h"
|
||||
// #include <engine_engine_number_9/http_structures/cookies.h>
|
||||
// #include <jsonincpp/string_representation.h>
|
||||
|
||||
#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;
|
||||
|
Loading…
Reference in New Issue
Block a user