Skip to content

Commit

Permalink
avatar colors added for chats
Browse files Browse the repository at this point in the history
  • Loading branch information
High-Voltaged committed Feb 5, 2022
1 parent ed5f90f commit 545e4e0
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 128 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $(LIBMX):
# make -sC $(LIBMX_DIR)

$(UTILSLIB):
# make -sC $(UTILSLIB_DIR)
make -sC $(UTILSLIB_DIR)

$(CJSON):
# make -sC $(CJSON_DIR)
Expand All @@ -35,7 +35,7 @@ $(SQLITE):
# make -sC $(SQLITE_DIR)

$(SERVER):
# make -sC $(SERVER_DIR)
make -sC $(SERVER_DIR)

$(CLIENT):
make -sC $(CLIENT_DIR)
Expand All @@ -48,9 +48,9 @@ clean:
rm -df $(OBJDIR)

uninstall:
# make -sC $(SERVER_DIR) $@
make -sC $(SERVER_DIR) $@
make -sC $(CLIENT_DIR) $@
# make -sC $(UTILSLIB_DIR) $@
make -sC $(UTILSLIB_DIR) $@
# make -sC $(LIBMX_DIR) $@
# make -sC $(CJSON_DIR) $@
# make -sC $(SQLITE_DIR) $@
Expand Down
1 change: 0 additions & 1 deletion client/inc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ typedef struct s_user {
char* name;
char* password;
char* avatar_path;
t_avatar_color avatar_color;
} t_user;

