diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index 1439cc28..7c48fa08 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -179,6 +179,9 @@ enum UserCommand { /// - List local users in the database List, + + /// - Lists all the rooms (local and remote) that the specified user is joined in + ListJoinedRooms { user_id: Box }, } #[cfg_attr(test, derive(Debug))] @@ -1005,6 +1008,46 @@ impl Service { ) } } + UserCommand::ListJoinedRooms { user_id } => { + let mut rooms: Vec<(OwnedRoomId, u64, String)> = vec![]; // room ID, members joined, room name + + for room_id in services().rooms.state_cache.rooms_joined(&user_id) { + let room_id = room_id?; + rooms.push(Self::get_room_info(room_id)); + } + + if rooms.is_empty() { + return Ok(RoomMessageEventContent::text_plain( + "User is not in any rooms.", + )); + } + + rooms.sort_by_key(|r| r.1); + rooms.reverse(); + + let output_plain = format!( + "Rooms {user_id} Joined:\n{}", + rooms + .iter() + .map(|(id, members, name)| format!( + "{id}\tMembers: {members}\tName: {name}" + )) + .collect::>() + .join("\n") + ); + let output_html = format!( + "\n\t\t\n{}
Rooms {user_id} Joined
idmembersname
", + rooms + .iter() + .fold(String::new(), |mut output, (id, members, name)| { + writeln!(output, "{}\t{}\t{}", escape_html(id.as_ref()), + members, + escape_html(name)).unwrap(); + output + }) + ); + RoomMessageEventContent::text_html(output_plain, output_html) + } }, AdminCommand::Rooms(command) => match command { RoomCommand::Moderation(command) => match command {