1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Close websockets properly when closing connection

This commit is contained in:
Pierre HUBERT 2021-02-06 16:35:24 +01:00
parent 7cf5977da2
commit 25d83db840
3 changed files with 25 additions and 6 deletions

View File

@ -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(())
}

View File

@ -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<bool> {
/// 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(())
}

View File

@ -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<UserID>),
@ -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(())
}