continuwuity/src/admin/context.rs
Jade Ellis e01181d7e5
Some checks failed
Checks / Prefligit / prefligit (push) Failing after 4s
Release Docker Image / define-variables (push) Failing after 6s
Release Docker Image / build-image (linux/amd64, release, linux-amd64, base) (push) Has been skipped
Release Docker Image / build-image (linux/arm64, release, linux-arm64, base) (push) Has been skipped
Release Docker Image / merge (push) Has been skipped
Checks / Rust / Format (push) Failing after 5s
Checks / Rust / Clippy (push) Failing after 11s
Checks / Rust / Cargo Test (push) Failing after 11s
feat: Pass sender through admin commands
2025-06-29 15:17:27 +01:00

52 lines
1.4 KiB
Rust

use std::{fmt, time::SystemTime};
use conduwuit::Result;
use conduwuit_service::Services;
use futures::{
Future, FutureExt, TryFutureExt,
io::{AsyncWriteExt, BufWriter},
lock::Mutex,
};
use ruma::{EventId, UserId};
pub(crate) struct Context<'a> {
pub(crate) services: &'a Services,
pub(crate) body: &'a [&'a str],
pub(crate) timer: SystemTime,
pub(crate) reply_id: Option<&'a EventId>,
pub(crate) sender: Option<&'a UserId>,
pub(crate) output: Mutex<BufWriter<Vec<u8>>>,
}
impl Context<'_> {
pub(crate) fn write_fmt(
&self,
arguments: fmt::Arguments<'_>,
) -> impl Future<Output = Result> + Send + '_ + use<'_> {
let buf = format!("{arguments}");
self.output.lock().then(async move |mut output| {
output.write_all(buf.as_bytes()).map_err(Into::into).await
})
}
pub(crate) fn write_str<'a>(
&'a self,
s: &'a str,
) -> impl Future<Output = Result> + Send + 'a {
self.output.lock().then(async move |mut output| {
output.write_all(s.as_bytes()).map_err(Into::into).await
})
}
/// Get the sender of the admin command, if available
pub(crate) fn sender(&self) -> Option<&UserId> { self.sender }
/// Check if the command has sender information
pub(crate) fn has_sender(&self) -> bool { self.sender.is_some() }
/// Get the sender as a string, or service user ID if not available
pub(crate) fn sender_or_service_user(&self) -> &UserId {
self.sender
.unwrap_or_else(|| self.services.globals.server_user.as_ref())
}
}