| 1 |
|
| 2 |
|
| 3 |
use axum::{ |
| 4 |
Form, |
| 5 |
extract::State, |
| 6 |
response::{Html, IntoResponse, Response}, |
| 7 |
}; |
| 8 |
use serde::Deserialize; |
| 9 |
|
| 10 |
use sqlx::PgPool; |
| 11 |
|
| 12 |
use crate::{auth::AuthUser, db, error::Result, templates::SaveStatusTemplate}; |
| 13 |
|
| 14 |
|
| 15 |
#[derive(Debug, Deserialize)] |
| 16 |
pub(crate) struct UpdatePreferencesForm { |
| 17 |
pub notify_sale: Option<String>, |
| 18 |
pub notify_follower: Option<String>, |
| 19 |
pub notify_release: Option<String>, |
| 20 |
pub login_notification_enabled: Option<String>, |
| 21 |
pub notify_issues: Option<String>, |
| 22 |
pub notify_status: Option<String>, |
| 23 |
pub tips_enabled: Option<String>, |
| 24 |
pub notify_tip: Option<String>, |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[tracing::instrument(skip_all, name = "users::update_preferences")] |
| 29 |
pub(in crate::routes::api) async fn update_preferences( |
| 30 |
State(db): State<PgPool>, |
| 31 |
AuthUser(user): AuthUser, |
| 32 |
Form(form): Form<UpdatePreferencesForm>, |
| 33 |
) -> Result<Response> { |
| 34 |
let notify_sale = form.notify_sale.as_deref() == Some("on"); |
| 35 |
let notify_follower = form.notify_follower.as_deref() == Some("on"); |
| 36 |
let notify_release = form.notify_release.as_deref() == Some("on"); |
| 37 |
let login_notification_enabled = form.login_notification_enabled.as_deref() == Some("on"); |
| 38 |
let notify_issues = form.notify_issues.as_deref() == Some("on"); |
| 39 |
let notify_status = form.notify_status.as_deref() == Some("on"); |
| 40 |
let tips_enabled = form.tips_enabled.as_deref() == Some("on"); |
| 41 |
let notify_tip = form.notify_tip.as_deref() == Some("on"); |
| 42 |
|
| 43 |
db::users::update_notification_preferences( |
| 44 |
&db, |
| 45 |
user.id, |
| 46 |
db::users::NotificationPreferences { |
| 47 |
notify_sale, |
| 48 |
notify_follower, |
| 49 |
notify_release, |
| 50 |
login_notification_enabled, |
| 51 |
notify_issues, |
| 52 |
notify_status, |
| 53 |
}, |
| 54 |
) |
| 55 |
.await?; |
| 56 |
db::users::update_tip_preferences(&db, user.id, tips_enabled, notify_tip).await?; |
| 57 |
|
| 58 |
Ok(Html( |
| 59 |
SaveStatusTemplate { |
| 60 |
success: true, |
| 61 |
message: "Preferences saved".to_string(), |
| 62 |
} |
| 63 |
.render_string()?, |
| 64 |
) |
| 65 |
.into_response()) |
| 66 |
} |
| 67 |
|