Files
continuwuity/src/admin/room/info.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
1.8 KiB
Rust
Raw Normal View History

2024-07-27 00:11:41 +00:00
use clap::Subcommand;
use conduwuit::{Err, Result, utils::ReadyExt};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::OwnedRoomId;
2024-07-27 00:11:41 +00:00
use crate::{admin_command, admin_command_dispatch};
2024-07-27 00:11:41 +00:00
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
2025-05-24 00:28:09 +01:00
pub enum RoomInfoCommand {
2024-07-27 00:11:41 +00:00
/// - List joined members in a room
ListJoinedMembers {
room_id: OwnedRoomId,
/// Lists only our local users in the specified room
#[arg(long)]
local_only: bool,
2024-07-27 00:11:41 +00:00
},
/// - Displays room topic
///
/// Room topics can be huge, so this is in its
/// own separate command
ViewRoomTopic {
room_id: OwnedRoomId,
2024-07-27 00:11:41 +00:00
},
}
2024-07-27 00:11:41 +00:00
#[admin_command]
async fn list_joined_members(&self, room_id: OwnedRoomId, local_only: bool) -> Result {
2024-07-27 00:11:41 +00:00
let room_name = self
.services
.rooms
.state_accessor
.get_name(&room_id)
2024-08-08 17:18:30 +00:00
.await
.unwrap_or_else(|_| room_id.to_string());
2024-08-08 17:18:30 +00:00
let member_info: Vec<_> = self
2024-07-27 00:11:41 +00:00
.services
.rooms
.state_cache
.room_members(&room_id)
2024-08-08 17:18:30 +00:00
.ready_filter(|user_id| {
2024-10-01 02:47:39 +00:00
local_only
.then(|| self.services.globals.user_is_local(user_id))
.unwrap_or(true)
2024-08-08 17:18:30 +00:00
})
2024-10-01 02:47:39 +00:00
.map(ToOwned::to_owned)
2024-08-08 17:18:30 +00:00
.filter_map(|user_id| async move {
Some((
2024-07-27 00:11:41 +00:00
self.services
.users
.displayname(&user_id)
2024-08-08 17:18:30 +00:00
.await
.unwrap_or_else(|_| user_id.to_string()),
user_id,
))
})
2024-08-08 17:18:30 +00:00
.collect()
.await;
let num = member_info.len();
let body = member_info
.into_iter()
.map(|(displayname, mxid)| format!("{mxid} | {displayname}"))
.collect::<Vec<_>>()
.join("\n");
self.write_str(&format!("{num} Members in Room \"{room_name}\":\n```\n{body}\n```",))
.await
}
2024-07-27 00:11:41 +00:00
#[admin_command]
async fn view_room_topic(&self, room_id: OwnedRoomId) -> Result {
2024-08-08 17:18:30 +00:00
let Ok(room_topic) = self
2024-07-27 00:11:41 +00:00
.services
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.get_room_topic(&room_id)
.await
2024-07-27 00:11:41 +00:00
else {
return Err!("Room does not have a room topic set.");
};
self.write_str(&format!("Room topic:\n```\n{room_topic}\n```"))
.await
}