Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.ucode.world:connect-khpi/connect-ucha…
Browse files Browse the repository at this point in the history
…t/pbalazhy-2
  • Loading branch information
YaroslavChuiko committed Feb 6, 2022
2 parents 1b35a53 + 813ce5a commit c05eb04
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 24 deletions.
8 changes: 5 additions & 3 deletions client/src/request_handlers/get_chat_msgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ 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;
}
t_chat* chat_by_id = mx_get_chat_by_id(utils->chatlist, chat_id->valueint);
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
Expand Down
5 changes: 3 additions & 2 deletions client/src/request_handlers/get_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
4 changes: 2 additions & 2 deletions client/src/request_handlers/send_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions client/src/request_handlers/server_updates.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions server/src/api/handle_get_chat_msgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions server/src/api/handle_get_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions utils/inc/db_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions utils/inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions utils/src/db_lists/chat_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 6 additions & 4 deletions utils/src/db_lists/msg_list.c
Original file line number Diff line number Diff line change
@@ -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));

Expand All @@ -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;

Expand All @@ -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);

}
Expand Down

0 comments on commit c05eb04

Please sign in to comment.