Skip to content

Commit

Permalink
add: clear online status on server boot
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHuo committed Jan 8, 2025
1 parent 9cf2a1f commit c3ad7c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/service/presence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,44 @@ impl Service {
self.db.remove_presence(user_id).await;
}

// Unset online/unavailable presence to offline on startup
pub async fn unset_all_presence(&self) -> Result<()> {
for user_id in &self
.services
.users
.list_local_users()
.map(UserId::to_owned)
.collect::<Vec<_>>()
.await
{
let presence = self.db.get_presence(user_id).await;

let presence = match presence {
| Ok((_, ref presence)) => &presence.content,
| _ => return Ok(()),
};

let need_reset = match presence.presence {
| PresenceState::Unavailable | PresenceState::Online => true,
| _ => false,
};

if !need_reset {
return Ok(());
}

self.set_presence(
user_id,
&PresenceState::Offline,
Some(false),
presence.last_active_ago,
presence.status_msg.clone(),
)
.await?;
}
Ok(())
}

/// Returns the most recent presence updates that happened after the event
/// with id `since`.
pub fn presence_since(
Expand Down
5 changes: 5 additions & 0 deletions src/service/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ impl Services {
.start()
.await?;

// clear online statuses
if self.server.config.allow_local_presence {
_ = self.presence.unset_all_presence().await;
}

// set the server user as online
if self.server.config.allow_local_presence {
_ = self
Expand Down

0 comments on commit c3ad7c3

Please sign in to comment.