From 3b0e59c8ab386746fb208124705d8c03eafef251 Mon Sep 17 00:00:00 2001 From: Geometrically <18202329+Geometrically@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:19:12 -0800 Subject: [PATCH] Switch to Sendy for newsletter (#2954) --- apps/labrinth/.env | 5 ++-- apps/labrinth/src/lib.rs | 5 ++-- apps/labrinth/src/routes/internal/flows.rs | 30 +++++++++++----------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/apps/labrinth/.env b/apps/labrinth/.env index 893f80b9..af01d17a 100644 --- a/apps/labrinth/.env +++ b/apps/labrinth/.env @@ -86,8 +86,9 @@ SITE_VERIFY_EMAIL_PATH=none SITE_RESET_PASSWORD_PATH=none SITE_BILLING_PATH=none -BEEHIIV_PUBLICATION_ID=none -BEEHIIV_API_KEY=none +SENDY_URL=none +SENDY_LIST_ID=none +SENDY_API_KEY=none ANALYTICS_ALLOWED_ORIGINS='["http://127.0.0.1:3000", "http://localhost:3000", "https://modrinth.com", "https://www.modrinth.com", "*"]' diff --git a/apps/labrinth/src/lib.rs b/apps/labrinth/src/lib.rs index 091b0998..d2108913 100644 --- a/apps/labrinth/src/lib.rs +++ b/apps/labrinth/src/lib.rs @@ -460,8 +460,9 @@ pub fn check_env_vars() -> bool { failed |= check_var::("SITE_RESET_PASSWORD_PATH"); failed |= check_var::("SITE_BILLING_PATH"); - failed |= check_var::("BEEHIIV_PUBLICATION_ID"); - failed |= check_var::("BEEHIIV_API_KEY"); + failed |= check_var::("SENDY_URL"); + failed |= check_var::("SENDY_LIST_ID"); + failed |= check_var::("SENDY_API_KEY"); if parse_strings_from_var("ANALYTICS_ALLOWED_ORIGINS").is_none() { warn!( diff --git a/apps/labrinth/src/routes/internal/flows.rs b/apps/labrinth/src/routes/internal/flows.rs index 7cc03b43..73c77b88 100644 --- a/apps/labrinth/src/routes/internal/flows.rs +++ b/apps/labrinth/src/routes/internal/flows.rs @@ -1430,23 +1430,23 @@ pub async fn delete_auth_provider( Ok(HttpResponse::NoContent().finish()) } -pub async fn sign_up_beehiiv(email: &str) -> Result<(), AuthenticationError> { - let id = dotenvy::var("BEEHIIV_PUBLICATION_ID")?; - let api_key = dotenvy::var("BEEHIIV_API_KEY")?; +pub async fn sign_up_sendy(email: &str) -> Result<(), AuthenticationError> { + let url = dotenvy::var("SENDY_URL")?; + let id = dotenvy::var("SENDY_LIST_ID")?; + let api_key = dotenvy::var("SENDY_API_KEY")?; let site_url = dotenvy::var("SITE_URL")?; + let mut form = HashMap::new(); + + form.insert("api_key", &*api_key); + form.insert("email", email); + form.insert("list", &*id); + form.insert("referrer", &*site_url); + let client = reqwest::Client::new(); client - .post(format!( - "https://api.beehiiv.com/v2/publications/{id}/subscriptions" - )) - .header(AUTHORIZATION, format!("Bearer {}", api_key)) - .json(&serde_json::json!({ - "email": email, - "utm_source": "modrinth", - "utm_medium": "account_creation", - "referring_site": site_url, - })) + .post(format!("{url}/subscribe")) + .form(&form) .send() .await? .error_for_status()? @@ -1578,7 +1578,7 @@ pub async fn create_account_with_password( )?; if new_account.sign_up_newsletter.unwrap_or(false) { - sign_up_beehiiv(&new_account.email).await?; + sign_up_sendy(&new_account.email).await?; } transaction.commit().await?; @@ -2453,7 +2453,7 @@ pub async fn subscribe_newsletter( .1; if let Some(email) = user.email { - sign_up_beehiiv(&email).await?; + sign_up_sendy(&email).await?; Ok(HttpResponse::NoContent().finish()) } else {