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

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

183 lines
5.4 KiB
Rust
Raw Normal View History

2024-05-23 01:27:04 -04:00
use std::fmt::Write;
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
use clap::Subcommand;
use conduit::Result;
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::{events::room::message::RoomMessageEventContent, OwnedRoomAliasId, OwnedRoomId, RoomAliasId, RoomId};
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
use crate::{escape_html, Command};
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
#[derive(Debug, Subcommand)]
pub(crate) enum RoomAliasCommand {
/// - Make an alias point to a room.
Set {
#[arg(short, long)]
/// Set the alias even if a room is already using it
force: bool,
/// The room id to set the alias on
room_id: Box<RoomId>,
/// The alias localpart to use (`alias`, not `#alias:servername.tld`)
room_alias_localpart: String,
},
/// - Remove a local alias
Remove {
/// The alias localpart to remove (`alias`, not `#alias:servername.tld`)
room_alias_localpart: String,
},
/// - Show which room is using an alias
Which {
/// The alias localpart to look up (`alias`, not
/// `#alias:servername.tld`)
room_alias_localpart: String,
},
/// - List aliases currently being used
List {
/// If set, only list the aliases for this room
room_id: Option<Box<RoomId>>,
},
}
pub(super) async fn process(command: RoomAliasCommand, context: &Command<'_>) -> Result<RoomMessageEventContent> {
let services = context.services;
let server_user = &services.globals.server_user;
2024-06-12 01:42:39 -04:00
2024-03-22 03:37:55 -07:00
match command {
RoomAliasCommand::Set {
ref room_alias_localpart,
..
}
| RoomAliasCommand::Remove {
ref room_alias_localpart,
}
| RoomAliasCommand::Which {
ref room_alias_localpart,
} => {
2024-07-27 00:11:41 +00:00
let room_alias_str = format!("#{}:{}", room_alias_localpart, services.globals.server_name());
2024-03-22 03:37:55 -07:00
let room_alias = match RoomAliasId::parse_box(room_alias_str) {
Ok(alias) => alias,
2024-06-02 00:15:02 +00:00
Err(err) => return Ok(RoomMessageEventContent::text_plain(format!("Failed to parse alias: {err}"))),
2024-03-22 03:37:55 -07:00
};
match command {
RoomAliasCommand::Set {
force,
room_id,
..
2024-08-08 17:18:30 +00:00
} => match (force, services.rooms.alias.resolve_local_alias(&room_alias).await) {
(true, Ok(id)) => match services
2024-06-12 01:42:39 -04:00
.rooms
.alias
.set_alias(&room_alias, &room_id, server_user)
2024-06-12 01:42:39 -04:00
{
2024-03-22 03:37:55 -07:00
Ok(()) => Ok(RoomMessageEventContent::text_plain(format!(
2024-06-02 00:15:02 +00:00
"Successfully overwrote alias (formerly {id})"
2024-03-22 03:37:55 -07:00
))),
2024-06-02 00:15:02 +00:00
Err(err) => Ok(RoomMessageEventContent::text_plain(format!("Failed to remove alias: {err}"))),
2024-03-22 03:37:55 -07:00
},
2024-08-08 17:18:30 +00:00
(false, Ok(id)) => Ok(RoomMessageEventContent::text_plain(format!(
2024-06-02 00:15:02 +00:00
"Refusing to overwrite in use alias for {id}, use -f or --force to overwrite"
2024-03-22 03:37:55 -07:00
))),
2024-08-08 17:18:30 +00:00
(_, Err(_)) => match services
2024-06-12 01:42:39 -04:00
.rooms
.alias
.set_alias(&room_alias, &room_id, server_user)
2024-06-12 01:42:39 -04:00
{
2024-03-22 03:37:55 -07:00
Ok(()) => Ok(RoomMessageEventContent::text_plain("Successfully set alias")),
Err(err) => Ok(RoomMessageEventContent::text_plain(format!("Failed to remove alias: {err}"))),
},
},
RoomAliasCommand::Remove {
..
2024-08-08 17:18:30 +00:00
} => match services.rooms.alias.resolve_local_alias(&room_alias).await {
Ok(id) => match services
2024-06-12 01:42:39 -04:00
.rooms
.alias
.remove_alias(&room_alias, server_user)
2024-06-12 01:42:39 -04:00
.await
{
2024-06-02 00:15:02 +00:00
Ok(()) => Ok(RoomMessageEventContent::text_plain(format!("Removed alias from {id}"))),
Err(err) => Ok(RoomMessageEventContent::text_plain(format!("Failed to remove alias: {err}"))),
2024-03-22 03:37:55 -07:00
},
2024-08-08 17:18:30 +00:00
Err(_) => Ok(RoomMessageEventContent::text_plain("Alias isn't in use.")),
2024-03-22 03:37:55 -07:00
},
RoomAliasCommand::Which {
..
2024-08-08 17:18:30 +00:00
} => match services.rooms.alias.resolve_local_alias(&room_alias).await {
Ok(id) => Ok(RoomMessageEventContent::text_plain(format!("Alias resolves to {id}"))),
Err(_) => Ok(RoomMessageEventContent::text_plain("Alias isn't in use.")),
2024-03-22 03:37:55 -07:00
},
RoomAliasCommand::List {
..
} => unreachable!(),
}
},
RoomAliasCommand::List {
room_id,
} => {
if let Some(room_id) = room_id {
2024-07-27 00:11:41 +00:00
let aliases = services
2024-03-25 17:05:11 -04:00
.rooms
.alias
.local_aliases_for_room(&room_id)
2024-08-08 17:18:30 +00:00
.map(Into::into)
.collect::<Vec<OwnedRoomAliasId>>()
.await;
let plain_list = aliases.iter().fold(String::new(), |mut output, alias| {
writeln!(output, "- {alias}").expect("should be able to write to string buffer");
output
});
let html_list = aliases.iter().fold(String::new(), |mut output, alias| {
writeln!(output, "<li>{}</li>", escape_html(alias.as_ref()))
.expect("should be able to write to string buffer");
output
});
let plain = format!("Aliases for {room_id}:\n{plain_list}");
let html = format!("Aliases for {room_id}:\n<ul>{html_list}</ul>");
Ok(RoomMessageEventContent::text_html(plain, html))
2024-03-22 03:37:55 -07:00
} else {
2024-07-27 00:11:41 +00:00
let aliases = services
2024-03-25 17:05:11 -04:00
.rooms
.alias
.all_local_aliases()
2024-08-08 17:18:30 +00:00
.map(|(room_id, localpart)| (room_id.into(), localpart.into()))
.collect::<Vec<(OwnedRoomId, String)>>()
.await;
let server_name = services.globals.server_name();
let plain_list = aliases
.iter()
.fold(String::new(), |mut output, (alias, id)| {
writeln!(output, "- `{alias}` -> #{id}:{server_name}")
.expect("should be able to write to string buffer");
output
});
let html_list = aliases
.iter()
.fold(String::new(), |mut output, (alias, id)| {
writeln!(
output,
"<li><code>{}</code> -> #{}:{}</li>",
escape_html(alias.as_ref()),
escape_html(id),
server_name
)
.expect("should be able to write to string buffer");
output
});
let plain = format!("Aliases:\n{plain_list}");
let html = format!("Aliases:\n<ul>{html_list}</ul>");
Ok(RoomMessageEventContent::text_html(plain, html))
2024-03-22 03:37:55 -07:00
}
},
}
}