From 813ce5add9785a2b30b4c5008d08097d4ea3624b Mon Sep 17 00:00:00 2001 From: Polina Balazhy Date: Sun, 6 Feb 2022 16:32:49 +0200 Subject: [PATCH] avatar colors for message senders --- client/src/request_handlers/get_chat_msgs.c | 8 +++++--- client/src/request_handlers/get_msg.c | 5 +++-- client/src/request_handlers/send_message.c | 4 ++-- client/src/request_handlers/server_updates.c | 6 +++--- server/src/api/handle_get_chat_msgs.c | 9 +++++---- server/src/api/handle_get_msg.c | 3 +-- utils/inc/db_types.h | 1 + utils/inc/utils.h | 5 +++-- utils/src/db_lists/chat_list.c | 4 ++-- utils/src/db_lists/msg_list.c | 10 ++++++---- 10 files changed, 31 insertions(+), 24 deletions(-) diff --git a/client/src/request_handlers/get_chat_msgs.c b/client/src/request_handlers/get_chat_msgs.c index a4e1fb6..2a18c90 100644 --- a/client/src/request_handlers/get_chat_msgs.c +++ b/client/src/request_handlers/get_chat_msgs.c @@ -5,12 +5,14 @@ t_response_code add_msg_to_msglist(cJSON* json) { cJSON* msg_id = cJSON_GetObjectItem(json, "msg_id"); cJSON* sender_id = cJSON_GetObjectItem(json, "sender_id"); cJSON* sender_name = cJSON_GetObjectItemCaseSensitive(json, "sender_name"); + cJSON* sender_color = cJSON_GetObjectItem(json, "sender_color"); cJSON* text = cJSON_GetObjectItemCaseSensitive(json, "text"); cJSON* chat_id = cJSON_GetObjectItem(json, "chat_id"); cJSON* date = cJSON_GetObjectItemCaseSensitive(json, "date"); + if (!cJSON_IsNumber(msg_id) || !cJSON_IsNumber(sender_id) || !cJSON_IsNumber(chat_id) || - !cJSON_IsString(sender_name) || !cJSON_IsString(text) || !cJSON_IsNumber(date)) { + !cJSON_IsString(sender_name) || !cJSON_IsString(text) || !cJSON_IsNumber(date) || !cJSON_IsNumber(sender_color)) { return R_JSON_FAILURE; } @@ -18,8 +20,8 @@ t_response_code add_msg_to_msglist(cJSON* json) { if (!chat_by_id) return R_CHAT_NOENT; - t_msg* new_msg = mx_create_msg(msg_id->valueint, sender_id->valueint, sender_name->valuestring, - chat_id->valueint, text->valuestring, get_string_time(date->valueint)); + t_msg* new_msg = mx_create_msg(msg_id->valueint, sender_id->valueint, sender_name->valuestring, chat_id->valueint, + text->valuestring, get_string_time(date->valueint), sender_color->valueint); // if (new_msg->sender_id != utils->current_user->user_id) // handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); // else diff --git a/client/src/request_handlers/get_msg.c b/client/src/request_handlers/get_msg.c index 8e81dc3..1519386 100644 --- a/client/src/request_handlers/get_msg.c +++ b/client/src/request_handlers/get_msg.c @@ -5,18 +5,19 @@ t_msg* get_msg_from_json(cJSON* json) { cJSON* msg_id = cJSON_GetObjectItem(json, "msg_id"); cJSON* sender_id = cJSON_GetObjectItem(json, "sender_id"); cJSON* sender_name = cJSON_GetObjectItemCaseSensitive(json, "sender_name"); + cJSON* sender_color = cJSON_GetObjectItem(json, "sender_color"); cJSON* text = cJSON_GetObjectItemCaseSensitive(json, "text"); cJSON* chat_id = cJSON_GetObjectItem(json, "chat_id"); cJSON* date = cJSON_GetObjectItemCaseSensitive(json, "date"); if (!cJSON_IsNumber(msg_id) || !cJSON_IsNumber(sender_id) || !cJSON_IsNumber(chat_id) || - !cJSON_IsString(sender_name) || !cJSON_IsString(text) || !cJSON_IsNumber(date)) { + !cJSON_IsString(sender_name) || !cJSON_IsString(text) || !cJSON_IsNumber(date) || !cJSON_IsNumber(sender_color)) { return NULL; } return mx_create_msg(msg_id->valueint, sender_id->valueint, sender_name->valuestring, chat_id->valueint, - text->valuestring, get_string_time(date->valueint)); + text->valuestring, get_string_time(date->valueint), sender_color->valueint); } diff --git a/client/src/request_handlers/send_message.c b/client/src/request_handlers/send_message.c index 7a1a89c..e6803d7 100644 --- a/client/src/request_handlers/send_message.c +++ b/client/src/request_handlers/send_message.c @@ -46,8 +46,8 @@ t_response_code handle_send_msg_request(const char* message_str) { utils->is_suspended = true; unsigned long curr_time = get_current_time(); - t_msg* sent_msg = mx_create_msg(-1, utils->current_user->user_id, utils->current_user->name, - utils->current_chat->id, message_str, get_string_time(curr_time)); + t_msg* sent_msg = mx_create_msg(-1, utils->current_user->user_id, utils->current_user->name, utils->current_chat->id, + message_str, get_string_time(curr_time), utils->current_user->avatar_color); cJSON *json = cJSON_CreateObject(); cJSON_AddNumberToObject(json, "type", REQ_SEND_MESSAGE); diff --git a/client/src/request_handlers/server_updates.c b/client/src/request_handlers/server_updates.c index 814d3fd..7f9eb14 100644 --- a/client/src/request_handlers/server_updates.c +++ b/client/src/request_handlers/server_updates.c @@ -10,7 +10,7 @@ void set_messages_as_read_for(t_chat* chat) { mx_msg_dfl_push_back(&chat->messages, new_msg->message_id, new_msg->sender_id, new_msg->sender_name, - new_msg->chat_id, new_msg->text, new_msg->date_str); + new_msg->chat_id, new_msg->text, new_msg->date_str, new_msg->avatar_color); t_msg* msg_to_add = mx_get_last_msg_node(chat->messages); // msg_to_add->avatar_path = mx_strdup(new_msg->avatar_path); @@ -81,8 +81,8 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current update_chatlist_item_info(curr_chat); char str[200]; - sprintf(str, "This is a t_msg msg:\n\ttext: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s, avatar: %s\n", - new_msg->text, new_msg->chat_id, new_msg->sender_id, new_msg->sender_name, new_msg->date_str, new_msg->avatar_path); + sprintf(str, "This is a t_msg msg:\n\ttext: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s, avatar: %d\n", + new_msg->text, new_msg->chat_id, new_msg->sender_id, new_msg->sender_name, new_msg->date_str, new_msg->avatar_color); client_log(str, INFO_LOG); g_usleep(100000); return 0; diff --git a/server/src/api/handle_get_chat_msgs.c b/server/src/api/handle_get_chat_msgs.c index 04f7783..b24693e 100644 --- a/server/src/api/handle_get_chat_msgs.c +++ b/server/src/api/handle_get_chat_msgs.c @@ -7,9 +7,10 @@ cJSON* get_msg_json(sqlite3_stmt* stmt) { cJSON_AddNumberToObject(json, "msg_id", sqlite3_column_int64(stmt, 0)); cJSON_AddNumberToObject(json, "sender_id", sqlite3_column_int64(stmt, 1)); cJSON_AddStringToObject(json, "sender_name", (const char*)sqlite3_column_text(stmt, 2)); - cJSON_AddNumberToObject(json, "chat_id", sqlite3_column_int64(stmt, 3)); - cJSON_AddStringToObject(json, "text", (const char*)sqlite3_column_text(stmt, 4)); - cJSON_AddNumberToObject(json, "date", sqlite3_column_int64(stmt, 5)); + cJSON_AddNumberToObject(json, "sender_color", sqlite3_column_int(stmt, 3)); + cJSON_AddNumberToObject(json, "chat_id", sqlite3_column_int64(stmt, 4)); + cJSON_AddStringToObject(json, "text", (const char*)sqlite3_column_text(stmt, 5)); + cJSON_AddNumberToObject(json, "date", sqlite3_column_int64(stmt, 6)); return json; } @@ -20,7 +21,7 @@ cJSON* get_msgs_array_json(int chat_id) { cJSON* chats_json = cJSON_CreateArray(); sqlite3* db = open_database(); sqlite3_stmt* stmt; - sqlite3_prepare_v2(db, "SELECT messages.id, messages.user_id, users.username, messages.chat_id, messages.text, messages.date " + sqlite3_prepare_v2(db, "SELECT messages.id, messages.user_id, users.username, users.avatar_color, messages.chat_id, messages.text, messages.date " "FROM `messages` INNER JOIN `users` ON users.id = messages.user_id " "WHERE messages.chat_id = ?", -1, &stmt, NULL); diff --git a/server/src/api/handle_get_msg.c b/server/src/api/handle_get_msg.c index 217e71a..de4caeb 100644 --- a/server/src/api/handle_get_msg.c +++ b/server/src/api/handle_get_msg.c @@ -10,7 +10,7 @@ static cJSON* get_msg_by_id(const cJSON* msg_info) { } sqlite3* db = open_database(); sqlite3_stmt* stmt; - sqlite3_prepare_v2(db, "SELECT messages.id, messages.user_id, users.username, messages.chat_id, messages.text, messages.date " + sqlite3_prepare_v2(db, "SELECT messages.id, messages.user_id, users.username, users.avatar_color, messages.chat_id, messages.text, messages.date " "FROM `messages` INNER JOIN `users` ON users.id = messages.user_id " "WHERE messages.chat_id = ? AND messages.id = ?", -1, &stmt, NULL); @@ -48,7 +48,6 @@ void handle_get_msg(const cJSON* msg_info, t_server_utils* utils) { cJSON_AddItemReferenceToObject(json, "message", msg_json); cJSON_AddNumberToObject(json, "type", REQ_GET_MSG); cJSON_AddNumberToObject(json, "error_code", R_SUCCESS); - char* json_str = cJSON_PrintUnformatted(json); send_response_to(utils->ssl, json_str); diff --git a/utils/inc/db_types.h b/utils/inc/db_types.h index 095109f..e59ad1c 100644 --- a/utils/inc/db_types.h +++ b/utils/inc/db_types.h @@ -19,6 +19,7 @@ typedef struct s_msg { char* date_str; char* text; char* avatar_path; + t_avatar_color avatar_color; struct s_msg* next; } t_msg; diff --git a/utils/inc/utils.h b/utils/inc/utils.h index 1648412..2605bd2 100644 --- a/utils/inc/utils.h +++ b/utils/inc/utils.h @@ -86,6 +86,7 @@ static const t_response response_objs[] = { { R_DB_FAILURE, "A database error occurred when handling the request" }, { R_JSON_FAILURE, "A json error occurred when handling the request" }, { R_INVALID_INPUT, "The input was invalid" }, + { R_FILE_ERROR, "A file error occured" }, { R_USR_EXISTS, "The user with this name already exists" }, { R_USR_NOENT, "There's no user by that name" }, { R_INVALID_PASS, "The entered password is incorrect" }, @@ -121,8 +122,8 @@ int mx_chat_list_size(t_chat* list); void mx_print_chat_list(t_chat* chat); // remove -t_msg* mx_create_msg(int msg_id, int user_id, const char* user_name, int chat_id, const char* text, const char* date_str); -void mx_msg_dfl_push_back(t_msg** list, int msg_id, int user_id, const char* user_name, int chat_id, const char* text, const char* date_str); +t_msg* mx_create_msg(int msg_id, int user_id, const char* user_name, int chat_id, const char* text, const char* date_str, t_avatar_color color); +void mx_msg_dfl_push_back(t_msg** list, int msg_id, int user_id, const char* user_name, int chat_id, const char* text, const char* date_str, t_avatar_color color); void mx_msg_push_back(t_msg** list, t_msg* new_node); void mx_clear_msg_list(t_msg **list); void mx_msg_pop_index(t_msg **list, int index); diff --git a/utils/src/db_lists/chat_list.c b/utils/src/db_lists/chat_list.c index 911e184..521f1aa 100644 --- a/utils/src/db_lists/chat_list.c +++ b/utils/src/db_lists/chat_list.c @@ -214,8 +214,8 @@ void mx_print_chat_list(t_chat* chat) { while (msg) { char str[200]; - sprintf(str, "Chat message:\n\tid: %d, text: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s, avatar: %s\n", - msg->message_id, msg->text, msg->chat_id, msg->sender_id, msg->sender_name, msg->date_str, msg->avatar_path); + sprintf(str, "Chat message:\n\tid: %d, text: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s, avatar: %d\n", + msg->message_id, msg->text, msg->chat_id, msg->sender_id, msg->sender_name, msg->date_str, msg->avatar_color); logger(str, INFO_LOG); msg = msg->next; diff --git a/utils/src/db_lists/msg_list.c b/utils/src/db_lists/msg_list.c index f6fa86c..d0105c8 100644 --- a/utils/src/db_lists/msg_list.c +++ b/utils/src/db_lists/msg_list.c @@ -1,6 +1,7 @@ #include "../../inc/utils.h" -t_msg* mx_create_msg(int msg_id, int user_id, const char* user_name, int chat_id, const char* text, const char* date_str) { +t_msg* mx_create_msg(int msg_id, int user_id, const char* user_name, int chat_id, + const char* text, const char* date_str, t_avatar_color color) { t_msg *new_node = malloc(sizeof(t_msg)); @@ -10,6 +11,7 @@ t_msg* mx_create_msg(int msg_id, int user_id, const char* user_name, int chat_id new_node->sender_name = mx_strdup(user_name); new_node->text = mx_strdup(text); new_node->date_str = mx_strdup(date_str); + new_node->avatar_color = color; new_node->avatar_path = NULL; new_node->next = NULL; @@ -35,10 +37,10 @@ void mx_msg_push_back(t_msg** list, t_msg* new_node) { } -void mx_msg_dfl_push_back(t_msg** list, int msg_id, int user_id, const char* user_name, - int chat_id, const char* text, const char* date_str) { +void mx_msg_dfl_push_back(t_msg** list, int msg_id, int user_id, const char* user_name, int chat_id, + const char* text, const char* date_str, t_avatar_color color) { - t_msg* new_node = mx_create_msg(msg_id, user_id, user_name, chat_id, text, date_str); + t_msg* new_node = mx_create_msg(msg_id, user_id, user_name, chat_id, text, date_str, color); mx_msg_push_back(list, new_node); }