5f84089
use std::sync::Arc;
use config::AppConfig;
use export::{AppDatabase, AppState, AppStorage};
use crate::errors::SpaceError;
#[derive(Clone)]
pub struct SpaceService {
pub state: AppState,
}
impl SpaceService {
pub fn new(state: AppState) -> Self {
Self { state }
}
pub fn config(&self) -> &AppConfig {
&self.state.config
}
pub fn db(&self) -> &Arc<AppDatabase> {
&self.state.database
}
pub fn storage(&self) -> &Arc<AppStorage> {
&self.state.storage
}
/// Language for one recipient's space mail.
pub(crate) async fn recipient_locale(&self, uid: Option<i64>) -> i18n::UiLocale {
let fallback = i18n::config_fallback(self.config().email.default_locale());
match uid {
Some(uid) => i18n::resolve_user_locale(self.db().read(), uid, fallback).await,
None => fallback,
}
}
pub(crate) async fn send_email(
&self,
to: &str,
subject: &str,
text: &str,
html: &str,
) -> Result<(), SpaceError> {
let Some(client) = self.state.mail.as_ref() else {
tracing::warn!(to, "email rpc disabled; skipping space mail");
return Ok(());
};
client
.send_mail(to, subject, text, html, None)
.await
.map_err(|error| SpaceError::Internal(format!("email send failed: {error}")))
}
}