use peisear_core::notifications::channel as channel_id;
use crate::config::SmtpConfig;
use crate::dispatch::DispatchEvent;
use crate::email::EmailError;
#[derive(Debug, thiserror::Error)]
pub enum ChannelSendError {
#[error("channel skipped: {0}")]
Skipped(&'static str),
#[error("send error: {0}")]
Send(String),
#[error(transparent)]
Email(#[from] EmailError),
#[error("unknown channel: {0}")]
UnknownChannel(String),
}
pub async fn send_via_channel(
channel: &str,
event: &DispatchEvent,
smtp: Option<&SmtpConfig>,
user_email: Option<&str>,
) -> Result<(), ChannelSendError> {
match channel {
channel_id::IN_APP => {
Ok(())
}
channel_id::EMAIL => {
let Some(cfg) = smtp else {
tracing::debug!(
user_id = %event.user_id,
kind = %event.kind,
"email channel: SMTP not configured, skipping",
);
return Err(ChannelSendError::Skipped("SMTP not configured"));
};
let Some(to) = user_email else {
tracing::debug!(
user_id = %event.user_id,
kind = %event.kind,
"email channel: no recipient address, skipping",
);
return Err(ChannelSendError::Skipped("recipient email unknown"));
};
crate::email::send_email(cfg, to, &event.title, &event.body).await?;
tracing::info!(
user_id = %event.user_id,
kind = %event.kind,
"email channel: delivered",
);
Ok(())
}
channel_id::WEBHOOK => {
tracing::info!(
user_id = %event.user_id,
kind = %event.kind,
title = %event.title,
"[webhook-stub] would POST notification",
);
Ok(())
}
other => Err(ChannelSendError::UnknownChannel(other.to_string())),
}
}