Skip to content

1987 persist user settings - #1118

Merged
jamiebenstead merged 10 commits into
mainfrom
1987-persist-user-settings
Nov 20, 2026
Merged

jamiebenstead merged 10 commits into
mainfrom
1987-persist-user-settings

Conversation

@jamiebenstead

Copy link
Copy Markdown
Contributor

Status

What's changed?

Adds a user_settings table and API so a user's editor settings (theme,
font size, AI hints) persist across browsers.

Changes

  • New table user_settings: one row per user (user_id unique), with a
    settings jsonb column.
  • UserSetting model: allowed settings are defined in
    UserSetting::REGISTRY (allowed values + default). Adding or removing a
    setting is a one-line change, with no migration.
  • New endpoints (logged-in users only):
Method Route Body Returns
GET /api/user_settings – The user's settings, with defaults for anything not saved
PATCH /api/user_settings { "settings": { "theme": "dark" } } The updated settings

Behaviour

  • Users with no saved settings get the defaults; no row is created until
    their first change.
  • PATCH only changes the keys sent. Sending null resets that setting to
    its default.
  • Unknown keys or invalid values return 422.
  • If two first saves race, the unique index rejects one and it retries once.

Current settings

Key Values Default
theme light, dark null (follow OS)
font_size small, medium, large small
ai_friendly_errors true, false true

Release

Deploy before the editor-ui change that uses these endpoints.

GET and PATCH /api/user_settings let the editor load and save a
user's settings so they persist across browsers. Users with no
saved settings get the defaults; a row is only created on the
first change.
@cla-bot cla-bot Bot added the cla-signed label Nov 17, 2026
Comment thread app/models/user_setting.rb Resolved
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test coverage

94.2% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/44716257243

@macroscopeapp

macroscopeapp Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Approvability

Verdict: Would Approve

Macroscope's review found this PR approvable — Adds authenticated persistence for user preferences with ownership checks, validated values, and transactional updates. The change is isolated to new endpoints and an additive table, without altering existing data or enabling AI functionality.

Needs human approval because:

  • approvability is in preview mode for this repository — Macroscope would have approved this PR outside preview mode

No code changes detected at 61771b9. Prior analysis still applies.

You can add or adjust custom eligibility rules. Learn more.

Lock the row while merging so concurrent updates are applied in
turn and none are lost, and create the row with ON CONFLICT DO
NOTHING so a racing first update no longer fails validation.
Also reject unknown keys sent as null.
@jamiebenstead

Copy link
Copy Markdown
Contributor Author

@macroscope-app review

@macroscopeapp

macroscopeapp Bot commented Nov 17, 2026

Copy link
Copy Markdown

Manual reviews triggered for commit 762cc4a:

All prior checks · these links stay valid even if you push more commits.

@macroscopeapp

macroscopeapp Bot commented Nov 17, 2026

Copy link
Copy Markdown

Review started; results will be posted as check runs.

@cocomarine cocomarine left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all the comments? Some can be removed. Also, some specs can be removed, too: for example, some request specs are already tested in the model specs etc.

Comment thread app/models/user_setting.rb Resolved
Validation rules are covered by the model specs, so the request specs only check one invalid case. Also removes the unused
UserSetting.for_user method.
@jamiebenstead

Copy link
Copy Markdown
Contributor Author

Do we need all the comments? Some can be removed. Also, some specs can be removed, too: for example, some request specs are already tested in the model specs etc.

Thanks @cocomarine I've updated these parts now

Comment thread app/models/user_setting.rb Outdated
REGISTRY.transform_values { |config| config[:default] }
end

def merged_settings

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing - I wondered if that's a name that better describes what the method is doing do disambiguate it from the similar method below, maybe 'active_settings'? Or maybe it's not needed? There's no harm is sending old settings keys the frontend.

@zetter-rpf zetter-rpf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one

jamiebenstead and others added 2 commits November 20, 2026 10:00
Stop filtering out old keys on read, as the frontend ignores keys
it doesn't know. The new name says what the method does and
avoids confusion with merge_settings.
@jamiebenstead
jamiebenstead merged commit 79db216 into main Nov 20, 2026
10 checks passed
@jamiebenstead
jamiebenstead deleted the 1987-persist-user-settings branch November 20, 2026 10:37
jamiebenstead added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Nov 20, 2026
Part of
RaspberryPiFoundation/digital-editor-issues#1987

## What

Logged-in users' theme, font size and AI hints settings are now saved to
editor-api, so they persist across browsers. Logged-out users keep using
cookies.

Depends on
[this](RaspberryPiFoundation/editor-api#1118)
editor-api `/api/user_settings` PR.

## Changes

- New `userSettings` Redux slice and hooks to load and save settings.
- Settings components now read from `userSettings` instead of cookies.
- Removed the `aiHint` cookie as they are only available to logged in
users. AI hints now use the saved setting.
- New `editor-settingsUpdated` event so host apps can stay in sync.

## Notes

- Users with no saved settings get the defaults.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants