Skip to content

Add user settings cloud sync (GetUserSettings / SetUserSettings)#62

Merged
PythonSmall-Q merged 8 commits into
masterfrom
copilot/add-sync-user-settings-backend
Mar 28, 2026
Merged

Add user settings cloud sync (GetUserSettings / SetUserSettings)#62
PythonSmall-Q merged 8 commits into
masterfrom
copilot/add-sync-user-settings-backend

Conversation

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Contributor
  • Explore codebase and understand existing patterns
  • Run existing tests (40 pass)
  • Create migration file for user_settings table (migrations/0004_add_user_settings.sql)
  • Add SetUserSettings endpoint to Process.ts
  • Add GetUserSettings endpoint to Process.ts
  • Add tests for both new endpoints (48 total, all pass)
  • Code review feedback addressed (size limit, shape validation, atomic upsert)
  • Fix failing test: update SetUserSettings updates existing settings row to simulate PK conflict instead of checking GetTableSize
  • CodeQL security scan: 0 alerts

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Summary by cubic

Adds a backend API to sync user settings across devices. Stores per-user settings in D1 and requires a valid phpsessid; the frontend calls it from XMOJ-Script (XMOJ.user.js).

  • New Features

    • Endpoints GetUserSettings/SetUserSettings for the logged-in user (JSON object only; 10k max; returns {} when unset; reports corrupted data).
    • D1 persistence in user_settings keyed by user_id.
  • Bug Fixes

    • Upsert is insert-first with update on duplicate key to avoid races; duplicate-key detection is more robust and non-uniqueness errors propagate.
    • Tests cover insert/update paths, invalid/corrupted JSON, and the update test simulates a PK conflict to assert the upsert path.

Written for commit 267d528. Summary will update on new commits.

Copilot AI changed the title [WIP] Add synchronization feature for user settings Add user settings cloud sync (GetUserSettings / SetUserSettings) Mar 22, 2026
Copilot AI requested a review from PythonSmall-Q March 22, 2026 14:14
@PythonSmall-Q PythonSmall-Q marked this pull request as ready for review March 22, 2026 14:16
Copilot AI review requested due to automatic review settings March 22, 2026 14:16

Copilot AI 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.

Pull request overview

Adds authenticated, D1-backed API support for syncing per-user script settings across devices via two new endpoints.

Changes:

  • Introduces SetUserSettings to validate and persist a JSON settings object for the authenticated user.
  • Introduces GetUserSettings to retrieve stored settings (or {} when none exist) and report corrupted stored data.
  • Adds a D1 migration creating the user_settings table and unit tests covering the new endpoints.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Source/Process.ts Adds SetUserSettings / GetUserSettings Process endpoints with JSON validation and D1 persistence.
migrations/0004_add_user_settings.sql Creates user_settings table keyed by user_id with a JSON-string settings column.
test/process.test.js Adds tests for insert/update flows, invalid JSON handling, empty settings, and corrupted stored JSON.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Source/Process.ts Outdated
Comment thread Source/Process.ts
Comment thread Source/Process.ts Outdated
Comment thread test/process.test.js

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="Source/Process.ts">

<violation number="1" location="Source/Process.ts:1509">
P2: The check-then-insert/update flow is race-prone; concurrent saves can hit a duplicate-key failure on first write for the same user.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread Source/Process.ts Outdated
PythonSmall-Q and others added 4 commits March 22, 2026 22:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
@PythonSmall-Q

Copy link
Copy Markdown
Member

@copilot fix failed test

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix failed test

Fixed in 791dc79. The test SetUserSettings updates existing settings row was written for the old check-then-insert/update flow, but the code was updated (per review) to use an atomic insert-first/catch-on-conflict approach. Updated the test to simulate a primary key UNIQUE constraint failed error from Insert, which then triggers the Update fallback — matching the actual runtime behaviour.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node /home/REDACTED/work/_temp/ghcca-node/node/bin/npm install --registry REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@PythonSmall-Q

PythonSmall-Q commented Mar 22, 2026

Copy link
Copy Markdown
Member
// Save — serialize all UserScript-Setting-* localStorage keys
RequestAPI("SetUserSettings", {
    Settings: JSON.stringify(
        Object.fromEntries(
            Object.keys(localStorage)
                .filter(k => k.startsWith("UserScript-Setting-"))
                .map(k => [k.replace("UserScript-Setting-", ""), localStorage.getItem(k)])
        )
    )
}, (res) => { /* handle */ });

// Load — restore into localStorage
RequestAPI("GetUserSettings", {}, (res) => {
    if (res.Success) {
        for (const [key, value] of Object.entries(res.Data.Settings))
            localStorage.setItem("UserScript-Setting-" + key, value);
    }
});

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="Source/Process.ts">

<violation number="1" location="Source/Process.ts:1529">
P1: Duplicate-key fallback is unreachable because `ThrowErrorIfFailed` throws `Result`, not `Error`, so existing-user updates can fail instead of running the update path.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread Source/Process.ts Outdated
@boomzero

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 791dc79c6c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Source/Process.ts Outdated
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
@PythonSmall-Q

Copy link
Copy Markdown
Member

@boomzero

@PythonSmall-Q PythonSmall-Q merged commit 8622479 into master Mar 28, 2026
7 checks passed
@PythonSmall-Q PythonSmall-Q deleted the copilot/add-sync-user-settings-backend branch March 28, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants