add: clear online status on server boot

This commit is contained in:
Holger Huo 2025-01-08 18:42:46 +08:00 committed by strawberry
parent 8451ea3bc3
commit b71201cf19
2 changed files with 43 additions and 0 deletions

View file

@ -170,6 +170,44 @@ impl Service {
self.db.remove_presence(user_id).await; 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 /// Returns the most recent presence updates that happened after the event
/// with id `since`. /// with id `since`.
pub fn presence_since( pub fn presence_since(

View file

@ -123,6 +123,11 @@ impl Services {
.start() .start()
.await?; .await?;
// clear online statuses
if self.server.config.allow_local_presence {
_ = self.presence.unset_all_presence().await;
}
// set the server user as online // set the server user as online
if self.server.config.allow_local_presence && !self.db.is_read_only() { if self.server.config.allow_local_presence && !self.db.is_read_only() {
_ = self _ = self