split admin room moderation commands

prior stack frame allocated 170 KiB

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-09 04:40:19 +00:00
parent fc1b8326e6
commit db2c9f28b6
2 changed files with 430 additions and 428 deletions

View file

@ -2,6 +2,6 @@ array-size-threshold = 4096
cognitive-complexity-threshold = 94 # TODO reduce me ALARA cognitive-complexity-threshold = 94 # TODO reduce me ALARA
excessive-nesting-threshold = 11 # TODO reduce me to 4 or 5 excessive-nesting-threshold = 11 # TODO reduce me to 4 or 5
future-size-threshold = 7745 # TODO reduce me ALARA future-size-threshold = 7745 # TODO reduce me ALARA
stack-size-threshold = 173577 # reduce me ALARA stack-size-threshold = 144000 # reduce me ALARA
too-many-lines-threshold = 700 # TODO reduce me to <= 100 too-many-lines-threshold = 700 # TODO reduce me to <= 100
type-complexity-threshold = 250 # reduce me to ~200 type-complexity-threshold = 250 # reduce me to ~200

View file

@ -18,7 +18,22 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
force, force,
room, room,
disable_federation, disable_federation,
} => { } => ban_room(body, force, room, disable_federation).await,
RoomModerationCommand::BanListOfRooms {
force,
disable_federation,
} => ban_list_of_rooms(body, force, disable_federation).await,
RoomModerationCommand::UnbanRoom {
room,
enable_federation,
} => unban_room(body, room, enable_federation).await,
RoomModerationCommand::ListBannedRooms => list_banned_rooms(body).await,
}
}
async fn ban_room(
_body: Vec<&str>, force: bool, room: Box<RoomOrAliasId>, disable_federation: bool,
) -> Result<RoomMessageEventContent> {
debug!("Got room alias or ID: {}", room); debug!("Got room alias or ID: {}", room);
let admin_room_alias: Box<RoomAliasId> = format!("#admins:{}", services().globals.server_name()) let admin_room_alias: Box<RoomAliasId> = format!("#admins:{}", services().globals.server_name())
@ -59,17 +74,14 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
}; };
debug!( debug!(
"Room specified is not a room ID, attempting to resolve room alias to a room ID locally, if not \ "Room specified is not a room ID, attempting to resolve room alias to a room ID locally, if not using \
using get_alias_helper to fetch room ID remotely" get_alias_helper to fetch room ID remotely"
); );
let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? { let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? {
room_id room_id
} else { } else {
debug!( debug!("We don't have this room alias to a room ID locally, attempting to fetch room ID over federation");
"We don't have this room alias to a room ID locally, attempting to fetch room ID over \
federation"
);
match get_alias_helper(room_alias, None).await { match get_alias_helper(room_alias, None).await {
Ok(response) => { Ok(response) => {
@ -153,9 +165,8 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
&local_user, &room_id, e &local_user, &room_id, e
); );
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"Error attempting to make local user {} leave room {} during room banning (room is still \ "Error attempting to make local user {} leave room {} during room banning (room is still banned \
banned but not removing any more users): {}\nIf you would like to ignore errors, use \ but not removing any more users): {}\nIf you would like to ignore errors, use --force",
--force",
&local_user, &room_id, e &local_user, &room_id, e
))); )));
} }
@ -170,14 +181,12 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
} }
Ok(RoomMessageEventContent::text_plain( Ok(RoomMessageEventContent::text_plain(
"Room banned and removed all our local users, use `!admin federation disable-room` to stop receiving \ "Room banned and removed all our local users, use `!admin federation disable-room` to stop receiving new \
new inbound federation events as well if needed.", inbound federation events as well if needed.",
)) ))
}, }
RoomModerationCommand::BanListOfRooms {
force, async fn ban_list_of_rooms(body: Vec<&str>, force: bool, disable_federation: bool) -> Result<RoomMessageEventContent> {
disable_federation,
} => {
if body.len() > 2 && body[0].trim().starts_with("```") && body.last().unwrap().trim() == "```" { if body.len() > 2 && body[0].trim().starts_with("```") && body.last().unwrap().trim() == "```" {
let rooms_s = body.clone().drain(1..body.len() - 1).collect::<Vec<_>>(); let rooms_s = body.clone().drain(1..body.len() - 1).collect::<Vec<_>>();
@ -205,15 +214,15 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
if force { if force {
// ignore rooms we failed to parse if we're force banning // ignore rooms we failed to parse if we're force banning
warn!( warn!(
"Error parsing room \"{room}\" during bulk room banning, ignoring \ "Error parsing room \"{room}\" during bulk room banning, ignoring error and \
error and logging here: {e}" logging here: {e}"
); );
continue; continue;
} }
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"{room} is not a valid room ID or room alias, please fix the list and try \ "{room} is not a valid room ID or room alias, please fix the list and try again: \
again: {e}" {e}"
))); )));
}, },
}; };
@ -224,21 +233,19 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
if room_alias_or_id.is_room_alias_id() { if room_alias_or_id.is_room_alias_id() {
match RoomAliasId::parse(room_alias_or_id) { match RoomAliasId::parse(room_alias_or_id) {
Ok(room_alias) => { Ok(room_alias) => {
let room_id = if let Some(room_id) = let room_id =
services().rooms.alias.resolve_local_alias(&room_alias)? if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? {
{
room_id room_id
} else { } else {
debug!( debug!(
"We don't have this room alias to a room ID locally, attempting to \ "We don't have this room alias to a room ID locally, attempting to fetch \
fetch room ID over federation" room ID over federation"
); );
match get_alias_helper(room_alias, None).await { match get_alias_helper(room_alias, None).await {
Ok(response) => { Ok(response) => {
debug!( debug!(
"Got federation response fetching room ID for room {room}: \ "Got federation response fetching room ID for room {room}: {:?}",
{:?}",
response response
); );
response.room_id response.room_id
@ -263,15 +270,15 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
if force { if force {
// ignore rooms we failed to parse if we're force deleting // ignore rooms we failed to parse if we're force deleting
error!( error!(
"Error parsing room \"{room}\" during bulk room banning, ignoring \ "Error parsing room \"{room}\" during bulk room banning, ignoring error and \
error and logging here: {e}" logging here: {e}"
); );
continue; continue;
} }
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"{room} is not a valid room ID or room alias, please fix the list and try \ "{room} is not a valid room ID or room alias, please fix the list and try again: \
again: {e}" {e}"
))); )));
}, },
} }
@ -281,8 +288,8 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
if force { if force {
// ignore rooms we failed to parse if we're force deleting // ignore rooms we failed to parse if we're force deleting
error!( error!(
"Error parsing room \"{room}\" during bulk room banning, ignoring error and \ "Error parsing room \"{room}\" during bulk room banning, ignoring error and logging here: \
logging here: {e}" {e}"
); );
continue; continue;
} }
@ -327,8 +334,7 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
.collect::<Vec<OwnedUserId>>() .collect::<Vec<OwnedUserId>>()
{ {
debug!( debug!(
"Attempting leave for user {} in room {} (forced, ignoring all errors, evicting \ "Attempting leave for user {} in room {} (forced, ignoring all errors, evicting admins too)",
admins too)",
&local_user, room_id &local_user, room_id
); );
if let Err(e) = leave_room(&local_user, &room_id, None).await { if let Err(e) = leave_room(&local_user, &room_id, None).await {
@ -362,9 +368,9 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
&local_user, &room_id, e &local_user, &room_id, e
); );
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"Error attempting to make local user {} leave room {} during room banning (room \ "Error attempting to make local user {} leave room {} during room banning (room is still \
is still banned but not removing any more users and not banning any more rooms): \ banned but not removing any more users and not banning any more rooms): {}\nIf you would \
{}\nIf you would like to ignore errors, use --force", like to ignore errors, use --force",
&local_user, &room_id, e &local_user, &room_id, e
))); )));
} }
@ -390,11 +396,11 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
Ok(RoomMessageEventContent::text_plain( Ok(RoomMessageEventContent::text_plain(
"Expected code block in command body. Add --help for details.", "Expected code block in command body. Add --help for details.",
)) ))
}, }
RoomModerationCommand::UnbanRoom {
room, async fn unban_room(
enable_federation, _body: Vec<&str>, room: Box<RoomOrAliasId>, enable_federation: bool,
} => { ) -> Result<RoomMessageEventContent> {
let room_id = if room.is_room_id() { let room_id = if room.is_room_id() {
let room_id = match RoomId::parse(&room) { let room_id = match RoomId::parse(&room) {
Ok(room_id) => room_id, Ok(room_id) => room_id,
@ -423,17 +429,14 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
}; };
debug!( debug!(
"Room specified is not a room ID, attempting to resolve room alias to a room ID locally, if not \ "Room specified is not a room ID, attempting to resolve room alias to a room ID locally, if not using \
using get_alias_helper to fetch room ID remotely" get_alias_helper to fetch room ID remotely"
); );
let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? { let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? {
room_id room_id
} else { } else {
debug!( debug!("We don't have this room alias to a room ID locally, attempting to fetch room ID over federation");
"We don't have this room alias to a room ID locally, attempting to fetch room ID over \
federation"
);
match get_alias_helper(room_alias, None).await { match get_alias_helper(room_alias, None).await {
Ok(response) => { Ok(response) => {
@ -464,11 +467,12 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
} }
Ok(RoomMessageEventContent::text_plain( Ok(RoomMessageEventContent::text_plain(
"Room unbanned, you may need to re-enable federation with the room using enable-room if this is a \ "Room unbanned, you may need to re-enable federation with the room using enable-room if this is a remote room \
remote room to make it fully functional.", to make it fully functional.",
)) ))
}, }
RoomModerationCommand::ListBannedRooms => {
async fn list_banned_rooms(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
let rooms = services() let rooms = services()
.rooms .rooms
.metadata .metadata
@ -499,6 +503,4 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
Ok(RoomMessageEventContent::text_plain(format!("Unable to list room aliases: {e}"))) Ok(RoomMessageEventContent::text_plain(format!("Unable to list room aliases: {e}")))
}, },
} }
},
}
} }