Skip to main content

max / makenotwork

2.3 KB · 70 lines History Blame Raw
1 //! Notification preference management.
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 /// Form input for notification preferences (checkbox values: "on" or absent).
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 pub notify_invite: Option<String>,
26 }
27
28 /// Update the authenticated user's notification preferences.
29 #[tracing::instrument(skip_all, name = "users::update_preferences")]
30 pub(in crate::routes::api) async fn update_preferences(
31 State(db): State<PgPool>,
32 AuthUser(user): AuthUser,
33 Form(form): Form<UpdatePreferencesForm>,
34 ) -> Result<Response> {
35 let notify_sale = form.notify_sale.as_deref() == Some("on");
36 let notify_follower = form.notify_follower.as_deref() == Some("on");
37 let notify_release = form.notify_release.as_deref() == Some("on");
38 let login_notification_enabled = form.login_notification_enabled.as_deref() == Some("on");
39 let notify_issues = form.notify_issues.as_deref() == Some("on");
40 let notify_status = form.notify_status.as_deref() == Some("on");
41 let tips_enabled = form.tips_enabled.as_deref() == Some("on");
42 let notify_tip = form.notify_tip.as_deref() == Some("on");
43 let notify_invite = form.notify_invite.as_deref() == Some("on");
44
45 db::users::update_notification_preferences(
46 &db,
47 user.id,
48 db::users::NotificationPreferences {
49 notify_sale,
50 notify_follower,
51 notify_release,
52 login_notification_enabled,
53 notify_issues,
54 notify_status,
55 notify_invite,
56 },
57 )
58 .await?;
59 db::users::update_tip_preferences(&db, user.id, tips_enabled, notify_tip).await?;
60
61 Ok(Html(
62 SaveStatusTemplate {
63 success: true,
64 message: "Preferences saved".to_string(),
65 }
66 .render_string()?,
67 )
68 .into_response())
69 }
70