-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
leave chat & delete account requests added
- Loading branch information
1 parent
b5fedc9
commit 6f6fe91
Showing
16 changed files
with
204 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
#include "../../inc/client.h" | ||
|
||
void leave_chat_btn_click(GtkWidget *widget, gpointer data) { | ||
|
||
|
||
int response_code = handle_leave_chat_request(utils->current_chat->name); | ||
|
||
if (response_code == R_SUCCESS) | ||
{ | ||
utils->current_chat = NULL; | ||
update_chatlist(); | ||
// build_start_messaging_label(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "../../inc/client.h" | ||
|
||
t_response_code handle_delete_account_request() { | ||
|
||
utils->is_suspended = true; | ||
|
||
cJSON *json = cJSON_CreateObject(); | ||
cJSON_AddNumberToObject(json, "type", REQ_DELETE_ACCOUNT); | ||
char* json_str = cJSON_PrintUnformatted(json); | ||
cJSON_Delete(json); | ||
|
||
char* response = send_and_recv_from_server(utils->ssl, json_str); | ||
int error_code = handle_server_response(response); | ||
logger(get_response_str(error_code), error_code == R_SUCCESS ? INFO_LOG : ERROR_LOG); | ||
|
||
free(json_str); | ||
free(response); | ||
return error_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "../../inc/client.h" | ||
|
||
t_response_code handle_leave_chat_request(const char* chat_name) { | ||
|
||
utils->is_suspended = true; | ||
|
||
cJSON *json = cJSON_CreateObject(); | ||
cJSON_AddStringToObject(json, "name", chat_name); | ||
cJSON_AddNumberToObject(json, "type", REQ_LEAVE_CHAT); | ||
char* json_str = cJSON_PrintUnformatted(json); | ||
cJSON_Delete(json); | ||
|
||
char* response = send_and_recv_from_server(utils->ssl, json_str); | ||
int error_code = handle_server_response(response); | ||
logger(get_response_str(error_code), error_code == R_SUCCESS ? INFO_LOG : ERROR_LOG); | ||
|
||
if (error_code == R_SUCCESS) { | ||
|
||
handle_get_chats_request(); | ||
|
||
} | ||
|
||
free(json_str); | ||
free(response); | ||
|
||
utils->is_suspended = false; | ||
return error_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "../../inc/server.h" | ||
|
||
t_response_code db_delete_user(int user_id) { | ||
|
||
if (!db_get_username_by_id(user_id)) { | ||
return R_USR_NOENT; | ||
} | ||
|
||
char query[QUERY_LEN]; | ||
sprintf(query, "DELETE FROM `users` WHERE `id` = '%d'", user_id); | ||
|
||
if (db_execute_query(query) != 0) { | ||
return R_DB_FAILURE; | ||
} | ||
return R_SUCCESS; | ||
|
||
} | ||
|
||
t_response_code db_delete_member_by_user_id(int user_id) { | ||
|
||
char query[QUERY_LEN]; | ||
sprintf(query, "DELETE FROM `members` WHERE `user_id` = '%d'", user_id); | ||
if (db_execute_query(query) != 0) { | ||
return R_DB_FAILURE; | ||
} | ||
|
||
return R_SUCCESS; | ||
|
||
} | ||
|
||
static t_response_code db_delete_messages(int user_id) { | ||
|
||
char query[QUERY_LEN]; | ||
sprintf(query, "DELETE FROM `messages` WHERE `user_id` = '%d'", user_id); | ||
if (db_execute_query(query) != 0) { | ||
return R_DB_FAILURE; | ||
} | ||
return R_SUCCESS; | ||
|
||
} | ||
|
||
void handle_delete_account(const cJSON* chat_info, t_server_utils* utils) { | ||
|
||
(void)chat_info; | ||
if (database_init() != 0) { | ||
send_server_response(utils->ssl, R_DB_FAILURE, REQ_DELETE_ACCOUNT); | ||
return; | ||
} | ||
|
||
int error_code = 0; | ||
if ((error_code = db_delete_user(utils->user->user_id)) != R_SUCCESS) { | ||
send_server_response(utils->ssl, error_code, REQ_DELETE_ACCOUNT); | ||
} | ||
|
||
if ((error_code = db_delete_member_by_user_id(utils->user->user_id)) != R_SUCCESS) { | ||
send_server_response(utils->ssl, error_code, REQ_DELETE_ACCOUNT); | ||
} | ||
|
||
if ((error_code = db_delete_messages(utils->user->user_id)) != R_SUCCESS) { | ||
send_server_response(utils->ssl, error_code, REQ_DELETE_ACCOUNT); | ||
} | ||
|
||
send_server_response(utils->ssl, R_SUCCESS, REQ_DELETE_ACCOUNT); | ||
|
||
logger("User has been removed\n", INFO_LOG); | ||
client_cleanup(utils, false); | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "../../inc/server.h" | ||
|
||
static t_response_code db_delete_member(int user_id, int chat_id) { | ||
|
||
if (!db_chat_exists(chat_id)) { | ||
return R_CHAT_NOENT; | ||
} | ||
|
||
if (!db_is_chat_member(user_id, chat_id)) { | ||
return R_ISNT_CHAT_MEMBER; | ||
} | ||
|
||
if (!db_has_chat_perms(user_id, chat_id, NORMAL_MEMBER)) { | ||
return R_NO_CHAT_PERMS; | ||
} | ||
|
||
char query[QUERY_LEN]; | ||
sprintf(query, "DELETE FROM `members` WHERE `user_id` = '%d' AND `chat_id` = '%d'", user_id, chat_id); | ||
if (db_execute_query(query) != 0) { | ||
return R_DB_FAILURE; | ||
} | ||
|
||
return R_SUCCESS; | ||
|
||
} | ||
|
||
void handle_leave_chat(const cJSON* chat_info, t_server_utils* utils) { | ||
|
||
if (database_init() != 0) { | ||
send_server_response(utils->ssl, R_DB_FAILURE, REQ_LEAVE_CHAT); | ||
return; | ||
} | ||
|
||
const cJSON *chat_name = cJSON_GetObjectItemCaseSensitive(chat_info, "name"); | ||
|
||
if (!cJSON_IsString(chat_name)) { | ||
send_server_response(utils->ssl, R_JSON_FAILURE, REQ_LEAVE_CHAT); | ||
return; | ||
} | ||
|
||
int chat_id = db_get_chat_id_by_name(chat_name->valuestring); | ||
int error_code = 0; | ||
|
||
if ((error_code = db_delete_member(utils->user->user_id, chat_id)) != 0) { | ||
send_server_response(utils->ssl, error_code, REQ_LEAVE_CHAT); | ||
return; | ||
} | ||
send_server_response(utils->ssl, R_SUCCESS, REQ_LEAVE_CHAT); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters