Skip to content

Commit 3593ec7

Browse files
committed
backend: add branding to email templates
1 parent 9d6dec3 commit 3593ec7

17 files changed

+792
-63
lines changed

backend/src/api_docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use std::net::Ipv4Addr;
2121

2222
use actix_web::{middleware::Logger, App, HttpServer};
23-
pub use backend::*;
23+
use backend::*;
2424
use log::LevelFilter;
2525
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode};
2626
use utoipa::{

backend/src/branding.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// Branding configuration for the application
2+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3+
pub enum Brand {
4+
Warp,
5+
Seb,
6+
}
7+
8+
impl Brand {
9+
/// Parse brand from environment variable or string
10+
pub fn from_env() -> Self {
11+
std::env::var("BRAND")
12+
.ok()
13+
.and_then(|s| Self::from_str(&s))
14+
.unwrap_or(Brand::Warp)
15+
}
16+
17+
/// Parse brand from string
18+
pub fn from_str(s: &str) -> Option<Self> {
19+
match s.to_lowercase().as_str() {
20+
"seb" => Some(Brand::Seb),
21+
"warp" => Some(Brand::Warp),
22+
_ => None,
23+
}
24+
}
25+
}
26+
27+
impl Default for Brand {
28+
fn default() -> Self {
29+
Brand::Warp
30+
}
31+
}

backend/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use udp_server::{
4242
management::RemoteConnMeta, packet::ManagementResponseV2, socket::ManagementSocket,
4343
};
4444

45+
pub mod branding;
4546
pub mod error;
4647
pub mod middleware;
4748
pub mod models;
@@ -90,6 +91,7 @@ pub struct AppState {
9091
pub frontend_url: String,
9192
pub sender_email: String,
9293
pub sender_name: String,
94+
pub brand: crate::branding::Brand,
9395
pub keys_in_use: Mutex<HashSet<uuid::Uuid>>,
9496
}
9597

@@ -302,6 +304,7 @@ pub(crate) mod tests {
302304
frontend_url: std::env::var("FRONTEND_URL").expect("FRONTEND_URL must be set!"),
303305
sender_email: String::new(),
304306
sender_name: String::new(),
307+
brand: crate::branding::Brand::default(),
305308
keys_in_use: Mutex::new(HashSet::new()),
306309
};
307310

backend/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ async fn main() -> std::io::Result<()> {
141141

142142
let sender_email = std::env::var("SENDER_EMAIL").expect("SENDER_EMAIL must be set");
143143
let sender_name = std::env::var("SENDER_NAME").expect("SENDER_NAME must be set");
144+
let brand = backend::branding::Brand::from_env();
144145

145146
let state = web::Data::new(AppState {
146147
pool: pool.clone(),
@@ -149,6 +150,7 @@ async fn main() -> std::io::Result<()> {
149150
frontend_url: std::env::var("FRONTEND_URL").expect("FRONTEND_URL must be set!"),
150151
sender_email,
151152
sender_name,
153+
brand,
152154
keys_in_use: Mutex::new(HashSet::new()),
153155
});
154156

backend/src/routes/auth/register.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,23 @@ use utoipa::ToSchema;
3232
use validator::Validate;
3333

3434
use crate::{
35-
error::Error,
36-
routes::auth::VERIFICATION_EXPIRATION_DAYS,
37-
utils::{self, get_connection},
38-
AppState,
35+
AppState, branding, error::Error, routes::auth::VERIFICATION_EXPIRATION_DAYS, utils::{self, get_connection}
3936
};
4037

4138
#[derive(Template)]
4239
#[template(path = "email_verification_en.html")]
4340
pub struct VerifyEmailENTemplate<'a> {
4441
pub name: &'a str,
4542
pub link: &'a str,
43+
pub brand: branding::Brand,
4644
}
4745

4846
#[derive(Template)]
4947
#[template(path = "email_verification_de.html")]
5048
pub struct VerifyEmailDETemplate<'a> {
5149
pub name: &'a str,
5250
pub link: &'a str,
51+
pub brand: branding::Brand,
5352
}
5453

5554
#[derive(Debug, Deserialize, Serialize, Validate, Clone, ToSchema)]
@@ -96,6 +95,7 @@ fn send_verification_mail(
9695
let template = VerifyEmailDETemplate {
9796
name: &name,
9897
link: &link,
98+
brand: state.brand,
9999
};
100100
match template.render() {
101101
Ok(body) => (body, "Email verifizieren"),
@@ -111,6 +111,7 @@ fn send_verification_mail(
111111
let template = VerifyEmailENTemplate {
112112
name: &name,
113113
link: &link,
114+
brand: state.brand,
114115
};
115116
match template.render() {
116117
Ok(body) => (body, "Verify email"),

backend/src/routes/auth/resend_verification.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use serde::{Deserialize, Serialize};
66
use utoipa::ToSchema;
77

88
use crate::{
9-
error::Error,
10-
routes::auth::VERIFICATION_EXPIRATION_DAYS,
11-
utils::{get_connection, web_block_unpacked},
12-
AppState,
9+
AppState, branding, error::Error, routes::auth::VERIFICATION_EXPIRATION_DAYS, utils::{get_connection, web_block_unpacked}
1310
};
1411

1512
use db_connector::models::{users::User, verification::Verification};
@@ -19,13 +16,15 @@ use db_connector::models::{users::User, verification::Verification};
1916
pub struct VerifyEmailENTemplate<'a> {
2017
pub name: &'a str,
2118
pub link: &'a str,
19+
pub brand: branding::Brand,
2220
}
2321

2422
#[derive(Template)]
2523
#[template(path = "email_verification_de.html")]
2624
pub struct VerifyEmailDETemplate<'a> {
2725
pub name: &'a str,
2826
pub link: &'a str,
27+
pub brand: branding::Brand,
2928
}
3029

3130
#[derive(Deserialize, ToSchema, Serialize)]
@@ -48,6 +47,7 @@ fn send_verification_mail(
4847
let template = VerifyEmailDETemplate {
4948
name: &name,
5049
link: &link,
50+
brand: state.brand,
5151
};
5252
match template.render() {
5353
Ok(body) => (body, "Email verifizieren"),
@@ -61,6 +61,7 @@ fn send_verification_mail(
6161
let template = VerifyEmailENTemplate {
6262
name: &name,
6363
link: &link,
64+
brand: state.brand,
6465
};
6566
match template.render() {
6667
Ok(body) => (body, "Verify email"),

backend/src/routes/auth/start_recovery.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use utoipa::IntoParams;
77
use uuid::Uuid;
88

99
use crate::{
10-
error::Error,
11-
routes::user::{get_user, get_user_id},
12-
utils::{self, get_connection, web_block_unpacked},
13-
AppState,
10+
AppState, branding, error::Error, routes::user::{get_user, get_user_id}, utils::{self, get_connection, web_block_unpacked}
1411
};
1512

1613
#[derive(Deserialize, IntoParams)]
@@ -23,13 +20,15 @@ struct StartRecoveryQuery {
2320
struct StartRecoveryDETemplate<'a> {
2421
name: &'a str,
2522
link: &'a str,
23+
brand: branding::Brand,
2624
}
2725

2826
#[derive(Template)]
2927
#[template(path = "start_recovery_en.html")]
3028
struct StartRecoveryENTemplate<'a> {
3129
name: &'a str,
3230
link: &'a str,
31+
brand: branding::Brand,
3332
}
3433

3534
#[allow(unused)]
@@ -50,6 +49,7 @@ fn send_email(
5049
let template = StartRecoveryDETemplate {
5150
name: &name,
5251
link: &link,
52+
brand: state.brand,
5353
};
5454
match template.render() {
5555
Ok(b) => (b, "Passwort Wiederherstellung"),
@@ -63,6 +63,7 @@ fn send_email(
6363
let template = StartRecoveryENTemplate {
6464
name: &name,
6565
link: &link,
66+
brand: state.brand,
6667
};
6768
match template.render() {
6869
Ok(b) => (b, "Password Recovery"),

backend/src/routes/send_chargelog_to_user.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ use std::io::Read;
66
use utoipa::ToSchema;
77

88
use crate::{
9-
error::Error,
10-
rate_limit::ChargerRateLimiter,
11-
routes::{charger::add::password_matches, user::get_user},
12-
utils::{get_charger_from_db, parse_uuid, send_email_with_attachment},
13-
AppState,
9+
AppState, branding, error::Error, rate_limit::ChargerRateLimiter, routes::{charger::add::password_matches, user::get_user}, utils::{get_charger_from_db, parse_uuid, send_email_with_attachment}
1410
};
1511

1612
#[derive(Serialize, Deserialize, ToSchema)]
@@ -38,6 +34,7 @@ struct ChargelogDETemplate<'a> {
3834
month: &'a str,
3935
display_name: &'a str,
4036
monthly_send: bool,
37+
brand: branding::Brand,
4138
}
4239

4340
#[derive(Template)]
@@ -47,6 +44,7 @@ struct ChargelogENTemplate<'a> {
4744
month: &'a str,
4845
display_name: &'a str,
4946
monthly_send: bool,
47+
brand: branding::Brand,
5048
}
5149

5250
fn render_chargelog_email(
@@ -55,6 +53,7 @@ fn render_chargelog_email(
5553
display_name: &str,
5654
lang: &str,
5755
monthly_send: bool,
56+
brand: branding::Brand,
5857
) -> actix_web::Result<(String, String)> {
5958
let (body, subject) = match lang {
6059
"de" | "de-DE" => {
@@ -63,6 +62,7 @@ fn render_chargelog_email(
6362
month,
6463
display_name,
6564
monthly_send,
65+
brand,
6666
};
6767
match template.render() {
6868
Ok(b) => {
@@ -89,6 +89,7 @@ fn render_chargelog_email(
8989
month,
9090
display_name,
9191
monthly_send,
92+
brand,
9293
};
9394
match template.render() {
9495
Ok(b) => {
@@ -167,6 +168,7 @@ pub async fn send_chargelog(
167168
&metadata.display_name,
168169
&lang_str,
169170
metadata.monthly_send,
171+
state.brand,
170172
)?;
171173

172174
let mut chargelog_file = chargelog.file.reopen().map_err(|err| {

backend/src/routes/user/update_user.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919

2020
use crate::{
21-
error::Error,
22-
routes::auth::VERIFICATION_EXPIRATION_DAYS,
23-
utils::{get_connection, send_email, web_block_unpacked},
24-
AppState,
21+
AppState, branding, error::Error, routes::auth::VERIFICATION_EXPIRATION_DAYS, utils::{get_connection, send_email, web_block_unpacked}
2522
};
2623
use actix_web::{error::ErrorConflict, put, web, HttpResponse, Responder};
2724
use askama::Template;
@@ -37,6 +34,7 @@ use validator::Validate;
3734
struct EmailChangeNotificationEn {
3835
name: String,
3936
sender_email: String,
37+
brand: branding::Brand,
4038
}
4139

4240
#[allow(unused)]
@@ -45,6 +43,7 @@ struct EmailChangeNotificationEn {
4543
struct EmailChangeNotificationDe {
4644
name: String,
4745
sender_email: String,
46+
brand: branding::Brand,
4847
}
4948

5049
#[allow(unused)]
@@ -60,6 +59,7 @@ fn send_email_change_notification(
6059
let template = EmailChangeNotificationDe {
6160
name: name.to_string(),
6261
sender_email: state.sender_email.clone(),
62+
brand: state.brand,
6363
};
6464
match template.render() {
6565
Ok(body) => (body, "E-Mail-Adresse geändert"),
@@ -73,6 +73,7 @@ fn send_email_change_notification(
7373
let template = EmailChangeNotificationEn {
7474
name: name.to_string(),
7575
sender_email: state.sender_email.clone(),
76+
brand: state.brand,
7677
};
7778
match template.render() {
7879
Ok(body) => (body, "Email address changed"),
@@ -106,6 +107,7 @@ fn send_verification_mail(
106107
"{}/api/auth/verify?id={}",
107108
state.frontend_url, verification_id
108109
),
110+
brand: state.brand,
109111
};
110112
match template.render() {
111113
Ok(body) => (body, "E-Mail-Adresse bestätigen"),
@@ -124,6 +126,7 @@ fn send_verification_mail(
124126
"{}/api/auth/verify?id={}",
125127
state.frontend_url, verification_id
126128
),
129+
brand: state.brand,
127130
};
128131
match template.render() {
129132
Ok(body) => (body, "Verify email address"),

0 commit comments

Comments
 (0)