2023-07-10 16:41:00 +02:00
|
|
|
use std::time::Duration;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2023-07-10 16:41:00 +02:00
|
|
|
use ruma::api::client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
presence::{get_presence, set_presence},
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-03 13:24:04 +01:00
|
|
|
use crate::{services, Error, Result, Ruma};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/presence/{userId}/status`
|
|
|
|
///
|
|
|
|
/// Sets the presence state of the sender user.
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn set_presence_route(body: Ruma<set_presence::v3::Request>) -> Result<set_presence::v3::Response> {
|
2023-09-08 14:36:39 +02:00
|
|
|
if !services().globals.allow_local_presence() {
|
2024-04-03 15:59:03 -04:00
|
|
|
return Err(Error::BadRequest(ErrorKind::forbidden(), "Presence is disabled on this server"));
|
2023-09-08 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2024-07-23 22:46:20 -04:00
|
|
|
|
|
|
|
if sender_user != &body.user_id && body.appservice_info.is_none() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Not allowed to set presence of other users",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:48:40 -07:00
|
|
|
services()
|
|
|
|
.presence
|
|
|
|
.set_presence(sender_user, &body.presence, None, None, body.status_msg.clone())?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(set_presence::v3::Response {})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
2021-05-14 11:03:18 +02:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/presence/{userId}/status`
|
|
|
|
///
|
|
|
|
/// Gets the presence state of the given user.
|
|
|
|
///
|
|
|
|
/// - Only works if you share a room with the user
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_presence_route(body: Ruma<get_presence::v3::Request>) -> Result<get_presence::v3::Response> {
|
2023-09-08 14:36:39 +02:00
|
|
|
if !services().globals.allow_local_presence() {
|
2024-04-03 15:59:03 -04:00
|
|
|
return Err(Error::BadRequest(ErrorKind::forbidden(), "Presence is disabled on this server"));
|
2023-09-08 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 11:03:18 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
let mut presence_event = None;
|
|
|
|
|
2024-04-01 20:48:40 -07:00
|
|
|
for _room_id in services()
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
.user
|
|
|
|
.get_shared_rooms(vec![sender_user.clone(), body.user_id.clone()])?
|
|
|
|
{
|
2024-05-02 03:55:25 -04:00
|
|
|
if let Some(presence) = services().presence.get_presence(&body.user_id)? {
|
2021-05-14 11:03:18 +02:00
|
|
|
presence_event = Some(presence);
|
2021-08-31 19:14:37 +02:00
|
|
|
break;
|
2021-05-14 11:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(presence) = presence_event {
|
2024-05-04 09:03:32 -04:00
|
|
|
let status_msg = if presence
|
|
|
|
.content
|
|
|
|
.status_msg
|
|
|
|
.as_ref()
|
|
|
|
.is_some_and(String::is_empty)
|
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
presence.content.status_msg
|
|
|
|
};
|
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_presence::v3::Response {
|
2021-05-14 11:03:18 +02:00
|
|
|
// TODO: Should ruma just use the presenceeventcontent type here?
|
2024-05-04 09:03:32 -04:00
|
|
|
status_msg,
|
2021-05-14 11:03:18 +02:00
|
|
|
currently_active: presence.content.currently_active,
|
2024-03-25 17:05:11 -04:00
|
|
|
last_active_ago: presence
|
|
|
|
.content
|
|
|
|
.last_active_ago
|
|
|
|
.map(|millis| Duration::from_millis(millis.into())),
|
2021-05-14 11:03:18 +02:00
|
|
|
presence: presence.content.presence,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2021-05-14 11:03:18 +02:00
|
|
|
} else {
|
2023-07-10 16:41:00 +02:00
|
|
|
Err(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
2023-12-20 21:46:05 -05:00
|
|
|
"Presence state for this user was not found",
|
2023-07-10 16:41:00 +02:00
|
|
|
))
|
2021-05-14 11:03:18 +02:00
|
|
|
}
|
|
|
|
}
|