From 314b9ee011f5f6bbc5f715664fc16d2fa12a98a0 Mon Sep 17 00:00:00 2001 From: BAaboe Date: Mon, 4 Aug 2025 20:07:46 +0200 Subject: [PATCH 1/3] feat: added online_status column to users table --- migrations/2025-08-04-180235_add_status_to_user/down.sql | 2 ++ migrations/2025-08-04-180235_add_status_to_user/up.sql | 2 ++ src/schema.rs | 1 + 3 files changed, 5 insertions(+) create mode 100644 migrations/2025-08-04-180235_add_status_to_user/down.sql create mode 100644 migrations/2025-08-04-180235_add_status_to_user/up.sql diff --git a/migrations/2025-08-04-180235_add_status_to_user/down.sql b/migrations/2025-08-04-180235_add_status_to_user/down.sql new file mode 100644 index 0000000..163f7f1 --- /dev/null +++ b/migrations/2025-08-04-180235_add_status_to_user/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE users DROP COLUMN online_status; diff --git a/migrations/2025-08-04-180235_add_status_to_user/up.sql b/migrations/2025-08-04-180235_add_status_to_user/up.sql new file mode 100644 index 0000000..ac16d77 --- /dev/null +++ b/migrations/2025-08-04-180235_add_status_to_user/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE users ADD COLUMN online_status INT2 NOT NULL DEFAULT 0; diff --git a/src/schema.rs b/src/schema.rs index 413f3f1..88f6155 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -157,6 +157,7 @@ diesel::table! { pronouns -> Nullable, #[max_length = 200] about -> Nullable, + online_status -> Int2, } } -- 2.47.3 From 027649a0608fce91f9f995c1fd9398b93897fd1f Mon Sep 17 00:00:00 2001 From: BAaboe Date: Mon, 4 Aug 2025 20:46:49 +0200 Subject: [PATCH 2/3] 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>, } -- 2.47.3 From 53451e67c7fe83648cac8170be2f4aafe6306809 Mon Sep 17 00:00:00 2001 From: BAaboe Date: Mon, 4 Aug 2025 20:52:19 +0200 Subject: [PATCH 3/3] fix: 4 is always bigger than 0 --- src/objects/me.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/objects/me.rs b/src/objects/me.rs index e76802c..0c54570 100644 --- a/src/objects/me.rs +++ b/src/objects/me.rs @@ -1,4 +1,4 @@ -use axum::{body::Bytes, http::StatusCode}; +use axum::body::Bytes; use diesel::{ ExpressionMethods, QueryDsl, Queryable, Selectable, SelectableHelper, delete, insert_into, update, @@ -284,7 +284,7 @@ impl Me { cache_pool: &redis::Client, new_status: i16, ) -> Result<(), Error> { - if new_status > 4 && new_status < 0 { + if new_status > 4 || new_status < 0 { return Err(Error::BadRequest("Invalid status code".to_string())); } self.online_status = new_status; -- 2.47.3