From a8f57f06216867acadf54ecef843446aa5c4b74d Mon Sep 17 00:00:00 2001 From: Awiteb Date: Fri, 19 Apr 2024 00:13:12 +0300 Subject: [PATCH] chore: Remove the `Service::send_message` function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove `Service::send_message` function and rename `Service::send_message_with_result` to `Service::send_message` - Log the `Service::send_message` error - Remove `AdminRoomEvent::SendMessage` variant Based-on: e09e55dba31523007b51521a1ab6552099a0118f Suggested-by: Timo Kösters Signed-off-by: Awiteb --- src/api/client_server/account.rs | 30 +++++++++++++++++++++--------- src/api/client_server/report.rs | 8 +++++--- src/database/mod.rs | 14 ++++++++++---- src/service/admin/mod.rs | 21 +++------------------ 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 9b6bb5f8..89e88b77 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -249,11 +249,15 @@ pub async fn register_route(body: Ruma) -> Result { if pwd_set { warn!("The Conduit account emergency password is set! Please unset it as soon as you finish admin account recovery!"); - services().admin.send_message(RoomMessageEventContent::text_plain("The Conduit account emergency password is set! Please unset it as soon as you finish admin account recovery!")); + if let Err(err) = services().admin.send_message( + &RoomMessageEventContent::text_plain("The Conduit account emergency password is set! Please unset it as soon as you finish admin account recovery!") + ).await { + tracing::error!("Failed to send a message to admin room: {err}"); + }; } } Err(e) => { @@ -1043,12 +1047,14 @@ impl KeyValueDatabase { last_update_id = last_update_id.max(update.id); if update.id > services().globals.last_check_for_updates_id()? { println!("{}", update.message); - services() + if let Err(err) = services() .admin - .send_message(RoomMessageEventContent::text_plain(format!( + .send_message(&RoomMessageEventContent::text_plain(format!( "@room: The following is a message from the Conduit developers. It was sent on '{}':\n\n{}", update.date, update.message - ))) + ))).await { + tracing::error!("Failed to send a message to admin room: {err}"); + } } } services() diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index 49193368..624809d5 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -184,7 +184,6 @@ enum AdminCommand { #[derive(Debug)] pub enum AdminRoomEvent { ProcessMessage(String, ruma::OwnedEventId), - SendMessage(RoomMessageEventContent), } pub struct Service { @@ -221,7 +220,6 @@ impl Service { tokio::select! { Some(event) = receiver.recv() => { let message_content = match event { - AdminRoomEvent::SendMessage(content) => Some(content), AdminRoomEvent::ProcessMessage(room_message, event_id) => self.process_admin_message(room_message, &event_id).await }; if let Some(message_content) = message_content { @@ -269,12 +267,6 @@ impl Service { .unwrap(); } - pub fn send_message(&self, message_content: RoomMessageEventContent) { - self.sender - .send(AdminRoomEvent::SendMessage(message_content)) - .unwrap(); - } - /// Delete user message in the conduit admin room. pub async fn delete_user_message( &self, @@ -322,15 +314,8 @@ impl Service { /// Send the message content and return it's result /// - /// Note: This is different from send_message, as it sends the message - /// and returns the sending result, unlike [`send_message`], which will send - /// the message via [`handler`] and ignore the sending result. - /// /// Note: Will return Ok(None) if there is no admin room - /// - /// [`send_message`]: Self::send_message() - /// [`handler`]: Self::handler() - pub async fn send_message_with_result( + pub async fn send_message( &self, message_content: &RoomMessageEventContent, ) -> Result>> { @@ -715,7 +700,7 @@ impl Service { // need it's event id to delete it after 60s let Some(sended_message_event_id) = services() .admin - .send_message_with_result(&RoomMessageEventContent::text_plain(format!( + .send_message(&RoomMessageEventContent::text_plain(format!( "Successfully reset the password for user {user_id}: {new_password} (This message will be deleted after 60s)" ))) .await? @@ -816,7 +801,7 @@ impl Service { // need it's event id to delete it after 60s let Some(sended_message_event_id) = services() .admin - .send_message_with_result(&RoomMessageEventContent::text_plain(format!( + .send_message(&RoomMessageEventContent::text_plain(format!( "Created user with user_id: {user_id} and password: {password} (This message will be deleted after 60s)" ))) .await?