Skip to main content

max / makenotwork

2.1 KB · 55 lines History Blame Raw
1 //! Notification preference management.
2
3 use axum::{
4 extract::State,
5 response::{Html, IntoResponse, Response},
6 Form,
7 };
8 use serde::Deserialize;
9
10 use crate::{
11 auth::AuthUser,
12 db,
13 error::Result,
14 templates::SaveStatusTemplate,
15 AppState,
16 };
17
18 /// Form input for notification preferences (checkbox values: "on" or absent).
19 #[derive(Debug, Deserialize)]
20 pub struct UpdatePreferencesForm {
21 pub notify_sale: Option<String>,
22 pub notify_follower: Option<String>,
23 pub notify_release: Option<String>,
24 pub login_notification_enabled: Option<String>,
25 pub notify_issues: Option<String>,
26 pub notify_status: Option<String>,
27 pub tips_enabled: Option<String>,
28 pub notify_tip: Option<String>,
29 }
30
31 /// Update the authenticated user's notification preferences.
32 #[tracing::instrument(skip_all, name = "users::update_preferences")]
33 pub(in crate::routes::api) async fn update_preferences(
34 State(state): State<AppState>,
35 AuthUser(user): AuthUser,
36 Form(form): Form<UpdatePreferencesForm>,
37 ) -> Result<Response> {
38 let notify_sale = form.notify_sale.as_deref() == Some("on");
39 let notify_follower = form.notify_follower.as_deref() == Some("on");
40 let notify_release = form.notify_release.as_deref() == Some("on");
41 let login_notification_enabled = form.login_notification_enabled.as_deref() == Some("on");
42 let notify_issues = form.notify_issues.as_deref() == Some("on");
43 let notify_status = form.notify_status.as_deref() == Some("on");
44 let tips_enabled = form.tips_enabled.as_deref() == Some("on");
45 let notify_tip = form.notify_tip.as_deref() == Some("on");
46
47 db::users::update_notification_preferences(&state.db, user.id, notify_sale, notify_follower, notify_release, login_notification_enabled, notify_issues, notify_status).await?;
48 db::users::update_tip_preferences(&state.db, user.id, tips_enabled, notify_tip).await?;
49
50 Ok(Html(SaveStatusTemplate {
51 success: true,
52 message: "Preferences saved".to_string(),
53 }.render_string()).into_response())
54 }
55