Skip to content

Commit

Permalink
Switch to Sendy for newsletter (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically authored Nov 19, 2024
1 parent 1f23038 commit 3b0e59c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
5 changes: 3 additions & 2 deletions apps/labrinth/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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", "*"]'

Expand Down
5 changes: 3 additions & 2 deletions apps/labrinth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,9 @@ pub fn check_env_vars() -> bool {
failed |= check_var::<String>("SITE_RESET_PASSWORD_PATH");
failed |= check_var::<String>("SITE_BILLING_PATH");

failed |= check_var::<String>("BEEHIIV_PUBLICATION_ID");
failed |= check_var::<String>("BEEHIIV_API_KEY");
failed |= check_var::<String>("SENDY_URL");
failed |= check_var::<String>("SENDY_LIST_ID");
failed |= check_var::<String>("SENDY_API_KEY");

if parse_strings_from_var("ANALYTICS_ALLOWED_ORIGINS").is_none() {
warn!(
Expand Down
30 changes: 15 additions & 15 deletions apps/labrinth/src/routes/internal/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3b0e59c

Please sign in to comment.