feat(preferences): configurable photo info attributes stored in user profile
- backend: users.preferences jsonb (migration 000004), Me returns preferences, PATCH /auth/me/preferences with validation (visible list + labels) - frontend: gear in info section opens PhotoInfoSettingsDialog; base attribute set shown by default, remaining under ellipsis; per-attribute visibility and custom labels persisted to the profile; reset clears settings from the profile - frontend: dialog has select-all/deselect-all icon buttons (no 'show all'); EXIF constants extracted to lib/exif.ts - fix: render modal overlays via portal to document.body so they center in the viewport despite transformed panel/rail ancestors - openspec: sync lightroom-workspace-ui artifacts (spec/design/proposal/tasks 9.8)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -89,5 +92,79 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
httpx.RespondError(c, httpx.Unauthorized("unauthorized", "user not found"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"user": u.Public()})
|
||||
prefs := u.Preferences
|
||||
if prefs == nil {
|
||||
prefs = map[string]any{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"user": u.Public(), "preferences": prefs})
|
||||
}
|
||||
|
||||
const (
|
||||
maxVisibleAttrs = 500
|
||||
maxLabelEntries = 500
|
||||
maxPreferenceKeyLen = 128
|
||||
maxPreferenceValueLen = 128
|
||||
)
|
||||
|
||||
type photoInfoPreferences struct {
|
||||
Visible []string `json:"visible"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
}
|
||||
|
||||
type preferencesPayload struct {
|
||||
PhotoInfo *photoInfoPreferences `json:"photo_info,omitempty"`
|
||||
}
|
||||
|
||||
func (p preferencesPayload) validate() error {
|
||||
pi := p.PhotoInfo
|
||||
if pi == nil {
|
||||
return nil
|
||||
}
|
||||
if len(pi.Visible) > maxVisibleAttrs {
|
||||
return fmt.Errorf("too many visible attributes")
|
||||
}
|
||||
for _, k := range pi.Visible {
|
||||
if k == "" || len(k) > maxPreferenceKeyLen {
|
||||
return fmt.Errorf("invalid visible attribute key")
|
||||
}
|
||||
}
|
||||
if len(pi.Labels) > maxLabelEntries {
|
||||
return fmt.Errorf("too many label overrides")
|
||||
}
|
||||
for k, v := range pi.Labels {
|
||||
if k == "" || len(k) > maxPreferenceKeyLen {
|
||||
return fmt.Errorf("invalid label key")
|
||||
}
|
||||
if len(v) > maxPreferenceValueLen {
|
||||
return fmt.Errorf("label value too long")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handler) UpdatePreferences(c *gin.Context) {
|
||||
body, err := io.ReadAll(io.LimitReader(c.Request.Body, 1<<20))
|
||||
if err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_preferences", "could not read request body"))
|
||||
return
|
||||
}
|
||||
var payload preferencesPayload
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_preferences", "preferences must be a JSON object"))
|
||||
return
|
||||
}
|
||||
if err := payload.validate(); err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_preferences", err.Error()))
|
||||
return
|
||||
}
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_preferences", "could not encode preferences"))
|
||||
return
|
||||
}
|
||||
if err := h.svc.store.UpdatePreferences(c.Request.Context(), UserID(c), raw); err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"preferences": payload})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -29,27 +30,45 @@ func (s *Store) CreateUser(ctx context.Context, u *models.User) error {
|
||||
|
||||
func (s *Store) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
return s.scanUser(s.pool.QueryRow(ctx,
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''), created_at
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
||||
COALESCE(preferences, '{}'::jsonb)::text, created_at
|
||||
FROM users WHERE lower(email) = lower($1)`, email))
|
||||
}
|
||||
|
||||
func (s *Store) GetUserByID(ctx context.Context, id string) (*models.User, error) {
|
||||
return s.scanUser(s.pool.QueryRow(ctx,
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''), created_at
|
||||
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
||||
COALESCE(preferences, '{}'::jsonb)::text, created_at
|
||||
FROM users WHERE id = $1`, id))
|
||||
}
|
||||
|
||||
func (s *Store) scanUser(row pgx.Row) (*models.User, error) {
|
||||
var u models.User
|
||||
if err := row.Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Name, &u.AvatarURL, &u.CreatedAt); err != nil {
|
||||
var prefs string
|
||||
if err := row.Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Name, &u.AvatarURL, &prefs, &u.CreatedAt); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if prefs != "" && prefs != "null" {
|
||||
_ = json.Unmarshal([]byte(prefs), &u.Preferences)
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdatePreferences(ctx context.Context, userID string, raw []byte) error {
|
||||
tag, err := s.pool.Exec(ctx,
|
||||
`UPDATE users SET preferences = $2::jsonb WHERE id = $1`, userID, string(raw))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateRefreshToken(ctx context.Context, t *models.RefreshToken) error {
|
||||
_, err := s.pool.Exec(ctx,
|
||||
`INSERT INTO refresh_tokens (id, user_id, token_hash, expires_at)
|
||||
|
||||
Reference in New Issue
Block a user