Add user settings cloud sync (GetUserSettings / SetUserSettings)#62
Add user settings cloud sync (GetUserSettings / SetUserSettings)#62
Conversation
Co-authored-by: PythonSmall-Q <106425289+PythonSmall-Q@users.noreply.github.com> Agent-Logs-Url: https://github.com/XMOJ-Script-dev/XMOJ-bbs/sessions/2b760a63-8dcf-43de-baf9-6333ab2fabc8
There was a problem hiding this comment.
Pull request overview
Adds authenticated, D1-backed API support for syncing per-user script settings across devices via two new endpoints.
Changes:
- Introduces
SetUserSettingsto validate and persist a JSON settings object for the authenticated user. - Introduces
GetUserSettingsto retrieve stored settings (or{}when none exist) and report corrupted stored data. - Adds a D1 migration creating the
user_settingstable 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.
There was a problem hiding this comment.
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.
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>
|
@copilot fix failed test |
Co-authored-by: PythonSmall-Q <106425289+PythonSmall-Q@users.noreply.github.com> Agent-Logs-Url: https://github.com/XMOJ-Script-dev/XMOJ-bbs/sessions/57c977ec-c427-4454-ab97-b298830f3cd3
Fixed in 791dc79. The test 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:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
There was a problem hiding this comment.
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.
| settings: SettingsString | ||
| })); | ||
| } catch (e) { | ||
| if (e instanceof Error && /UNIQUE|constraint|duplicate/i.test(e.message)) { |
There was a problem hiding this comment.
P1: Duplicate-key fallback is unreachable because ThrowErrorIfFailed throws Result, not Error, so existing-user updates can fail instead of running the update path.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At Source/Process.ts, line 1529:
<comment>Duplicate-key fallback is unreachable because `ThrowErrorIfFailed` throws `Result`, not `Error`, so existing-user updates can fail instead of running the update path.</comment>
<file context>
@@ -1493,32 +1493,50 @@ export class Process {
- user_id: this.Username
- }));
+ } catch (e) {
+ if (e instanceof Error && /UNIQUE|constraint|duplicate/i.test(e.message)) {
+ // Row for this user_id already exists, perform an update instead.
+ ThrowErrorIfFailed(await this.XMOJDatabase.Update("user_settings", {
</file context>
| if (e instanceof Error && /UNIQUE|constraint|duplicate/i.test(e.message)) { | |
| const errorMessage = e instanceof Error ? e.message : (typeof e === "object" && e !== null && "Message" in e ? String((e as { Message?: unknown }).Message) : String(e)); | |
| if (/UNIQUE|constraint|duplicate/i.test(errorMessage)) { |
user_settingstable (migrations/0004_add_user_settings.sql)SetUserSettingsendpoint toProcess.tsGetUserSettingsendpoint toProcess.tsSetUserSettings updates existing settings rowto simulate PK conflict instead of checkingGetTableSize💡 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
GetUserSettings/SetUserSettingsfor the logged-in user (JSON object only; 10k max; returns{}when unset; reports corrupted data).user_settingskeyed byuser_id.phpsessid.Bug Fixes
Written for commit 791dc79. Summary will update on new commits.