Skip to content
Clone
ef0b83a
82 lines 2.4 KB Rust
use std::collections::HashMap;
use std::sync::Arc;
use config::AppConfig;
use export::AppState;
use export::database::Database;
use space::services::dto::UserDto;
use storage::BlobStore;
use crate::errors::ChatError;
use crate::share::realtime::Realtime;
#[derive(Clone)]
pub struct ChatService {
pub state: AppState,
pub realtime: Realtime,
}
impl ChatService {
pub fn new(state: AppState) -> Self {
let realtime = Realtime::new(state.clone());
Self { state, realtime }
}
pub fn config(&self) -> &AppConfig {
&self.state.config
}
pub fn db(&self) -> &Arc<dyn Database> {
&self.state.database
}
pub fn storage(&self) -> &dyn BlobStore {
self.state.storage.as_ref()
}
pub async fn load_user_briefs(&self, uids: &[i64]) -> Result<HashMap<i64, UserDto>, ChatError> {
Ok(export::users::load_user_briefs(&self.state, uids).await?)
}
/// Language for one recipient's notification/e-mail text.
///
/// Account preference only: a fan-out event has no single ambient request
/// language, and writing one language for every recipient is the bug this
/// replaces.
pub(crate) async fn recipient_locale(&self, uid: i64) -> i18n::UiLocale {
let fallback = i18n::config_fallback(self.config().email.default_locale());
i18n::resolve_user_locale(self.db().read(), uid, fallback).await
}
pub(crate) async fn primary_emails(
&self,
uids: &[i64],
) -> Result<HashMap<i64, String>, ChatError> {
if uids.is_empty() {
return Ok(HashMap::new());
}
let users = export::users::load_public_users(&self.state, uids).await?;
Ok(users
.into_iter()
.filter(|(_, user)| user.email_verified && !user.email.trim().is_empty())
.map(|(uid, user)| (uid, user.email.to_lowercase()))
.collect())
}
pub(crate) async fn send_email(
&self,
to: &str,
subject: &str,
text: &str,
html: &str,
) -> Result<(), ChatError> {
let Some(client) = self.state.mail.as_ref() else {
tracing::warn!(to, "email rpc disabled; skipping chat mail");
return Ok(());
};
client
.send_mail(to, subject, text, html, None)
.await
.map_err(|error| ChatError::Internal(format!("email send failed: {error}")))
}
}