use rocket::serde::{self, Serialize}; use uuid::Uuid; use time::OffsetDateTime; use sqlx::{postgres::types::PgInterval, Pool, Postgres}; #[derive(Serialize, Debug)] #[serde(crate = "rocket::serde")] pub struct User { uuid: Uuid, email: String, username: String, display_name: Option, password_hash: Option, #[serde(with="time::serde::rfc3339")] time_created: OffsetDateTime, #[serde(with="time::serde::rfc3339::option")] time_password_changed: Option, #[serde(with="time::serde::rfc3339::option")] time_email_confirmed: Option } #[derive(Serialize, Debug)] #[serde(crate = "rocket::serde")] pub struct LoginSession { uuid: Uuid, #[serde(with="time::serde::rfc3339")] time_created: OffsetDateTime, #[serde(with="time::serde::rfc3339")] time_last_active: OffsetDateTime, #[serde(skip_serializing)] duration: PgInterval, #[serde(with="time::serde::rfc3339::option")] time_logged_out: Option, ip_address: String, user_agent: String, referer: String, user: User } impl LoginSession { pub async fn lookup(connection: &Pool::, uuid: Uuid) -> Option { joinrs::query_parsed!( connection = connection, query = r#" select login_session.*, user_uuid, user_display_name, user_password_hash, user_time_created, user_time_password_changed, user_time_email_confirmed, user_username::text as username, user_email::text as email from phtx.login_session left join phtx."user" on login_session_user_uuid = user_uuid where login_session_time_last_active + login_session_duration >= now() and login_session_uuid = $1 "#, params = [uuid], return_type = LoginSession { uuid: login_session_uuid, time_created: login_session_time_created, time_last_active: login_session_time_last_active, duration: login_session_duration, time_logged_out: login_session_time_logged_out, ip_address: login_session_ip_address, user_agent: login_session_user_agent, referer: login_session_referer, user: User { uuid: user_uuid, email: email, username: username, display_name: user_display_name, password_hash: user_password_hash, time_created: user_time_created, time_password_changed: user_time_password_changed, time_email_confirmed: user_time_email_confirmed } } ).ok() } }