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/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..0c54570 100644 --- a/src/objects/me.rs +++ b/src/objects/me.rs @@ -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>, } 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, } }