Simplify api to send notices to admin room

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-04-26 23:04:58 +00:00 committed by Jade Ellis
parent 732a77f3a8
commit 21bbee8e3c
No known key found for this signature in database
GPG key ID: 8705A2A3EBF77BD2
3 changed files with 32 additions and 49 deletions

View file

@ -26,10 +26,7 @@ use ruma::{
}, },
events::{ events::{
GlobalAccountDataEventType, StateEventType, GlobalAccountDataEventType, StateEventType,
room::{ room::power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
message::RoomMessageEventContent,
power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
},
}, },
push, push,
}; };
@ -414,32 +411,21 @@ pub(crate) async fn register_route(
// log in conduit admin channel if a non-guest user registered // log in conduit admin channel if a non-guest user registered
if body.appservice_info.is_none() && !is_guest { if body.appservice_info.is_none() && !is_guest {
if !device_display_name.is_empty() { if !device_display_name.is_empty() {
info!( let notice = format!(
"New user \"{user_id}\" registered on this server with device display name: \ "New user \"{user_id}\" registered on this server from IP {client} and device \
\"{device_display_name}\"" display name \"{device_display_name}\""
); );
info!("{notice}");
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services.admin.notice(&notice).await;
.admin
.send_message(RoomMessageEventContent::notice_plain(format!(
"New user \"{user_id}\" registered on this server from IP {client} and \
device display name \"{device_display_name}\""
)))
.await
.ok();
} }
} else { } else {
info!("New user \"{user_id}\" registered on this server."); let notice = format!("New user \"{user_id}\" registered on this server.");
info!("{notice}");
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services.admin.notice(&notice).await;
.admin
.send_message(RoomMessageEventContent::notice_plain(format!(
"New user \"{user_id}\" registered on this server from IP {client}"
)))
.await
.ok();
} }
} }
} }
@ -452,24 +438,22 @@ pub(crate) async fn register_route(
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services
.admin .admin
.send_message(RoomMessageEventContent::notice_plain(format!( .notice(&format!(
"Guest user \"{user_id}\" with device display name \ "Guest user \"{user_id}\" with device display name \
\"{device_display_name}\" registered on this server from IP {client}" \"{device_display_name}\" registered on this server from IP {client}"
))) ))
.await .await;
.ok();
} }
} else { } else {
#[allow(clippy::collapsible_else_if)] #[allow(clippy::collapsible_else_if)]
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services
.admin .admin
.send_message(RoomMessageEventContent::notice_plain(format!( .notice(&format!(
"Guest user \"{user_id}\" with no device display name registered on \ "Guest user \"{user_id}\" with no device display name registered on \
this server from IP {client}", this server from IP {client}",
))) ))
.await .await;
.ok();
} }
} }
} }
@ -677,11 +661,8 @@ pub(crate) async fn change_password_route(
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services
.admin .admin
.send_message(RoomMessageEventContent::notice_plain(format!( .notice(&format!("User {sender_user} changed their password."))
"User {sender_user} changed their password." .await;
)))
.await
.ok();
} }
Ok(change_password::v3::Response {}) Ok(change_password::v3::Response {})
@ -784,11 +765,8 @@ pub(crate) async fn deactivate_route(
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services
.admin .admin
.send_message(RoomMessageEventContent::notice_plain(format!( .notice(&format!("User {sender_user} deactivated their account."))
"User {sender_user} deactivated their account." .await;
)))
.await
.ok();
} }
Ok(deactivate::v3::Response { Ok(deactivate::v3::Response {

View file

@ -92,19 +92,17 @@ pub(crate) async fn create_room_route(
&& !services.users.is_admin(sender_user).await && !services.users.is_admin(sender_user).await
&& body.appservice_info.is_none() && body.appservice_info.is_none()
{ {
info!( warn!(
"Non-admin user {sender_user} tried to publish {0} to the room directory while \ "Non-admin user {sender_user} tried to publish {room_id} to the room directory \
\"lockdown_public_room_directory\" is enabled", while \"lockdown_public_room_directory\" is enabled"
&room_id
); );
if services.server.config.admin_room_notices { if services.server.config.admin_room_notices {
services services
.admin .admin
.send_text(&format!( .notice(&format!(
"Non-admin user {sender_user} tried to publish {0} to the room directory \ "Non-admin user {sender_user} tried to publish {room_id} to the room \
while \"lockdown_public_room_directory\" is enabled", directory while \"lockdown_public_room_directory\" is enabled"
&room_id
)) ))
.await; .await;
} }

View file

@ -142,6 +142,13 @@ impl crate::Service for Service {
} }
impl Service { impl Service {
/// Sends markdown notice to the admin room as the admin user.
pub async fn notice(&self, body: &str) {
self.send_message(RoomMessageEventContent::notice_markdown(body))
.await
.ok();
}
/// Sends markdown message (not an m.notice for notification reasons) to the /// Sends markdown message (not an m.notice for notification reasons) to the
/// admin room as the admin user. /// admin room as the admin user.
pub async fn send_text(&self, body: &str) { pub async fn send_text(&self, body: &str) {