Skip to content

Commit

Permalink
Reset user status on startup & last con
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptex-github authored May 27, 2024
1 parent 5a10c64 commit d0dcc8c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async fn main() {
.expect("Failed to create global event exchange");

events::setup(con);
presence::reset_all().await.expect("failed to reset presence");

let listener = TcpListener::bind("0.0.0.0:8076")
.await
Expand Down
27 changes: 23 additions & 4 deletions src/presence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ pub async fn get_devices(user_id: u64) -> Result<Devices> {
Ok(devices)
}

pub async fn reset_all() -> Result<()> {
let mut con = get_con().await?;

let keys = con.keys::<_, String>("session-*").await?;
con.del(key).await?;
let keys = con.keys::<_, String>("presence-*").await?;
con.del(key).await?;

Ok(())
}

pub async fn get_last_session(user_id: u64) -> Result<Option<PresenceSession>> {
let key = format!("session-{user_id}");

Expand Down Expand Up @@ -109,6 +120,7 @@ pub async fn remove_session(user_id: u64, session_id: impl AsRef<str>) -> Result

if sessions.len() == 1 {
con.del::<_, ()>(key).await?;
update_presence(user_id, PresenceStatus::Offline).await?;

return Ok(());
}
Expand All @@ -130,10 +142,17 @@ pub async fn remove_session(user_id: u64, session_id: impl AsRef<str>) -> Result
pub async fn update_presence(user_id: u64, status: PresenceStatus) -> Result<()> {
let key = format!("presence-{user_id}");

get_con()
.await?
.set(key, bincode::encode_to_vec(status, CONFIG)?)
.await?;
if status == PresenceStatus::Offline {
get_con()
.await?
.del(key)
.await?;
} else {
get_con()
.await?
.set(key, bincode::encode_to_vec(status, CONFIG)?)
.await?;
}

Ok(())
}
Expand Down

0 comments on commit d0dcc8c

Please sign in to comment.