@@ -0,0 +1,158 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package auth |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| 10 | + "github.com/tenseleyFlow/shithub/internal/web/middleware" |
| 11 | +) |
| 12 | + |
| 13 | +// notifChannel is one toggleable preference. Adding a channel is a code |
| 14 | +// change (this list) plus a translation, never a DB migration; the |
| 15 | +// underlying user_notification_prefs table is generic key/value. |
| 16 | +type notifChannel struct { |
| 17 | + Key string |
| 18 | + Title string |
| 19 | + Description string |
| 20 | + Required bool // when true, displayed but not toggleable |
| 21 | +} |
| 22 | + |
| 23 | +// notifChannels is the fixed set surfaced on /settings/notifications. |
| 24 | +// Order matters — it's the render order. Required channels (security |
| 25 | +// alerts) are listed first so they're prominent. |
| 26 | +var notifChannels = []notifChannel{ |
| 27 | + { |
| 28 | + Key: "security_alerts", |
| 29 | + Title: "Security alerts", |
| 30 | + Description: "New sign-ins, password changes, 2FA enrollment, recovery codes used. These cannot be disabled.", |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + Key: "account_changes", |
| 35 | + Title: "Account changes", |
| 36 | + Description: "Username changes, primary-email switches, account-deletion confirmations.", |
| 37 | + }, |
| 38 | + { |
| 39 | + Key: "product_news", |
| 40 | + Title: "Product news", |
| 41 | + Description: "Occasional updates about new shithub features. Off by default.", |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +// notifChannelDefault returns the default state for a channel that has |
| 46 | +// no DB row. Account-related channels are opt-out (default on); marketing |
| 47 | +// channels are opt-in (default off). |
| 48 | +func notifChannelDefault(key string) bool { |
| 49 | + switch key { |
| 50 | + case "security_alerts", "account_changes": |
| 51 | + return true |
| 52 | + default: |
| 53 | + return false |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// notifPrefValue is the JSON shape we persist in user_notification_prefs.value. |
| 58 | +type notifPrefValue struct { |
| 59 | + Enabled bool `json:"enabled"` |
| 60 | +} |
| 61 | + |
| 62 | +// settingsNotificationsForm renders GET /settings/notifications. |
| 63 | +func (h *Handlers) settingsNotificationsForm(w http.ResponseWriter, r *http.Request) { |
| 64 | + h.renderNotificationsForm(w, r, "") |
| 65 | +} |
| 66 | + |
| 67 | +// settingsNotificationsSubmit handles POST /settings/notifications. |
| 68 | +// |
| 69 | +// Diff strategy: |
| 70 | +// - For each non-required channel, read the form checkbox. |
| 71 | +// - If desired matches default → DeleteUserNotificationPref (so the |
| 72 | +// row only exists when the user has actively diverged). |
| 73 | +// - Otherwise → UpsertUserNotificationPref with {"enabled":<desired>}. |
| 74 | +// |
| 75 | +// This keeps the table small and makes "reset to defaults" a no-op |
| 76 | +// rather than a code change. |
| 77 | +func (h *Handlers) settingsNotificationsSubmit(w http.ResponseWriter, r *http.Request) { |
| 78 | + if err := r.ParseForm(); err != nil { |
| 79 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 80 | + return |
| 81 | + } |
| 82 | + user := middleware.CurrentUserFromContext(r.Context()) |
| 83 | + |
| 84 | + for _, ch := range notifChannels { |
| 85 | + if ch.Required { |
| 86 | + continue |
| 87 | + } |
| 88 | + desired := r.PostFormValue(ch.Key) == "on" |
| 89 | + if desired == notifChannelDefault(ch.Key) { |
| 90 | + if err := h.q.DeleteUserNotificationPref(r.Context(), h.d.Pool, usersdb.DeleteUserNotificationPrefParams{ |
| 91 | + UserID: user.ID, Key: ch.Key, |
| 92 | + }); err != nil { |
| 93 | + h.d.Logger.ErrorContext(r.Context(), "notif: delete", "error", err, "key", ch.Key) |
| 94 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 95 | + return |
| 96 | + } |
| 97 | + continue |
| 98 | + } |
| 99 | + val, err := json.Marshal(notifPrefValue{Enabled: desired}) |
| 100 | + if err != nil { |
| 101 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 102 | + return |
| 103 | + } |
| 104 | + if err := h.q.UpsertUserNotificationPref(r.Context(), h.d.Pool, usersdb.UpsertUserNotificationPrefParams{ |
| 105 | + UserID: user.ID, Key: ch.Key, Value: val, |
| 106 | + }); err != nil { |
| 107 | + h.d.Logger.ErrorContext(r.Context(), "notif: upsert", "error", err, "key", ch.Key) |
| 108 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 109 | + return |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + h.renderNotificationsForm(w, r, "Preferences saved.") |
| 114 | +} |
| 115 | + |
| 116 | +// renderNotificationsForm pulls the current persisted state, layers it |
| 117 | +// on top of defaults, and renders the toggles. |
| 118 | +func (h *Handlers) renderNotificationsForm(w http.ResponseWriter, r *http.Request, successMsg string) { |
| 119 | + user := middleware.CurrentUserFromContext(r.Context()) |
| 120 | + rows, err := h.q.ListUserNotificationPrefs(r.Context(), h.d.Pool, user.ID) |
| 121 | + if err != nil { |
| 122 | + h.d.Logger.ErrorContext(r.Context(), "notif: list", "error", err) |
| 123 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 124 | + return |
| 125 | + } |
| 126 | + persisted := make(map[string]bool, len(rows)) |
| 127 | + for _, p := range rows { |
| 128 | + var v notifPrefValue |
| 129 | + if err := json.Unmarshal(p.Value, &v); err != nil { |
| 130 | + continue |
| 131 | + } |
| 132 | + persisted[p.Key] = v.Enabled |
| 133 | + } |
| 134 | + |
| 135 | + type viewChan struct { |
| 136 | + notifChannel |
| 137 | + Enabled bool |
| 138 | + } |
| 139 | + view := make([]viewChan, 0, len(notifChannels)) |
| 140 | + for _, ch := range notifChannels { |
| 141 | + enabled := notifChannelDefault(ch.Key) |
| 142 | + if v, ok := persisted[ch.Key]; ok { |
| 143 | + enabled = v |
| 144 | + } |
| 145 | + if ch.Required { |
| 146 | + enabled = true |
| 147 | + } |
| 148 | + view = append(view, viewChan{notifChannel: ch, Enabled: enabled}) |
| 149 | + } |
| 150 | + |
| 151 | + h.renderPage(w, r, "settings/notifications", map[string]any{ |
| 152 | + "Title": "Notifications", |
| 153 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 154 | + "SettingsActive": "notifications", |
| 155 | + "Channels": view, |
| 156 | + "Success": successMsg, |
| 157 | + }) |
| 158 | +} |