add reading settings sync
This commit is contained in:
@@ -53,6 +53,51 @@ func (h *UserHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
response.JSON(w, http.StatusOK, u)
|
||||
}
|
||||
|
||||
// GetReadingPrefs returns the caller's saved reading prefs blob (always
|
||||
// well-formed JSON; an empty object when the user has never written).
|
||||
// The frontend treats the response as the source of truth on first
|
||||
// sign-in for this device.
|
||||
func (h *UserHandler) GetReadingPrefs(w http.ResponseWriter, r *http.Request) {
|
||||
uid := middleware.UserID(r.Context())
|
||||
raw, err := h.svc.GetReadingPrefs(r.Context(), uid)
|
||||
if err != nil {
|
||||
response.Error(w, http.StatusInternalServerError, "could not load reading prefs")
|
||||
return
|
||||
}
|
||||
// Round-trip the JSONB as the data field directly. We use a raw
|
||||
// json.RawMessage so the body is `{"data": {...}}` not a re-encoded
|
||||
// string. The envelope wrapper keeps shape parity with the rest of
|
||||
// the API.
|
||||
response.JSON(w, http.StatusOK, json.RawMessage(raw))
|
||||
}
|
||||
|
||||
// UpdateReadingPrefs replaces the caller's reading-prefs blob with the
|
||||
// request body (which must itself be a JSON object — we re-marshal a
|
||||
// decoded map to enforce that without imposing a fixed schema on a
|
||||
// payload the frontend owns).
|
||||
func (h *UserHandler) UpdateReadingPrefs(w http.ResponseWriter, r *http.Request) {
|
||||
uid := middleware.UserID(r.Context())
|
||||
|
||||
// Decode into map[string]any to guarantee the body is an object (not
|
||||
// an array, number, or string). Re-marshal before persisting so the
|
||||
// stored JSON is canonicalised and any duplicate keys are dropped.
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
response.Error(w, http.StatusBadRequest, "invalid JSON body (expect an object)")
|
||||
return
|
||||
}
|
||||
prefsJSON, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
response.Error(w, http.StatusBadRequest, "could not re-encode prefs")
|
||||
return
|
||||
}
|
||||
if err := h.svc.UpdateReadingPrefs(r.Context(), uid, prefsJSON); err != nil {
|
||||
response.Error(w, http.StatusInternalServerError, "could not save reading prefs")
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, json.RawMessage(prefsJSON))
|
||||
}
|
||||
|
||||
type presignReq struct {
|
||||
ContentType string `json:"content_type"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user