From b0ab736da54a6a267d9e9ed9bbc0113c79d08a5e Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Wed, 12 Jun 2024 21:15:33 -0700 Subject: [PATCH] add admin command to unset a room alias --- src/service/admin.rs | 45 ++++++++++++++++++++++++++++------- src/service/rooms/timeline.rs | 2 +- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/service/admin.rs b/src/service/admin.rs index 76b6dc15..39337861 100644 --- a/src/service/admin.rs +++ b/src/service/admin.rs @@ -23,8 +23,8 @@ use ruma::{ TimelineEventType, }, signatures::verify_json, - EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, RoomVersionId, - ServerName, UserId, + EventId, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId, OwnedRoomId, + RoomAliasId, RoomId, RoomVersionId, ServerName, UserId, }; use serde_json::value::to_raw_value; use tokio::sync::{mpsc, Mutex, RwLock}; @@ -179,6 +179,11 @@ enum AdminCommand { room_id: Box, }, + /// Remove a room alias. + UnsetAlias { + room_alias_id: OwnedRoomAliasId, + }, + /// Verify json signatures /// [commandbody]() /// # ``` @@ -200,7 +205,7 @@ enum AdminCommand { #[derive(Debug)] pub(crate) enum AdminRoomEvent { - ProcessMessage(String), + ProcessMessage(Box, String), SendMessage(RoomMessageEventContent), } @@ -247,8 +252,8 @@ impl Service { ) { let message_content = match event { AdminRoomEvent::SendMessage(content) => content, - AdminRoomEvent::ProcessMessage(room_message) => { - self.process_admin_message(room_message).await + AdminRoomEvent::ProcessMessage(pdu, room_message) => { + 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(), ), )] - pub(crate) fn process_message(&self, room_message: String) { - self.sender.send(AdminRoomEvent::ProcessMessage(room_message)).unwrap(); + pub(crate) fn process_message(&self, pdu: PduEvent, room_message: String) { + self.sender + .send(AdminRoomEvent::ProcessMessage(Box::new(pdu), room_message)) + .unwrap(); } #[tracing::instrument(skip(self, message_content))] @@ -311,6 +318,7 @@ impl Service { )] async fn process_admin_message( &self, + pdu: PduEvent, room_message: String, ) -> RoomMessageEventContent { 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, Err(error) => { let markdown_message = format!( @@ -388,6 +396,7 @@ impl Service { #[tracing::instrument(skip(self, body))] async fn process_admin_command( &self, + pdu: PduEvent, command: AdminCommand, body: Vec<&str>, ) -> Result { @@ -1081,6 +1090,9 @@ impl Service { ) } } + AdminCommand::UnsetAlias { + room_alias_id, + } => cmd_unset_alias(&room_alias_id, &pdu.sender), }; 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)] mod test { use super::*; diff --git a/src/service/rooms/timeline.rs b/src/service/rooms/timeline.rs index 2a0c8882..ed5d3e06 100644 --- a/src/service/rooms/timeline.rs +++ b/src/service/rooms/timeline.rs @@ -545,7 +545,7 @@ impl Service { && !from_grapevine && admin_room == pdu.room_id { - services().admin.process_message(body); + services().admin.process_message(pdu.clone(), body); } } }