Skip to main content

tuwunel_service/admin/
notices.rs

1use futures::FutureExt;
2use ruma::{RoomId, events::room::message::RoomMessageEventContent};
3use tuwunel_core::{Result, implement};
4
5/// Sends a notice gated on `admin.room.notices`.
6///
7/// Admin command responses use the unconditional `notice()`.
8#[implement(super::Service)]
9pub async fn notify(&self, body: &str) {
10	if self.services.server.config.admin.room.notices {
11		self.notice(body).await;
12	}
13}
14
15/// Sends a message gated on `admin.room.notices`.
16///
17/// Admin command responses use the unconditional `send_text()`.
18#[implement(super::Service)]
19pub async fn notify_loud(&self, body: &str) {
20	if self.services.server.config.admin.room.notices {
21		self.send_text(body).await;
22	}
23}
24
25/// Sends markdown notice to the admin room as the admin user.
26#[implement(super::Service)]
27pub async fn notice(&self, body: &str) {
28	self.send_message(notice_markdown(body))
29		.await
30		.ok();
31}
32
33/// Sends markdown message (not an m.notice for notification reasons) to the
34/// admin room as the admin user.
35#[implement(super::Service)]
36pub async fn send_text(&self, body: &str) { self.send_message(text_markdown(body)).await.ok(); }
37
38/// Sends a markdown report to the configured report room, falling back to
39/// the admin room, as the server user.
40#[implement(super::Service)]
41pub async fn send_report(&self, body: &str) {
42	self.send_report_content(text_markdown(body))
43		.await;
44}
45
46/// Sends prepared report content to the configured report room.
47///
48/// Falls back to the admin room and sends as the server user. The supplied
49/// plaintext and formatted bodies are passed through without Markdown parsing.
50#[implement(super::Service)]
51#[tracing::instrument(level = "debug", skip_all)]
52pub async fn send_report_content(&self, content: RoomMessageEventContent) {
53	let Ok(room_id) = self.get_report_room().await else {
54		return;
55	};
56
57	self.send_to_room(content, &room_id).await.ok();
58}
59
60/// Sends a message to the admin room as the admin user (see send_text() for
61/// convenience).
62#[implement(super::Service)]
63pub async fn send_message(&self, message_content: RoomMessageEventContent) -> Result {
64	let room_id = self.get_admin_room().await?;
65
66	self.send_to_room(message_content, &room_id).await
67}
68
69#[implement(super::Service)]
70async fn send_to_room(&self, content: RoomMessageEventContent, room_id: &RoomId) -> Result {
71	let user_id = &self.services.globals.server_user;
72
73	self.respond_to_room(content, room_id, user_id)
74		.boxed() // size firewall
75		.await
76}
77
78/// Builds an `m.notice` from Markdown source.
79///
80/// The body keeps the source as given. Builds with the `markdown` feature add
81/// an HTML formatted body when the source uses Markdown syntax.
82#[cfg(feature = "markdown")]
83pub(super) fn notice_markdown(body: &str) -> RoomMessageEventContent {
84	RoomMessageEventContent::notice_markdown(body)
85}
86
87#[cfg(not(feature = "markdown"))]
88pub(super) fn notice_markdown(body: &str) -> RoomMessageEventContent {
89	RoomMessageEventContent::notice_plain(body)
90}
91
92/// Builds an `m.text` message from Markdown source.
93///
94/// The body keeps the source as given. Builds with the `markdown` feature add
95/// an HTML formatted body when the source uses Markdown syntax.
96#[cfg(feature = "markdown")]
97pub(super) fn text_markdown(body: &str) -> RoomMessageEventContent {
98	RoomMessageEventContent::text_markdown(body)
99}
100
101#[cfg(not(feature = "markdown"))]
102pub(super) fn text_markdown(body: &str) -> RoomMessageEventContent {
103	RoomMessageEventContent::text_plain(body)
104}