1use futures::StreamExt;
2use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
3use tuwunel_core::{Err, Result, err};
4use tuwunel_service::Services;
5
6pub(crate) async fn get_room_info(
7 services: &Services,
8 room_id: &RoomId,
9) -> (OwnedRoomId, u64, String) {
10 let join_count = services
11 .state_cache
12 .room_joined_count(room_id)
13 .await
14 .unwrap_or(0);
15
16 let name = match services.state_accessor.get_name(room_id).await {
17 | Ok(name) => name,
18 | Err(_) if join_count == 2 => services
19 .state_cache
20 .room_members(room_id)
21 .map(ToString::to_string)
22 .collect::<Vec<_>>()
23 .await
24 .join(", "),
25 | Err(_) => room_id.to_string(),
26 };
27
28 (room_id.into(), join_count, name)
29}
30
31pub(crate) fn parse_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
33 UserId::parse_with_server_name(user_id.to_lowercase(), services.globals.server_name())
34 .map_err(|e| err!("The supplied username is not a valid username: {e}"))
35}
36
37pub(crate) fn parse_local_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
39 let user_id = parse_user_id(services, user_id)?;
40
41 if !services.globals.user_is_local(&user_id) {
42 return Err!("User {user_id:?} does not belong to our server.");
43 }
44
45 Ok(user_id)
46}
47
48pub(crate) async fn parse_active_local_user_id(
50 services: &Services,
51 user_id: &str,
52) -> Result<OwnedUserId> {
53 let user_id = parse_local_user_id(services, user_id)?;
54
55 if !services.users.exists(&user_id).await {
56 return Err!("User {user_id:?} does not exist on this server.");
57 }
58
59 if services.users.is_deactivated(&user_id).await? {
60 return Err!("User {user_id:?} is deactivated.");
61 }
62
63 Ok(user_id)
64}