conduit/src/client_server/presence.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

2020-07-30 16:14:47 +00:00
use super::State;
use crate::{utils, ConduitResult, Database, Ruma};
use ruma::api::client::r0::presence::set_presence;
use std::convert::TryInto;
#[cfg(feature = "conduit_bin")]
use rocket::put;
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/presence/<_>/status", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn set_presence_route(
2020-07-30 16:14:47 +00:00
db: State<'_, Database>,
2020-09-08 15:32:03 +00:00
body: Ruma<set_presence::Request<'_>>,
2020-07-30 16:14:47 +00:00
) -> ConduitResult<set_presence::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 16:14:47 +00:00
for room_id in db.rooms.rooms_joined(&sender_user) {
2020-07-30 16:14:47 +00:00
let room_id = room_id?;
db.rooms.edus.update_presence(
&sender_user,
2020-07-30 16:14:47 +00:00
&room_id,
ruma::events::presence::PresenceEvent {
content: ruma::events::presence::PresenceEventContent {
avatar_url: db.users.avatar_url(&sender_user)?,
2020-07-30 16:14:47 +00:00
currently_active: None,
displayname: db.users.displayname(&sender_user)?,
2020-07-30 16:14:47 +00:00
last_active_ago: Some(
utils::millis_since_unix_epoch()
.try_into()
.expect("time is valid"),
),
presence: body.presence.clone(),
2020-07-30 16:14:47 +00:00
status_msg: body.status_msg.clone(),
},
sender: sender_user.clone(),
2020-07-30 16:14:47 +00:00
},
&db.globals,
)?;
}
db.flush().await?;
2020-07-30 16:14:47 +00:00
Ok(set_presence::Response.into())
}