feat: notification enhancement
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/prabhupadasays/spsays-bff/internal/middleware"
|
||||
@@ -51,6 +52,48 @@ func (h *NotificationHandler) MarkAllRead(w http.ResponseWriter, r *http.Request
|
||||
response.NoContent(w)
|
||||
}
|
||||
|
||||
// GetPrefs — GET /notifications/prefs. The resolved Settings matrix:
|
||||
// {"prefs": {"follows": {"in_app": true, "email": true, "push": true}, …}}.
|
||||
func (h *NotificationHandler) GetPrefs(w http.ResponseWriter, r *http.Request) {
|
||||
uid := middleware.UserID(r.Context())
|
||||
matrix, err := h.svc.PrefMatrix(r.Context(), uid)
|
||||
if err != nil {
|
||||
response.Error(w, http.StatusInternalServerError, "could not load notification preferences")
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, map[string]any{"prefs": matrix})
|
||||
}
|
||||
|
||||
// UpdatePrefs — PUT /notifications/prefs { event, channel, enabled }. Writes
|
||||
// one matrix cell; returns the full re-resolved matrix so the client can
|
||||
// render authoritative state.
|
||||
func (h *NotificationHandler) UpdatePrefs(w http.ResponseWriter, r *http.Request) {
|
||||
uid := middleware.UserID(r.Context())
|
||||
var req struct {
|
||||
Event string `json:"event"`
|
||||
Channel string `json:"channel"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
response.Error(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
if err := h.svc.SetPref(r.Context(), uid, req.Event, req.Channel, req.Enabled); err != nil {
|
||||
if errors.Is(err, service.ErrUnknownPref) {
|
||||
response.Error(w, http.StatusBadRequest, "unknown event or channel")
|
||||
return
|
||||
}
|
||||
response.Error(w, http.StatusInternalServerError, "could not update notification preferences")
|
||||
return
|
||||
}
|
||||
matrix, err := h.svc.PrefMatrix(r.Context(), uid)
|
||||
if err != nil {
|
||||
response.Error(w, http.StatusInternalServerError, "could not load notification preferences")
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, map[string]any{"prefs": matrix})
|
||||
}
|
||||
|
||||
type emailPrefsResp struct {
|
||||
LikeEmails bool `json:"like_emails"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user