2023-07-10 16:41:00 +02:00
|
|
|
use std::time::Duration;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2025-03-31 02:28:01 +00:00
|
|
|
use conduwuit::{Err, Result};
|
|
|
|
use ruma::api::client::presence::{get_presence, set_presence};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-03-31 02:28:01 +00:00
|
|
|
use crate::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-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn set_presence_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
body: Ruma<set_presence::v3::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<set_presence::v3::Response> {
|
2025-03-31 02:28:01 +00:00
|
|
|
if !services.config.allow_local_presence {
|
|
|
|
return Err!(Request(Forbidden("Presence is disabled on this server")));
|
2023-09-08 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
2025-03-31 02:28:01 +00:00
|
|
|
if body.sender_user() != body.user_id && body.appservice_info.is_none() {
|
|
|
|
return Err!(Request(InvalidParam("Not allowed to set presence of other users")));
|
2024-07-23 22:46:20 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-04-01 20:48:40 -07:00
|
|
|
.presence
|
2025-03-31 02:28:01 +00:00
|
|
|
.set_presence(body.sender_user(), &body.presence, None, None, body.status_msg.clone())
|
2024-08-08 17:18:30 +00:00
|
|
|
.await?;
|
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-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn get_presence_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
body: Ruma<get_presence::v3::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<get_presence::v3::Response> {
|
2025-03-31 02:28:01 +00:00
|
|
|
if !services.config.allow_local_presence {
|
|
|
|
return Err!(Request(Forbidden("Presence is disabled on this server",)));
|
2023-09-08 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 11:03:18 +02:00
|
|
|
let mut presence_event = None;
|
2024-08-08 17:18:30 +00:00
|
|
|
let has_shared_rooms = services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
2024-11-20 20:21:31 +00:00
|
|
|
.state_cache
|
2025-03-31 02:28:01 +00:00
|
|
|
.user_sees_user(body.sender_user(), &body.user_id)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await;
|
|
|
|
|
|
|
|
if has_shared_rooms {
|
|
|
|
if let Ok(presence) = services.presence.get_presence(&body.user_id).await {
|
2021-05-14 11:03:18 +02:00
|
|
|
presence_event = Some(presence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
match presence_event {
|
|
|
|
| Some(presence) => {
|
|
|
|
let status_msg = if presence
|
2025-01-08 17:57:12 +08:00
|
|
|
.content
|
2025-02-23 01:17:45 -05:00
|
|
|
.status_msg
|
|
|
|
.as_ref()
|
|
|
|
.is_some_and(String::is_empty)
|
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
presence.content.status_msg
|
|
|
|
};
|
|
|
|
|
|
|
|
let last_active_ago = match presence.content.currently_active {
|
|
|
|
| Some(true) => None,
|
|
|
|
| _ => presence
|
|
|
|
.content
|
|
|
|
.last_active_ago
|
|
|
|
.map(|millis| Duration::from_millis(millis.into())),
|
|
|
|
};
|
2025-01-08 17:57:12 +08:00
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
Ok(get_presence::v3::Response {
|
|
|
|
// TODO: Should ruma just use the presenceeventcontent type here?
|
|
|
|
status_msg,
|
|
|
|
currently_active: presence.content.currently_active,
|
|
|
|
last_active_ago,
|
|
|
|
presence: presence.content.presence,
|
|
|
|
})
|
|
|
|
},
|
2025-03-31 02:28:01 +00:00
|
|
|
| _ => Err!(Request(NotFound("Presence state for this user was not found"))),
|
2021-05-14 11:03:18 +02:00
|
|
|
}
|
|
|
|
}
|