1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Automatically update last user activity

This commit is contained in:
Pierre HUBERT 2021-02-05 14:49:45 +01:00
parent 35a77a729e
commit 5c3bbfcfaa
3 changed files with 25 additions and 8 deletions

View File

@ -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
@ -104,3 +106,6 @@ pub const PASSWORD_MIN_LENGTH: usize = 3;
/// Supported languages (for ComunicWeb)
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);

View File

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

View File

@ -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<Self>) {
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<Self>, msg: &str) -> Res<UserWsMessage> {
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(),