typedef struct s_client_utils {
Expand Down
1 change: 1 addition & 0 deletions client/src/request_handlers/create_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ t_response_code handle_create_chat_request(const char* chat_name) {
cJSON *json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "name", chat_name);
cJSON_AddNumberToObject(json, "date", get_current_time());
cJSON_AddNumberToObject(json, "avatar_color", get_avatar_color());
cJSON_AddNumberToObject(json, "type", REQ_CREATE_CHAT);
char* json_str = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
Expand Down
1 change: 0 additions & 1 deletion client/src/request_handlers/get_chat_msgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ t_response_code handle_get_chat_msgs_request(int chat_id) {
if ((error_code = handle_get_chat_msgs_response(response)) != R_SUCCESS) {
logger(get_response_str(error_code), ERROR_LOG);
free(response);
utils->is_suspended = false;
return error_code;
}
free(response);
Expand Down
8 changes: 5 additions & 3 deletions client/src/request_handlers/get_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ t_response_code add_chat_to_chatlist(cJSON* json, t_chat** chat_list, bool is_se

cJSON* chat_id = cJSON_GetObjectItem(json, "chat_id");
cJSON* chat_name = cJSON_GetObjectItemCaseSensitive(json, "chat_name");
cJSON* chat_color = cJSON_GetObjectItem(json, "chat_color");
cJSON* chat_perms = cJSON_GetObjectItem(json, "chat_permissions");

if (!cJSON_IsNumber(chat_id) || !cJSON_IsString(chat_name) || !cJSON_IsNumber(chat_perms)) {
if (!cJSON_IsNumber(chat_id) || !cJSON_IsString(chat_name) ||
!cJSON_IsNumber(chat_perms) || !cJSON_IsNumber(chat_color)) {
return R_JSON_FAILURE;
}
if (!is_search)
pthread_mutex_lock(&utils->lock);

mx_chat_push_back(chat_list, chat_id->valueint, chat_name->valuestring, chat_perms->valueint);
mx_chat_push_back(chat_list, chat_id->valueint, chat_name->valuestring,
chat_perms->valueint, chat_color->valueint);

if (!is_search)
pthread_mutex_unlock(&utils->lock);
Expand Down Expand Up @@ -83,7 +86,6 @@ t_response_code handle_get_chats_request() {
if ((error_code = handle_get_chats_response(&utils->chatlist, response, false)) != R_SUCCESS) {
logger(get_response_str(error_code), ERROR_LOG);
free(response);
utils->is_suspended = false;
return error_code;
}
free(response);
Expand Down
4 changes: 1 addition & 3 deletions client/src/request_handlers/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ void set_current_user(t_user** user, const cJSON* user_json) {
const cJSON* id_json = cJSON_GetObjectItem(user_json, "id");
const cJSON* name_json = cJSON_GetObjectItemCaseSensitive(user_json, "username");
const cJSON* pass_json = cJSON_GetObjectItemCaseSensitive(user_json, "password");
const cJSON* color_json = cJSON_GetObjectItem(user_json, "avatar_color");

if (!cJSON_IsNumber(id_json) || !cJSON_IsString(name_json) ||
!cJSON_IsString(pass_json) || !cJSON_IsNumber(color_json)) {
!cJSON_IsString(pass_json)) {
return;
}

*user = malloc(sizeof(t_user));
(*user)->user_id = id_json->valueint;
(*user)->name = mx_strdup(name_json->valuestring);
(*user)->password = mx_strdup(pass_json->valuestring);
(*user)->avatar_color = color_json->valueint;
(*user)->avatar_path = NULL;

}
Expand Down
1 change: 0 additions & 1 deletion client/src/request_handlers/signup.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ t_response_code handle_signup_request(const char* user_name, const char* user_pa
cJSON *json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "name", user_name);
cJSON_AddStringToObject(json, "password", user_password);
cJSON_AddNumberToObject(json, "avatar_color", get_avatar_color());
cJSON_AddNumberToObject(json, "type", REQ_USR_SIGNUP);
char* json_str = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
Expand Down
3 changes: 1 addition & 2 deletions server/inc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ sqlite3* open_database();
int db_execute_query(const char* query);
sqlite3_stmt* db_execute_stmt_for(const char* query, sqlite3* db);

t_response_code db_insert_chat(const char* chat_name, int date);
t_response_code db_insert_chat(const char* chat_name, int date, int avatar_color);
bool db_chat_exists(int chat_id);
int db_get_chats_total(int user_id);
bool db_has_chat_perms(int user_id, int chat_id, t_member_type perms);
bool db_user_exists(const char* username);
char* db_get_username_by_id(int user_id);
t_chat* db_get_chat_by_id(int user_id, int chat_id);
int db_insert_member(const char* chat_name, t_member_type member_type, t_server_utils* utils);
t_chat* db_get_chats_by_user_id(int user_id);
t_user* db_get_user_by_id(int user_id, t_server_utils* utils);
bool db_is_chat_member(int user_id, int chat_id);
int db_get_chat_id_by_name(const char* chat_name);
Expand Down
2 changes: 0 additions & 2 deletions server/inc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ typedef struct s_user {
int user_id;
char* name;
char* password;
t_avatar_color avatar_color;
t_chat* chats;
struct s_user* next;
} t_user;

Expand Down
5 changes: 3 additions & 2 deletions server/src/api/handle_create_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ void handle_create_chat(const cJSON* chat_info, t_server_utils* utils) {

const cJSON *chat_name = cJSON_GetObjectItemCaseSensitive(chat_info, "name");
const cJSON *date = cJSON_GetObjectItem(chat_info, "date");
const cJSON *av_color = cJSON_GetObjectItem(chat_info, "avatar_color");

if (!cJSON_IsString(chat_name) || !cJSON_IsNumber(date)) {
if (!cJSON_IsString(chat_name) || !cJSON_IsNumber(av_color) || !cJSON_IsNumber(date)) {
send_server_response(utils->ssl, R_JSON_FAILURE, REQ_CREATE_CHAT);
return;
}
Expand All @@ -31,7 +32,7 @@ void handle_create_chat(const cJSON* chat_info, t_server_utils* utils) {
}

t_response_code resp_code = 0;
if ((resp_code = db_insert_chat(chat_name->valuestring, date->valueint)) != R_SUCCESS) {
if ((resp_code = db_insert_chat(chat_name->valuestring, date->valueint, av_color->valueint)) != R_SUCCESS) {
send_server_response(utils->ssl, resp_code, REQ_CREATE_CHAT);
return;
}
Expand Down
2 changes: 0 additions & 2 deletions server/src/api/handle_delete_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ int db_delete_member(int chat_id, t_server_utils* utils) {
if (db_execute_query(query) != 0) {
return 1;
}

mx_chat_pop_id(&utils->user->chats, chat_id);
return 0;

}
Expand Down
5 changes: 3 additions & 2 deletions server/src/api/handle_get_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ cJSON* get_chat_json(sqlite3_stmt* stmt, bool is_for_search) {
cJSON* json = cJSON_CreateObject();
cJSON_AddNumberToObject(json, "chat_id", sqlite3_column_int64(stmt, 0));
cJSON_AddStringToObject(json, "chat_name", (const char*)sqlite3_column_text(stmt, 1));
cJSON_AddNumberToObject(json, "chat_permissions", !is_for_search ? sqlite3_column_int64(stmt, 2) : 1);
cJSON_AddNumberToObject(json, "chat_color", sqlite3_column_int64(stmt, 2));
cJSON_AddNumberToObject(json, "chat_permissions", !is_for_search ? sqlite3_column_int64(stmt, 3) : 1);
return json;

}
Expand All @@ -17,7 +18,7 @@ cJSON* get_chats_array_json(int user_id) {
cJSON* chats_json = cJSON_CreateArray();
sqlite3* db = open_database();
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, "SELECT chats.id, chats.name, members.permissions FROM chats "
sqlite3_prepare_v2(db, "SELECT chats.id, chats.name, chats.avatar_color, members.permissions FROM chats "
"INNER JOIN `members` ON members.chat_id = chats.id "
"WHERE chats.id IN (SELECT `chat_id` FROM `members` WHERE `user_id` = ?) AND members.user_id = ? "
"ORDER BY chats.date DESC ",
Expand Down
1 change: 0 additions & 1 deletion server/src/api/handle_last_msg_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ t_response_code get_last_msg_id(const cJSON* chat_info, t_server_utils* utils, i
}

if (!db_chat_exists(chat_id->valueint)) {
mx_chat_pop_id(&utils->user->chats, chat_id->valueint);
return R_CHAT_NOENT;
}

Expand Down
7 changes: 2 additions & 5 deletions server/src/api/handle_usr_login.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ char* get_json_formatted_user(const t_user* user) {
cJSON_AddNumberToObject(json, "id", user->user_id);
cJSON_AddStringToObject(json, "username", user->name);
cJSON_AddStringToObject(json, "password", user->password);
cJSON_AddNumberToObject(json, "avatar_color", user->avatar_color);
cJSON_AddNumberToObject(json, "error_code", R_SUCCESS);
char* user_info = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
Expand All @@ -22,7 +21,6 @@ void set_user_account_data(sqlite3_stmt* stmt, t_server_utils* utils) {
utils->user = mx_create_user(sqlite3_column_int64(stmt, 0), utils->client_socket, utils->ssl);
utils->user->name = mx_strdup((const char*)sqlite3_column_text(stmt, 1));
utils->user->password = mx_strdup((const char*)sqlite3_column_text(stmt, 2));
utils->user->avatar_color = sqlite3_column_int(stmt, 3);

}
sqlite3_finalize(stmt);
Expand Down Expand Up @@ -52,10 +50,9 @@ t_response_code set_user_by_username(const char* username, const char* password,
free(response);

char result_to_log[QUERY_LEN];
sprintf(result_to_log, "Logged in user info: id: %d, name: %s, avatar color: %d",
sprintf(result_to_log, "Logged in user info: id: %d, name: %s",
utils->user->user_id,
utils->user->name,
utils->user->avatar_color);
utils->user->name);

logger(result_to_log, INFO_LOG);
return R_SUCCESS;
Expand Down
7 changes: 3 additions & 4 deletions server/src/api/handle_usr_signup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ t_response_code db_add_user(const cJSON* user_info) {

const cJSON *user_name = cJSON_GetObjectItemCaseSensitive(user_info, "name");
const cJSON *user_password = cJSON_GetObjectItemCaseSensitive(user_info, "password");
const cJSON *avatar_color = cJSON_GetObjectItem(user_info, "avatar_color");

if (!cJSON_IsString(user_name) || !cJSON_IsString(user_password) || !cJSON_IsNumber(avatar_color)) {
if (!cJSON_IsString(user_name) || !cJSON_IsString(user_password)) {
return R_JSON_FAILURE;
}

Expand All @@ -26,8 +25,8 @@ t_response_code db_add_user(const cJSON* user_info) {
}

char query[QUERY_LEN];
sprintf(query, "INSERT INTO `users` (`username`, `password`, `avatar_color`) VALUES('%s', '%s', '%d')",
user_name->valuestring, user_password->valuestring, avatar_color->valueint);
sprintf(query, "INSERT INTO `users` (`username`, `password`) VALUES('%s', '%s')",
user_name->valuestring, user_password->valuestring);

if (db_execute_query(query) != 0) {
return R_DB_FAILURE;
Expand Down
30 changes: 3 additions & 27 deletions server/src/database/db_chat.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "../../inc/server.h"

t_response_code db_insert_chat(const char* chat_name, int date) {
t_response_code db_insert_chat(const char* chat_name, int date, int avatar_color) {

int chat_id = db_get_chat_id_by_name(chat_name);
if (chat_id != -1) {
return R_CHAT_EXISTS;
}

char query[QUERY_LEN];
sprintf(query, "INSERT INTO `chats` (`name`, `date`) VALUES('%s', '%d')", chat_name, date);
sprintf(query, "INSERT INTO `chats` (`name`, `date`, `avatar_color`) VALUES('%s', '%d', '%d')",
chat_name, date, avatar_color);

if (db_execute_query(query) != 0) {
return R_DB_FAILURE;
Expand Down Expand Up @@ -51,31 +52,6 @@ int db_get_chat_id_by_name(const char* chat_name) {

}

t_chat* db_get_chats_by_user_id(int user_id) {

sqlite3* db = open_database();
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, "SELECT chats.id, chats.name, chats.permissions FROM `chats` "
"WHERE `id` IN (SELECT `chat_id` FROM `members` WHERE `user_id` = ?)",
-1, &stmt, NULL);
sqlite3_bind_int64(stmt, 1, user_id);

t_chat* chats = NULL;

while (sqlite3_step(stmt) == SQLITE_ROW) {

int chat_id = sqlite3_column_int64(stmt, 0);
const char* chat_name = (const char*)sqlite3_column_text(stmt, 1);
int permissions = sqlite3_column_int64(stmt, 2);
mx_chat_push_back(&chats, chat_id, chat_name, permissions);

}
sqlite3_finalize(stmt);
sqlite3_close(db);
return chats;

}

bool db_has_chat_perms(int user_id, int chat_id, t_member_type perms) {

char query[QUERY_LEN];
Expand Down
2 changes: 0 additions & 2 deletions server/src/database/db_member.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ int db_insert_member(const char* chat_name, t_member_type member_type, t_server_
if (db_execute_query(query) != 0) {
return 1;
}

mx_chat_push_back(&utils->user->chats, chat_id, chat_name, member_type);

return 0;

Expand Down
6 changes: 3 additions & 3 deletions server/src/database_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ int database_init() {
"`id` INTEGER PRIMARY KEY AUTOINCREMENT,"
"`username` VARCHAR(32) NOT NULL,"
"`password` VARCHAR(32) NOT NULL,"
"`image` BLOB,"
"`avatar_color` TINYINT NOT NULL);"
"`image` BLOB);"

"CREATE TABLE `chats` ("
"`id` INTEGER PRIMARY KEY AUTOINCREMENT,"
"`name` VARCHAR(32) NOT NULL,"
"`date` INTEGER NOT NULL);"
"`date` INTEGER NOT NULL,"
"`avatar_color` TINYINT NOT NULL);"

"CREATE TABLE `members` ("
"`id` INTEGER PRIMARY KEY AUTOINCREMENT,"
Expand Down
46 changes: 0 additions & 46 deletions server/src/server_utils/user_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ t_user* mx_create_user(int id, int client_fd, SSL* ssl) {
new_node->name = NULL;
new_node->password = NULL;
new_node->ssl = ssl;

new_node->chats = db_get_chats_by_user_id(id);

new_node->next = NULL;
return new_node;
Expand Down Expand Up @@ -53,7 +51,6 @@ void mx_clear_user(t_user** p) {

free((*p)->name);
free((*p)->password);
mx_clear_chat_list(&(*p)->chats);
free(*p);
*p = NULL;

Expand Down Expand Up @@ -122,22 +119,6 @@ void mx_user_pop_index(t_user **list, int index) {

}

// int mx_get_user_id(int user_db_id) {

// int i = 0;
// t_user* curr_usr = global_state.logged_users;
// for (; curr_usr; ++i) {

// if (curr_usr->user_id == user_db_id)
// return i;

// curr_usr = curr_usr->next;

// }
// return i;

// }

void mx_clear_user_list(t_user **list)
{
if (list == NULL || *list == NULL)
Expand Down Expand Up @@ -171,30 +152,3 @@ int mx_user_list_size(t_user* list) {
return size;

}

// remove later
// void print_logged_users() {

// t_user* temp = global_state.logged_users;
// logger("Logged in:", INFO_LOG);
// while (temp) {

// char user[100];
// sprintf(user, "logged in -- %d: %s", temp->user_id, temp->name);
// logger(user, INFO_LOG);

// t_chat* temp_chat = temp->chats;
// while (temp_chat) {

// char user[180];
// sprintf(user, "\t\tchat_id -- %d, chat_name -- %s, chat_perms - %d",
// temp_chat->id, temp_chat->name, temp_chat->permissions);
// logger(user, INFO_LOG);
// temp_chat = temp_chat->next;

// }
// temp = temp->next;

// }

// }
Loading

0 comments on commit 545e4e0

Please sign in to comment.