Online status #43
6 changed files with 44 additions and 0 deletions
2
migrations/2025-08-04-180235_add_status_to_user/down.sql
Normal file
2
migrations/2025-08-04-180235_add_status_to_user/down.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
ALTER TABLE users DROP COLUMN online_status;
|
2
migrations/2025-08-04-180235_add_status_to_user/up.sql
Normal file
2
migrations/2025-08-04-180235_add_status_to_user/up.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
ALTER TABLE users ADD COLUMN online_status INT2 NOT NULL DEFAULT 0;
|
|
@ -49,6 +49,7 @@ struct NewInfo {
|
||||||
email: Option<String>,
|
email: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
|
online_status: Option<i16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(
|
pub async fn update(
|
||||||
|
@ -110,5 +111,10 @@ pub async fn update(
|
||||||
.await?;
|
.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)
|
Ok(StatusCode::OK)
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ pub struct Me {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
|
online_status: i16,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub email_verified: bool,
|
pub email_verified: bool,
|
||||||
}
|
}
|
||||||
|
@ -277,6 +278,35 @@ impl Me {
|
||||||
Ok(())
|
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::<User>(self.uuid.to_string())
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
cache_pool.del_cache_key(self.uuid.to_string()).await?
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn friends_with(
|
pub async fn friends_with(
|
||||||
&self,
|
&self,
|
||||||
conn: &mut Conn,
|
conn: &mut Conn,
|
||||||
|
|
|
@ -18,6 +18,7 @@ pub struct UserBuilder {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
|
online_status: i16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserBuilder {
|
impl UserBuilder {
|
||||||
|
@ -29,6 +30,7 @@ impl UserBuilder {
|
||||||
avatar: self.avatar,
|
avatar: self.avatar,
|
||||||
pronouns: self.pronouns,
|
pronouns: self.pronouns,
|
||||||
about: self.about,
|
about: self.about,
|
||||||
|
online_status: self.online_status,
|
||||||
friends_since: None,
|
friends_since: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +44,7 @@ pub struct User {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
|
online_status: i16,
|
||||||
friends_since: Option<DateTime<Utc>>,
|
friends_since: Option<DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,6 +157,7 @@ diesel::table! {
|
||||||
pronouns -> Nullable<Varchar>,
|
pronouns -> Nullable<Varchar>,
|
||||||
#[max_length = 200]
|
#[max_length = 200]
|
||||||
about -> Nullable<Varchar>,
|
about -> Nullable<Varchar>,
|
||||||
|
online_status -> Int2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue