From 027649a0608fce91f9f995c1fd9398b93897fd1f Mon Sep 17 00:00:00 2001 From: BAaboe Date: Mon, 4 Aug 2025 20:46:49 +0200 Subject: [PATCH] feat: added online status --- src/api/v1/me/mod.rs | 6 ++++++ src/objects/me.rs | 32 +++++++++++++++++++++++++++++++- src/objects/user.rs | 3 +++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/api/v1/me/mod.rs b/src/api/v1/me/mod.rs index 86d3d9e..9d75d26 100644 --- a/src/api/v1/me/mod.rs +++ b/src/api/v1/me/mod.rs @@ -49,6 +49,7 @@ struct NewInfo { email: Option, pronouns: Option, about: Option, + online_status: Option, } pub async fn update( @@ -110,5 +111,10 @@ pub async fn update( .await?; } + if let Some(online_status) = &json.online_status { + me.set_online_status(&mut conn, &app_state.cache_pool, *online_status) + .await?; + } + Ok(StatusCode::OK) } diff --git a/src/objects/me.rs b/src/objects/me.rs index d03e08b..e76802c 100644 --- a/src/objects/me.rs +++ b/src/objects/me.rs @@ -1,4 +1,4 @@ -use axum::body::Bytes; +use axum::{body::Bytes, http::StatusCode}; use diesel::{ ExpressionMethods, QueryDsl, Queryable, Selectable, SelectableHelper, delete, insert_into, update, @@ -29,6 +29,7 @@ pub struct Me { avatar: Option, pronouns: Option, about: Option, + online_status: i16, pub email: String, pub email_verified: bool, } @@ -277,6 +278,35 @@ impl Me { Ok(()) } + pub async fn set_online_status( + &mut self, + conn: &mut Conn, + cache_pool: &redis::Client, + new_status: i16, + ) -> Result<(), Error> { + if new_status > 4 && new_status < 0 { + return Err(Error::BadRequest("Invalid status code".to_string())); + } + self.online_status = new_status; + + use users::dsl; + update(users::table) + .filter(dsl::uuid.eq(self.uuid)) + .set(dsl::online_status.eq(new_status)) + .execute(conn) + .await?; + + if cache_pool + .get_cache_key::(self.uuid.to_string()) + .await + .is_ok() + { + cache_pool.del_cache_key(self.uuid.to_string()).await? + } + + Ok(()) + } + pub async fn friends_with( &self, conn: &mut Conn, diff --git a/src/objects/user.rs b/src/objects/user.rs index a686c39..e9f638a 100644 --- a/src/objects/user.rs +++ b/src/objects/user.rs @@ -18,6 +18,7 @@ pub struct UserBuilder { avatar: Option, pronouns: Option, about: Option, + online_status: i16, } impl UserBuilder { @@ -29,6 +30,7 @@ impl UserBuilder { avatar: self.avatar, pronouns: self.pronouns, about: self.about, + online_status: self.online_status, friends_since: None, } } @@ -42,6 +44,7 @@ pub struct User { avatar: Option, pronouns: Option, about: Option, + online_status: i16, friends_since: Option>, }