179 lines
6.1 KiB
Rust
179 lines
6.1 KiB
Rust
use rocket::State;
|
|
use crate::helper::settings::Settings;
|
|
use crate::helper::session_cookies::model::SessionCookie;
|
|
use crate::modules::api::model::api_outcome::{ApiErrorWrapper, ApiError};
|
|
use rocket::serde::json::Json;
|
|
use crate::modules::api::member_management::controller::parser::parse_member_cookie;
|
|
use crate::helper::mail_queue::queue::{Mail, MailQueue};
|
|
|
|
use std::sync::{Arc};
|
|
|
|
use crate::database::controller::api_communication_targets::{get_member_email_addresses, get_group_email_addresses, get_unit_email_addresses};
|
|
|
|
use crate::helper::translate_diesel_error::translate_diesel;
|
|
|
|
use crate::database::controller::members::check_access_to_resource;
|
|
use crate::database::controller::groups::get_group;
|
|
|
|
use crate::database::controller::units::get_unit;
|
|
|
|
#[derive(Queryable, Clone, Deserialize, Serialize)]
|
|
pub struct ApiEmail{
|
|
pub(crate) to: Option<Vec<String>>,
|
|
pub(crate) to_members: Option<Vec<uuid::Uuid>>,
|
|
pub(crate) cc: Option<Vec<String>>,
|
|
pub(crate) cc_members: Option<Vec<uuid::Uuid>>,
|
|
pub(crate) bcc: Option<Vec<String>>,
|
|
pub(crate) bcc_members: Option<Vec<uuid::Uuid>>,
|
|
pub(crate) reply_to: Option<String>,
|
|
pub(crate) selected_groups: Option<Vec<uuid::Uuid>>,
|
|
pub(crate) selected_units: Option<Vec<uuid::Uuid>>,
|
|
pub(crate) subject: String,
|
|
pub(crate) body: String,
|
|
}
|
|
|
|
#[post("/api/communicator/email", format = "json", data = "<mail>")]
|
|
pub fn create_email(mq: &State<Arc<MailQueue>>, settings: &State<Settings>, cookie: SessionCookie, mail: Json<ApiEmail>) -> Result<(), Json<ApiErrorWrapper>>{
|
|
let caller = parse_member_cookie(cookie.member)?;
|
|
if !caller.has_permission(crate::permissions::modules::communicator::email::SEND.to_string()){
|
|
return Err(Json(ApiError::new(403, "Keine Berechtigung Email zu versenden!".to_string()).to_wrapper()));
|
|
}
|
|
|
|
let mail = mail.into_inner();
|
|
|
|
let mut to: Vec<String> = vec![];
|
|
let mut cc: Vec<String> = vec![];
|
|
let mut bcc: Vec<String> = vec![];
|
|
|
|
match mail.to{
|
|
Some(mut mail) => {
|
|
to.append(mail.as_mut())
|
|
}
|
|
None => {}
|
|
}
|
|
match mail.to_members{
|
|
Some(members) => {
|
|
for member_id in members{
|
|
match get_member_email_addresses(settings, member_id){
|
|
Ok(mut addresses) => {
|
|
to.append(addresses.as_mut());
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
None => {}
|
|
}
|
|
match mail.cc{
|
|
Some(mut mail) => {
|
|
cc.append(mail.as_mut())
|
|
}
|
|
None => {}
|
|
}
|
|
match mail.cc_members{
|
|
Some(members) => {
|
|
for member_id in members{
|
|
match get_member_email_addresses(settings, member_id){
|
|
Ok(mut addresses) => {
|
|
cc.append(addresses.as_mut());
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
None => {}
|
|
}
|
|
match mail.bcc{
|
|
Some(mut mail) => {
|
|
bcc.append(mail.as_mut())
|
|
}
|
|
None => {}
|
|
}
|
|
match mail.bcc_members{
|
|
Some(members) => {
|
|
for member_id in members{
|
|
match get_member_email_addresses(settings, member_id){
|
|
Ok(mut addresses) => {
|
|
bcc.append(addresses.as_mut());
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
None => {}
|
|
}
|
|
|
|
match mail.selected_groups{
|
|
None => {}
|
|
Some(groups) => {
|
|
for group in groups{
|
|
if !check_access_to_resource(settings, caller.entity_id, group, crate::permissions::modules::communicator::email::SEND){
|
|
match get_group(settings, group){
|
|
Ok(group) => {
|
|
return Err(Json(ApiError::new(403, format!("Keine Berechtigung eine Email an die Gruppe {} zu schicken!", group.name)).to_wrapper()))
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
match get_group_email_addresses(settings, group){
|
|
Ok(mut emails) => {
|
|
bcc.append(emails.as_mut());
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
match mail.selected_units{
|
|
None => {}
|
|
Some(units) => {
|
|
for unit in units{
|
|
if !check_access_to_resource(settings, caller.entity_id, unit, crate::permissions::modules::communicator::email::SEND){
|
|
match get_unit(settings, unit){
|
|
Ok(unit) => {
|
|
return Err(Json(ApiError::new(403, format!("Keine Berechtigung eine Email an die Einheit {} zu schicken!", unit.name)).to_wrapper()))
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
match get_unit_email_addresses(settings, unit){
|
|
Ok(mut emails) => {
|
|
bcc.append(emails.as_mut());
|
|
}
|
|
Err(e) => {
|
|
return Err(translate_diesel(e))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if to.is_empty() && cc.is_empty() && bcc.is_empty(){
|
|
return Err(Json(ApiError::new(422, "Es muss mindestens ein Empfänger angegeben werden!".to_string()).to_wrapper()))
|
|
}
|
|
|
|
let composed_mail = Mail::new(settings.mail.from.clone(), to,mail.subject,cc,bcc,mail.reply_to,mail.body,None);
|
|
|
|
match mq.add_mail(composed_mail){
|
|
Ok(_) => Ok(()),
|
|
Err(_) => Err(Json(ApiError::new(500, "Couldn't add mail to mail queue!".to_string()).to_wrapper()))
|
|
}
|
|
|
|
}
|