From 25d83db84055f16e4f5e9bbc38a8cd85945ffd98 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 6 Feb 2021 16:35:24 +0100 Subject: [PATCH] Close websockets properly when closing connection --- src/controllers/user_ws_controller.rs | 15 ++++++++++++++- src/helpers/account_helper.rs | 9 +++++---- src/helpers/events_helper.rs | 7 ++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index fb0b1c4..c77074c 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -24,7 +24,8 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::data::user::UserID; use crate::data::user_ws_message::UserWsMessage; use crate::data::user_ws_request_handler::{UserWsRequestHandler, UserWsResponseType}; -use crate::helpers::account_helper; +use crate::helpers::{account_helper, events_helper}; +use crate::helpers::events_helper::Event; use crate::utils::crypt_utils::rand_str; use crate::utils::date_utils::time; @@ -570,5 +571,17 @@ pub fn disconnect_user_from_all_sockets(user_id: &UserID) -> Res { c.do_send(WsCloseConnection {}); } + Ok(()) +} + +/// Events handler +pub fn handle_event(e: &events_helper::Event) -> Res { + match e { + Event::DestroyedLoginToken(user_id, client) => { + disconnect_user_from_client(user_id, client)?; + } + _ => {} + } + Ok(()) } \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index d6f027f..6a568c1 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -10,8 +10,9 @@ use crate::data::new_account::NewAccount; use crate::data::security_settings::SecuritySettings; use crate::data::user::{AccountImageVisibility, UserID, UserPageStatus}; use crate::data::user_token::UserAccessToken; -use crate::helpers::{comments_helper, conversations_helper, custom_emojies_helper, database, friends_helper, groups_helper, likes_helper, movies_helper, notifications_helper, posts_helper, survey_helper, user_helper}; +use crate::helpers::{comments_helper, conversations_helper, custom_emojies_helper, database, events_helper, friends_helper, groups_helper, likes_helper, movies_helper, notifications_helper, posts_helper, survey_helper, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo}; +use crate::helpers::events_helper::Event; use crate::helpers::likes_helper::LikeType; use crate::utils::crypt_utils::{crypt_pass, rand_str}; use crate::utils::date_utils::{mysql_date, time}; @@ -107,14 +108,14 @@ pub fn exists_mail(mail: &str) -> ResultBoxError { /// Destroy a given user login tokens pub fn destroy_login_tokens(id: &UserID, client: &APIClient) -> ResultBoxError<()> { - - // TODO : send an event (destroyed_login_tokens) - database::delete(DeleteQuery::new(USER_ACCESS_TOKENS_TABLE) .cond_u32("service_id", client.id) .cond_user_id("user_id", id) )?; + // Send an event (destroyed_login_tokens) + events_helper::propagate_event(&Event::DestroyedLoginToken(id.clone(), client))?; + Ok(()) } diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index 7cb4886..5ccb4cc 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -4,13 +4,17 @@ -use crate::controllers::{comments_controller, conversations_controller, notifications_controller}; +use crate::controllers::{comments_controller, conversations_controller, notifications_controller, user_ws_controller}; +use crate::data::api_client::APIClient; use crate::data::comment::Comment; use crate::data::conversation_message::ConversationMessage; use crate::data::error::Res; use crate::data::user::UserID; pub enum Event<'a> { + /// Destroyed a login token + DestroyedLoginToken(UserID, &'a APIClient), + /// Updated the number of notifications of one of multiple users user UpdatedNotificationsNumber(&'a Vec), @@ -44,5 +48,6 @@ pub fn propagate_event(e: &Event) -> Res { conversations_controller::handle_event(e)?; comments_controller::handle_event(e)?; notifications_controller::handle_event(e)?; + user_ws_controller::handle_event(e)?; Ok(()) } \ No newline at end of file