2024-07-22 07:43:51 +00:00
|
|
|
use conduit_core::{err, Err, Result};
|
2024-06-09 21:13:53 -04:00
|
|
|
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
|
2024-07-22 07:43:51 +00:00
|
|
|
use service::Services;
|
2024-05-09 15:59:08 -07:00
|
|
|
|
|
|
|
pub(crate) fn escape_html(s: &str) -> String {
|
|
|
|
s.replace('&', "&")
|
|
|
|
.replace('<', "<")
|
|
|
|
.replace('>', ">")
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(crate) async fn get_room_info(services: &Services, room_id: &RoomId) -> (OwnedRoomId, u64, String) {
|
2024-05-09 15:59:08 -07:00
|
|
|
(
|
2024-08-08 17:18:30 +00:00
|
|
|
room_id.into(),
|
2024-07-22 07:43:51 +00:00
|
|
|
services
|
2024-05-09 15:59:08 -07:00
|
|
|
.rooms
|
|
|
|
.state_cache
|
2024-08-08 17:18:30 +00:00
|
|
|
.room_joined_count(room_id)
|
|
|
|
.await
|
2024-05-09 15:59:08 -07:00
|
|
|
.unwrap_or(0),
|
2024-07-22 07:43:51 +00:00
|
|
|
services
|
2024-05-09 15:59:08 -07:00
|
|
|
.rooms
|
|
|
|
.state_accessor
|
2024-08-08 17:18:30 +00:00
|
|
|
.get_name(room_id)
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|_| room_id.to_string()),
|
2024-05-09 15:59:08 -07:00
|
|
|
)
|
|
|
|
}
|
2024-06-09 21:13:53 -04:00
|
|
|
|
|
|
|
/// Parses user ID
|
2024-07-22 07:43:51 +00:00
|
|
|
pub(crate) fn parse_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
|
|
|
|
UserId::parse_with_server_name(user_id.to_lowercase(), services.globals.server_name())
|
2024-07-13 21:11:05 +00:00
|
|
|
.map_err(|e| err!("The supplied username is not a valid username: {e}"))
|
2024-06-09 21:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses user ID as our local user
|
2024-07-22 07:43:51 +00:00
|
|
|
pub(crate) fn parse_local_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
|
|
|
|
let user_id = parse_user_id(services, user_id)?;
|
2024-06-09 21:13:53 -04:00
|
|
|
|
2024-07-22 07:43:51 +00:00
|
|
|
if !services.globals.user_is_local(&user_id) {
|
2024-07-13 21:11:05 +00:00
|
|
|
return Err!("User {user_id:?} does not belong to our server.");
|
2024-06-09 21:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(user_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses user ID that is an active (not guest or deactivated) local user
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(crate) async fn parse_active_local_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
|
2024-07-22 07:43:51 +00:00
|
|
|
let user_id = parse_local_user_id(services, user_id)?;
|
2024-06-09 21:13:53 -04:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
if !services.users.exists(&user_id).await {
|
2024-07-13 21:11:05 +00:00
|
|
|
return Err!("User {user_id:?} does not exist on this server.");
|
2024-06-09 21:13:53 -04:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
if services.users.is_deactivated(&user_id).await? {
|
2024-07-13 21:11:05 +00:00
|
|
|
return Err!("User {user_id:?} is deactivated.");
|
2024-06-09 21:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(user_id)
|
|
|
|
}
|