add admin command to unset a room alias

This commit is contained in:
Charles Hall 2024-06-12 21:15:33 -07:00
parent 70fa17dde0
commit b0ab736da5
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
2 changed files with 38 additions and 9 deletions

View file

@ -23,8 +23,8 @@ use ruma::{
TimelineEventType, TimelineEventType,
}, },
signatures::verify_json, signatures::verify_json,
EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, RoomVersionId, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId, OwnedRoomId,
ServerName, UserId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tokio::sync::{mpsc, Mutex, RwLock}; use tokio::sync::{mpsc, Mutex, RwLock};
@ -179,6 +179,11 @@ enum AdminCommand {
room_id: Box<RoomId>, room_id: Box<RoomId>,
}, },
/// Remove a room alias.
UnsetAlias {
room_alias_id: OwnedRoomAliasId,
},
/// Verify json signatures /// Verify json signatures
/// [commandbody]() /// [commandbody]()
/// # ``` /// # ```
@ -200,7 +205,7 @@ enum AdminCommand {
#[derive(Debug)] #[derive(Debug)]
pub(crate) enum AdminRoomEvent { pub(crate) enum AdminRoomEvent {
ProcessMessage(String), ProcessMessage(Box<PduEvent>, String),
SendMessage(RoomMessageEventContent), SendMessage(RoomMessageEventContent),
} }
@ -247,8 +252,8 @@ impl Service {
) { ) {
let message_content = match event { let message_content = match event {
AdminRoomEvent::SendMessage(content) => content, AdminRoomEvent::SendMessage(content) => content,
AdminRoomEvent::ProcessMessage(room_message) => { AdminRoomEvent::ProcessMessage(pdu, room_message) => {
self.process_admin_message(room_message).await self.process_admin_message(*pdu, room_message).await
} }
}; };
@ -290,8 +295,10 @@ impl Service {
room_message = truncate_str_for_debug(&room_message, 50).as_ref(), room_message = truncate_str_for_debug(&room_message, 50).as_ref(),
), ),
)] )]
pub(crate) fn process_message(&self, room_message: String) { pub(crate) fn process_message(&self, pdu: PduEvent, room_message: String) {
self.sender.send(AdminRoomEvent::ProcessMessage(room_message)).unwrap(); self.sender
.send(AdminRoomEvent::ProcessMessage(Box::new(pdu), room_message))
.unwrap();
} }
#[tracing::instrument(skip(self, message_content))] #[tracing::instrument(skip(self, message_content))]
@ -311,6 +318,7 @@ impl Service {
)] )]
async fn process_admin_message( async fn process_admin_message(
&self, &self,
pdu: PduEvent,
room_message: String, room_message: String,
) -> RoomMessageEventContent { ) -> RoomMessageEventContent {
let mut lines = room_message.lines().filter(|l| !l.trim().is_empty()); let mut lines = room_message.lines().filter(|l| !l.trim().is_empty());
@ -333,7 +341,7 @@ impl Service {
} }
}; };
match self.process_admin_command(admin_command, body).await { match self.process_admin_command(pdu, admin_command, body).await {
Ok(reply_message) => reply_message, Ok(reply_message) => reply_message,
Err(error) => { Err(error) => {
let markdown_message = format!( let markdown_message = format!(
@ -388,6 +396,7 @@ impl Service {
#[tracing::instrument(skip(self, body))] #[tracing::instrument(skip(self, body))]
async fn process_admin_command( async fn process_admin_command(
&self, &self,
pdu: PduEvent,
command: AdminCommand, command: AdminCommand,
body: Vec<&str>, body: Vec<&str>,
) -> Result<RoomMessageEventContent> { ) -> Result<RoomMessageEventContent> {
@ -1081,6 +1090,9 @@ impl Service {
) )
} }
} }
AdminCommand::UnsetAlias {
room_alias_id,
} => cmd_unset_alias(&room_alias_id, &pdu.sender),
}; };
Ok(reply_message_content) Ok(reply_message_content)
@ -1548,6 +1560,23 @@ impl Service {
} }
} }
/// Remove an alias from a room
///
/// This authenticates the command against the command's sender.
fn cmd_unset_alias(
room_alias_id: &RoomAliasId,
user_id: &UserId,
) -> RoomMessageEventContent {
let res = services().rooms.alias.remove_alias(room_alias_id, user_id);
let res = match res {
Ok(()) => "Successfully removed room alias.".to_owned(),
Err(e) => format!("Failed to remove room alias: {e}"),
};
RoomMessageEventContent::text_plain(res)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View file

@ -545,7 +545,7 @@ impl Service {
&& !from_grapevine && !from_grapevine
&& admin_room == pdu.room_id && admin_room == pdu.room_id
{ {
services().admin.process_message(body); services().admin.process_message(pdu.clone(), body);
} }
} }
} }