mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
Type size, density and accent were kept in localStorage, which is per-origin, so hanzo.ai, hanzo.chat, hanzo.app and console held four independent copies of one person's setting. Measured: text set to 1.3 on hanzo.ai rendered 18.2px there and 14px on the other three, same browser, same person. A setting that does not travel reads as a setting that does not work. It nests in the hanzo.preferences blob beside consent -- no second key, no second table -- with per-application overrides resolved axis by axis, so an app can differ on density without restating a type scale. Every axis's zero value means "not set", and that is load-bearing: 0 for the type scale sits outside 0.85..1.4, so "never chose" stays distinct from "chose the middle setting". The read half and Validate share one definition of what an axis accepts, because a value they disagreed about would be stored and never shown. Consent's merge helpers were hardcoded to its own key, so appearance would have needed a second identical merge -- the one thing the ConsentKey comment forbids. They are one now, parameterized by key, and consent is 62 lines lighter for it. The OIDC preferences endpoint shallow-merges opaque JSON, so it runs a patch through the parser: without that the validation is decorative, and a client could store a density nothing renders while the person is told it saved. Co-authored-by: Hanzo Dev <[email protected]>
75 lines
2.4 KiB
Go
75 lines
2.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package schema
|
|
|
|
import "encoding/json"
|
|
|
|
// The preferences blob is ONE JSON object on the user row, and every
|
|
// account-backed record nests inside it under its own key — consent under
|
|
// ConsentKey, appearance under AppearanceKey. This file holds the merge all of
|
|
// them write through. A second merge over the same object would eventually
|
|
// disagree with this one, and the disagreement shows up as a preference that
|
|
// silently vanished when an unrelated screen saved.
|
|
|
|
// PreferencesKey is the User.Properties entry holding the cross-product
|
|
// preferences JSON blob (the console-side twin is PREFS_PROPERTY; keep in
|
|
// lockstep). Every nested record shares it, so there is ONE store and ONE merge —
|
|
// no parallel table to drift.
|
|
const PreferencesKey = "hanzo.preferences"
|
|
|
|
// pref returns the raw member stored under key in u's preferences, and whether
|
|
// there is one at all. The distinction matters to [User.CarryConsentFrom]: a user
|
|
// who has never answered must stay unanswered, not acquire a default-valued
|
|
// record that looks like one.
|
|
func (u *User) pref(key string) (json.RawMessage, bool) {
|
|
blob := u.Properties[PreferencesKey]
|
|
if blob == "" {
|
|
return nil, false
|
|
}
|
|
var m map[string]json.RawMessage
|
|
if json.Unmarshal([]byte(blob), &m) != nil {
|
|
return nil, false
|
|
}
|
|
raw, ok := m[key]
|
|
return raw, ok
|
|
}
|
|
|
|
// putPref stores raw as u's member at key, doing the map bookkeeping once for
|
|
// every writer.
|
|
func (u *User) putPref(key string, raw json.RawMessage) error {
|
|
prior := u.Properties[PreferencesKey]
|
|
if prior == "" && raw == nil {
|
|
return nil // nothing recorded, so nothing to strip
|
|
}
|
|
blob, err := setPref(prior, key, raw)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if u.Properties == nil {
|
|
u.Properties = map[string]string{}
|
|
}
|
|
u.Properties[PreferencesKey] = blob
|
|
return nil
|
|
}
|
|
|
|
// setPref is the ONE mutation of a member of a preferences blob: `raw` replaces
|
|
// the member at key, a nil `raw` removes it, and every other top-level key
|
|
// survives either way.
|
|
func setPref(prefs, key string, raw json.RawMessage) (string, error) {
|
|
merged := map[string]json.RawMessage{}
|
|
if prefs != "" {
|
|
_ = json.Unmarshal([]byte(prefs), &merged)
|
|
}
|
|
if raw == nil {
|
|
delete(merged, key)
|
|
} else {
|
|
merged[key] = raw
|
|
}
|
|
out, err := json.Marshal(merged)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(out), nil
|
|
}
|