From 5c3bbfcfaa8359b4fcdda25a265d45dc40e51c26 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 5 Feb 2021 14:49:45 +0100 Subject: [PATCH] Automatically update last user activity --- src/constants.rs | 7 ++++++- src/controllers/friends_controller.rs | 7 +------ src/controllers/user_ws_controller.rs | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 5bac31a..d564fdd 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -4,6 +4,8 @@ //! //! @author Pierre Hubert +use std::time::Duration; + /// The name of the tables pub mod database_tables_names { /// API services tokens table @@ -103,4 +105,7 @@ pub const PASSWORD_RESET_TOKEN_LIFETIME: u64 = 60 * 60 * 6; pub const PASSWORD_MIN_LENGTH: usize = 3; /// Supported languages (for ComunicWeb) -pub const SUPPORTED_LANGUAGES: &'static [&'static str] = &["en", "fr"]; \ No newline at end of file +pub const SUPPORTED_LANGUAGES: &'static [&'static str] = &["en", "fr"]; + +/// Interval at which last active time of user should be recorded +pub const USER_LAST_ACTIVITY_REFRESH: Duration = Duration::from_secs(60); \ No newline at end of file diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index d61ceeb..8bdbbfc 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -8,17 +8,12 @@ use crate::controllers::routes::RequestResult; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::notification::NotifEventType; -use crate::helpers::{account_helper, friends_helper, notifications_helper, user_helper}; +use crate::helpers::{friends_helper, notifications_helper, user_helper}; /// Get the list of friends of the current user pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { let list = friends_helper::GetFriendsQuery::new(&r.user_id()?).exec()?; - // Update last activity (if allowed) - if !r.post_bool_opt("incognito", false) { - account_helper::update_last_activity(&r.user_id()?)?; - } - r.set_response(FriendAPI::from_list(&list)) } diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index ae224d7..31ca3ef 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -12,7 +12,7 @@ use actix_web_actors::ws::ProtocolError; use serde_json::Value; use crate::api_data::res_get_ws_token::ResGetWsToken; -use crate::constants::WS_ACCESS_TOKEN_LENGTH; +use crate::constants::{USER_LAST_ACTIVITY_REFRESH, WS_ACCESS_TOKEN_LENGTH}; use crate::controllers::user_ws_controller::ws_connections_list::{add_connection, find_connection, get_ws_connections_list, remove_connection}; pub use crate::controllers::user_ws_controller::ws_connections_list::WsConnection; use crate::controllers::user_ws_routes::find_user_ws_route; @@ -24,6 +24,7 @@ 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::utils::crypt_utils::rand_str; use crate::utils::date_utils::time; @@ -240,6 +241,21 @@ impl WsSession { }); } + /// helper method that update user last activity at every specified amount of time + fn user_activity(&self, ctx: &mut actix_web_actors::ws::WebsocketContext) { + if !self.incognito && account_helper::update_last_activity(&self.user_id).is_err() { + eprintln!("Failed to do initial refresh of last activity for user {} !", self.user_id.id()); + } + + ctx.run_interval(USER_LAST_ACTIVITY_REFRESH, |_, ctx| { + if let Some(conn) = find_connection(ctx.address()) { + if !conn.incognito && account_helper::update_last_activity(&conn.user_id).is_err() { + eprintln!("Failed to refresh last activity for user {} !", conn.user_id.id()); + } + } + }); + } + /// Handle incoming message fn handle_message(&self, ctx: &mut ws::WebsocketContext, msg: &str) -> Res { let incoming_msg: UserWsMessage = serde_json::from_str(&msg)?; @@ -302,6 +318,7 @@ impl Actor for WsSession { fn started(&mut self, ctx: &mut Self::Context) { // we'll start heartbeat process on session start. self.hb(ctx); + self.user_activity(ctx); add_connection(WsConnection { user_id: self.user_id.clone(),