HTML and plain text for new post mail notifications

This commit is contained in:
Alphonse Paix
2025-09-21 03:45:29 +02:00
parent 56b25515f9
commit 0725b87bf2
5 changed files with 222 additions and 25 deletions

View File

@@ -112,7 +112,7 @@ pub async fn publish_newsletter(
fn validate_form(form: &BodyData) -> Result<(), &'static str> {
if form.title.is_empty() {
return Err("The title was empty");
return Err("The title was empty.");
}
if form.html.is_empty() || form.text.is_empty() {
return Err("The content was empty.");

View File

@@ -3,7 +3,7 @@ use crate::{
idempotency::{IdempotencyKey, save_response, try_processing},
routes::{AdminError, AppError, enqueue_delivery_tasks, insert_newsletter_issue},
startup::AppState,
templates::MessageTemplate,
templates::{MessageTemplate, NewPostEmailTemplate},
};
use anyhow::Context;
use askama::Template;
@@ -34,7 +34,9 @@ fn validate_form(form: &CreatePostForm) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Creating a post", skip(connection_pool, form))]
pub async fn create_post(
State(AppState {
connection_pool, ..
connection_pool,
base_url,
..
}): State<AppState>,
Extension(AuthenticatedUser { user_id, .. }): Extension<AuthenticatedUser>,
Form(form): Form<CreatePostForm>,
@@ -57,7 +59,7 @@ pub async fn create_post(
.await
.context("Failed to insert new post in the database.")?;
let newsletter_uuid = create_newsletter(&mut transaction, &form.title, &form.content, &post_id)
let newsletter_uuid = create_newsletter(&mut transaction, &base_url, &form.title, &post_id)
.await
.context("Failed to create newsletter.")?;
@@ -103,14 +105,21 @@ pub async fn insert_post(
#[tracing::instrument(
name = "Creating newsletter for new post",
skip(transaction, title, content, _post_id)
skip(transaction, post_title, post_id)
)]
pub async fn create_newsletter(
transaction: &mut Transaction<'static, Postgres>,
title: &str,
content: &str,
_post_id: &Uuid,
base_url: &str,
post_title: &str,
post_id: &Uuid,
) -> Result<Uuid, sqlx::Error> {
// We need to send a special link with a unique ID to determine if the user clicked it or not.
insert_newsletter_issue(transaction, title, content, content).await
let template = NewPostEmailTemplate {
base_url,
post_title,
post_id,
post_excerpt: "",
};
let html_content = template.render().unwrap();
let text_content = template.text_version();
insert_newsletter_issue(transaction, post_title, &text_content, &html_content).await
}