mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-10-11 18:57:26 +00:00
Part B of #2128: a logged-in user's settings follow them across devices. Log in on a phone and your own nodes, favorites, customizer and filters are there; a change on one device reaches the others within a minute or when you return to the tab. **This PR builds on #2129.** Until that one is merged, the diff here includes it. The commits for this part start at `docs(specs): settings sync for optional user management (sub-project B)`. ## The situation Everything a visitor sets up lives in one browser's `localStorage` (about 100 keys in `public/`). A second device or a cleared cache starts from zero (#895). ## What this PR adds **Storage.** `users.db` schema v2: one JSON document per user in `user_settings`, with a revision number and a generation id. A write succeeds only when the client's revision and generation match the stored ones, so two devices cannot overwrite each other silently. **Server.** `GET`, `PUT` and `DELETE /api/account/settings`, behind the same session and CSRF checks as the account routes. - The server owns the list of synced keys (61 keys, [`settings_allowlist.go`](https://github.com/efiten/CoreScope/blob/feat/settings-sync/cmd/server/settings_allowlist.go)) and sends it to the client, so the two cannot drift. - A hard denylist, checked first, refuses `meshcore-api-key`, every `corescope_channel_*` key and `live-channel-colors` (#725). The colour map is keyed by channel hash, and for a user-added channel that hash is `user:<name>`, which would expose hashtag channel names. - Documents are capped at 256 KiB, measured like `JSON.stringify`. PUT is limited to 60 requests per hour per user. A stale revision gets 409 with the current document. **Client** ([`settings-sync.js`](https://github.com/efiten/CoreScope/blob/feat/settings-sync/public/settings-sync.js)). Inert unless the feature is on and someone is logged in. - It wraps `localStorage.setItem` and `removeItem` for allowlisted keys only and pushes 2 seconds after the last change. - It pulls on login, page load, tab focus and every 60 seconds while the tab is visible. - **Merge:** three-way, against a per-device baseline that belongs to one user and one document generation. Lists (own nodes, favorites, saved filters) merge per item, so an item added anywhere is kept and an item removed on one device does not come back from another. Single values: the profile wins unless only this device changed it. - Remote changes are written without a push, theme and colour-blind preset are re-applied, and the current page re-renders (skipped on account pages and while the geofilter editor is open). **UI.** - Logout asks: keep my settings on this device (default), remove them from this device, or cancel. Channel keys are never removed: no copy exists anywhere else. - The account page gets a "Settings sync" section: last synced time, "Sync now", what is and is not synced, and "Delete synced settings from my account". ## Not synced Layout and device state (panel and column widths, collapsed panels, map positions, geofilter drafts), channel data (#725), the API key, and all `sessionStorage`. The full list is in the [spec](https://github.com/efiten/CoreScope/blob/feat/settings-sync/docs/specs/2026-10-06-user-settings-sync-design.md). ## Performance - One GET per page load, tab focus and minute while visible; one debounced PUT per burst of changes. - The `setItem` wrapper costs one Set lookup per write for non-synced keys. A synced write reads one small revision key, not the stored document. - The server reads or writes one row per request. ## Verification - `internal/users` and `cmd/server`: `go vet` and `go test` pass locally (22 new Go tests), including a test that every allowlisted key still occurs in `public/`, and denylist tests. - `tests/unit/test-settings-sync.js`: 79 passing (vm, real module). The cases cover the merge table, two tabs sharing one storage, stale answers after a push, delete while a push is in flight, and logout while the final push fails. - `sh test-all.sh` exits 0. - `tests/e2e/test-user-management-e2e.js` (10 steps, 4 of them new) passed locally with two browser contexts as two devices: a favorite and the packet time window travel from device 1 to device 2, a removal does not come back, and "remove from this device" clears the synced keys while a channel key stays. - Checked by hand on a staging instance with a desktop and a phone on one account. ## Not in this PR - On a shared browser where the previous user chose "keep", the next user's first login merges those settings into their own account. The user guide says to choose "remove" on shared computers. - Saved filter expressions are synced as typed, including any channel names written in them. The guide says so. - Realtime push between devices; the minute pull is the sync interval. --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
164 lines
5.3 KiB
Go
164 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/meshcore-analyzer/users"
|
|
)
|
|
|
|
// settingsBodyMax leaves room for the request envelope around a document
|
|
// at the cap.
|
|
const settingsBodyMax = settingsDocMaxBytes + 8<<10
|
|
|
|
// settingsDoc is one user's synced settings: raw localStorage strings by
|
|
// key. The server never parses the values.
|
|
type settingsDoc struct {
|
|
V int `json:"v"`
|
|
Keys map[string]string `json:"keys"`
|
|
}
|
|
|
|
// Generation identifies one document: revisions restart at 1 after a
|
|
// DELETE, and the generation tells the new document from the old one.
|
|
type settingsGetResponse struct {
|
|
Revision int64 `json:"revision"`
|
|
Generation string `json:"generation"`
|
|
Doc *settingsDoc `json:"doc"`
|
|
Allowlist []settingsKey `json:"allowlist"`
|
|
}
|
|
|
|
type settingsPutRequest struct {
|
|
BaseRevision int64 `json:"baseRevision"`
|
|
BaseGeneration string `json:"baseGeneration"`
|
|
Doc *settingsDoc `json:"doc"`
|
|
}
|
|
|
|
type settingsPutResponse struct {
|
|
Revision int64 `json:"revision"`
|
|
Generation string `json:"generation"`
|
|
}
|
|
|
|
type settingsConflictResponse struct {
|
|
Revision int64 `json:"revision"`
|
|
Generation string `json:"generation"`
|
|
Doc *settingsDoc `json:"doc"`
|
|
}
|
|
|
|
// validateSettingsDoc checks the documented shape, then every key: denied
|
|
// keys first (#725), then the allowlist.
|
|
func validateSettingsDoc(d *settingsDoc) error {
|
|
if d == nil || d.V != 1 || d.Keys == nil {
|
|
return errors.New(`doc must be {"v": 1, "keys": {...}}`)
|
|
}
|
|
allowed := map[string]bool{}
|
|
for _, k := range syncedSettingsKeys() {
|
|
allowed[k.Key] = true
|
|
}
|
|
for key := range d.Keys {
|
|
if settingsDenied(key) {
|
|
return fmt.Errorf("key %q is never synced", key)
|
|
}
|
|
if !allowed[key] {
|
|
return fmt.Errorf("key %q is not a synced setting", key)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// encodeSettingsDoc serializes without HTML escaping, like the browser's
|
|
// JSON.stringify, so the size cap matches what the client sends.
|
|
func encodeSettingsDoc(d *settingsDoc) (string, error) {
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
if err := enc.Encode(d); err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes.TrimRight(buf.Bytes(), "\n")), nil
|
|
}
|
|
|
|
// loadSettings reads the stored document (nil at revision 0). On failure
|
|
// it writes 500 and returns ok=false.
|
|
func (s *Server) loadSettings(w http.ResponseWriter, uid int64) (users.SettingsVersion, *settingsDoc, bool) {
|
|
raw, v, err := s.auth.st.GetSettings(uid)
|
|
if err != nil {
|
|
log.Printf("[users] settings read for user #%d: %v", uid, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return users.SettingsVersion{}, nil, false
|
|
}
|
|
if v.Revision == 0 {
|
|
return users.SettingsVersion{}, nil, true
|
|
}
|
|
var doc settingsDoc
|
|
if err := json.Unmarshal([]byte(raw), &doc); err != nil {
|
|
log.Printf("[users] settings for user #%d do not decode (%d bytes): %v", uid, len(raw), err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return users.SettingsVersion{}, nil, false
|
|
}
|
|
return v, &doc, true
|
|
}
|
|
|
|
func (s *Server) handleSettingsGet(w http.ResponseWriter, _ *http.Request, u *users.User, _ *users.Session) {
|
|
v, doc, ok := s.loadSettings(w, u.ID)
|
|
if !ok {
|
|
return
|
|
}
|
|
writeJSON(w, settingsGetResponse{Revision: v.Revision, Generation: v.Generation, Doc: doc, Allowlist: syncedSettingsKeys()})
|
|
}
|
|
|
|
func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request, u *users.User, _ *users.Session) {
|
|
a := s.auth
|
|
if ok, wait := a.settingsPut.take("user:" + strconv.FormatInt(u.ID, 10)); !ok {
|
|
writeTooManyRequests(w, wait)
|
|
return
|
|
}
|
|
var req settingsPutRequest
|
|
if !decodeJSONMax(w, r, &req, settingsBodyMax, http.StatusRequestEntityTooLarge) {
|
|
return
|
|
}
|
|
if err := validateSettingsDoc(req.Doc); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
raw, err := encodeSettingsDoc(req.Doc)
|
|
if err != nil {
|
|
log.Printf("[users] settings encode for user #%d: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
if len(raw) > settingsDocMaxBytes {
|
|
log.Printf("[users] settings for user #%d refused: %d bytes", u.ID, len(raw))
|
|
writeError(w, http.StatusRequestEntityTooLarge, fmt.Sprintf("settings are larger than %d KiB", settingsDocMaxBytes>>10))
|
|
return
|
|
}
|
|
v, err := a.st.PutSettings(u.ID, users.SettingsVersion{Revision: req.BaseRevision, Generation: req.BaseGeneration}, raw)
|
|
if errors.Is(err, users.ErrSettingsConflict) {
|
|
cur, doc, ok := s.loadSettings(w, u.ID)
|
|
if !ok {
|
|
return
|
|
}
|
|
writeJSONStatus(w, http.StatusConflict, settingsConflictResponse{Revision: cur.Revision, Generation: cur.Generation, Doc: doc})
|
|
return
|
|
}
|
|
if err != nil {
|
|
log.Printf("[users] settings write for user #%d (%d bytes): %v", u.ID, len(raw), err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
writeJSON(w, settingsPutResponse{Revision: v.Revision, Generation: v.Generation})
|
|
}
|
|
|
|
func (s *Server) handleSettingsDelete(w http.ResponseWriter, _ *http.Request, u *users.User, _ *users.Session) {
|
|
if err := s.auth.st.DeleteSettings(u.ID); err != nil {
|
|
log.Printf("[users] settings delete for user #%d: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
writeJSON(w, okResponse{OK: true})
|
|
}
|