Skip to content

Commit

Permalink
Merge pull request #110 from lajp/current-flush
Browse files Browse the repository at this point in the history
feat: flush old session on current activity request
  • Loading branch information
lajp authored Dec 27, 2023
2 parents 292a146 + e0525a3 commit 29f3a31
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/api/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,24 @@ pub async fn get_current_activity(

match heartbeats.get(&target_user) {
Some(heartbeat) => {
let (inner_heartbeat, start_time, duration) = heartbeat.to_owned();
let (inner_heartbeat, start, duration) = heartbeat.to_owned();
drop(heartbeat);
let current_heartbeat = CurrentActivity {
started: start_time,
duration: duration.num_seconds(),
heartbeat: inner_heartbeat,
};
Ok(web::Json(Some(current_heartbeat)))
let curtime = Local::now().naive_local();
if curtime.signed_duration_since(start + duration) > Duration::seconds(900) {
db.add_activity(target_user, inner_heartbeat, start, duration)
.await
.map_err(ErrorInternalServerError)?;

heartbeats.remove(&target_user);
Err(TimeError::NotActive)
} else {
let current_heartbeat = CurrentActivity {
started: start,
duration: duration.num_seconds(),
heartbeat: inner_heartbeat,
};
Ok(web::Json(Some(current_heartbeat)))
}
}
None => Err(TimeError::NotActive),
}
Expand Down

0 comments on commit 29f3a31

Please sign in to comment.