conduit/src/client_server/presence.rs

93 lines
2.9 KiB
Rust
Raw Normal View History

2021-07-14 07:07:08 +00:00
use crate::{database::DatabaseGuard, utils, ConduitResult, Ruma};
2021-05-14 09:03:18 +00:00
use ruma::api::client::r0::presence::{get_presence, set_presence};
2021-07-14 07:07:08 +00:00
use std::{convert::TryInto, time::Duration};
2020-07-30 16:14:47 +00:00
#[cfg(feature = "conduit_bin")]
2021-05-14 09:03:18 +00:00
use rocket::{get, put};
2020-07-30 16:14:47 +00:00
#[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(
2021-07-14 07:07:08 +00:00
db: DatabaseGuard,
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())
}
2021-05-14 09:03:18 +00:00
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/client/r0/presence/<_>/status", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn get_presence_route(
2021-07-14 07:07:08 +00:00
db: DatabaseGuard,
2021-05-14 09:03:18 +00:00
body: Ruma<get_presence::Request<'_>>,
) -> ConduitResult<get_presence::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut presence_event = None;
for room_id in db
.rooms
2021-06-08 16:10:00 +00:00
.get_shared_rooms(vec![sender_user.clone(), body.user_id.clone()])?
2021-05-14 09:03:18 +00:00
{
let room_id = room_id?;
if let Some(presence) = db
.rooms
.edus
.get_last_presence_event(&sender_user, &room_id)?
{
presence_event = Some(presence);
}
}
if let Some(presence) = presence_event {
Ok(get_presence::Response {
// TODO: Should ruma just use the presenceeventcontent type here?
status_msg: presence.content.status_msg,
currently_active: presence.content.currently_active,
last_active_ago: presence
.content
.last_active_ago
.map(|millis| Duration::from_millis(millis.into())),
presence: presence.content.presence,
}
.into())
} else {
todo!();
}
}