You've already forked AstralRinth
forked from didirus/AstralRinth
Custom Emails (#4526)
* Dynamic email template * Set lower cache expiry for templates * Custom email route * Fix subject line on custom emails * chore: query cache, clippy, fmt * Bugfixes * Key-based caching on custom emails * Sequentially process emails prone to causing cache stampede * Fill variables in dynamic body + subject line * Update apps/labrinth/src/queue/email/templates.rs Co-authored-by: aecsocket <aecsocket@tutanota.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Update apps/labrinth/src/queue/email/templates.rs Co-authored-by: aecsocket <aecsocket@tutanota.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> --------- Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> Co-authored-by: aecsocket <aecsocket@tutanota.com>
This commit is contained in:
committed by
GitHub
parent
aec49cff7c
commit
0c66fa3f12
@@ -1,10 +1,15 @@
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database::models::ids::DBUserId;
|
||||
use crate::database::models::notification_item::NotificationBuilder;
|
||||
use crate::database::models::user_item::DBUser;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::users::Role;
|
||||
use crate::models::v3::notifications::NotificationBody;
|
||||
use crate::models::v3::pats::Scopes;
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::ApiError;
|
||||
use crate::util::guards::external_notification_key_guard;
|
||||
use actix_web::HttpRequest;
|
||||
use actix_web::web;
|
||||
use actix_web::{HttpResponse, post};
|
||||
use ariadne::ids::UserId;
|
||||
@@ -12,7 +17,7 @@ use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub fn config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(create);
|
||||
cfg.service(create).service(send_custom_email);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -50,3 +55,64 @@ pub async fn create(
|
||||
|
||||
Ok(HttpResponse::Accepted().finish())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SendEmail {
|
||||
pub users: Vec<UserId>,
|
||||
pub key: String,
|
||||
pub body_md: String,
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
#[post("external_notifications/send_custom_email")]
|
||||
pub async fn send_custom_email(
|
||||
req: HttpRequest,
|
||||
pool: web::Data<PgPool>,
|
||||
redis: web::Data<RedisPool>,
|
||||
session_queue: web::Data<AuthQueue>,
|
||||
body: web::Json<SendEmail>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let user = get_user_from_headers(
|
||||
&req,
|
||||
&**pool,
|
||||
&redis,
|
||||
&session_queue,
|
||||
Scopes::SESSION_ACCESS,
|
||||
)
|
||||
.await?
|
||||
.1;
|
||||
|
||||
if user.role != Role::Admin {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have permission to send custom emails!".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let SendEmail {
|
||||
users,
|
||||
body_md,
|
||||
title,
|
||||
key,
|
||||
} = body.into_inner();
|
||||
|
||||
let users = users
|
||||
.into_iter()
|
||||
.map(|x| DBUserId(x.0 as i64))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut txn = pool.begin().await?;
|
||||
|
||||
NotificationBuilder {
|
||||
body: NotificationBody::Custom {
|
||||
title,
|
||||
body_md,
|
||||
key,
|
||||
},
|
||||
}
|
||||
.insert_many(users, &mut txn, &redis)
|
||||
.await?;
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
Ok(HttpResponse::Accepted().finish())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user