Merge #732: feat(settings): sync case options and selection across devices

This commit is contained in:
Codeman maintainer committed 2026-11-09 08:01:43 +01:00
commit 4f29b0187a
23 files changed
+877 -37

No files matched your search

+31 -4
View File
@@ -87,6 +87,7 @@ import {
settingsWriteErrorResponse,
} from '../settings-writer.js';
import { LinkedCasesWriteRefusedError, linkedCasesPath, mutateLinkedCases } from '../linked-cases-writer.js';
import { dropCaseSettings, renameCaseSettings } from '../case-state-sync.js';
import { isMultiUserMode } from '../../config/multiuser.js';
import type { AuthUser } from '../../types.js';
import { SseEvent } from '../sse-events.js';
@@ -1747,6 +1748,14 @@ export function registerCaseRoutes(
// a stale value is ignored at creation anyway).
// An admin deleting another user's remote/Docker case clears that owner's too.
const forgetCaseDefault = async (caseOwner?: string): Promise<void> => {
// The shared run options (syncCaseState) are the admin's server-wide map: a non-admin's own case of
// the same name must leave them alone. Best-effort, like the default group.
if (isAdmin(req)) {
await mutateSettingsFile((current) => {
const rest = dropCaseSettings(current.caseSettings, name);
return rest ? { ...current, caseSettings: rest } : undefined;
}).catch(() => undefined);
}
const owners = new Set([ownerLayoutKey(ownerFor(req)), ...(caseOwner ? [ownerLayoutKey(caseOwner)] : [])]);
for (const owner of owners) {
try {
@@ -2015,6 +2024,7 @@ export function registerCaseRoutes(
// The settings.json values this rename replaced, set only once it wrote them. A failure puts back
// these keys alone, so a settings save that landed in between is kept.
let settingsUndo: Record<string, unknown> | undefined;
let sharedRenamed = false;
try {
// settings.json is server-wide and caseOrder/lastUsedCase name the admin's cases, so only the
// admin's rename touches it (a non-admin's own rename must not rewrite someone else's list).
@@ -2023,12 +2033,24 @@ export function registerCaseRoutes(
const { before, after } = await mutateSettingsFile((existing) => {
const order = renameInOrder(existing.caseOrder, name, newName);
const last = existing.lastUsedCase === name ? newName : undefined;
if (!order && !last) return undefined;
return { ...existing, ...(order ? { caseOrder: order } : {}), ...(last ? { lastUsedCase: last } : {}) };
const picked = existing.lastPickedCase === name ? newName : undefined;
// The shared run options (syncCaseState) follow the case to its new name.
const shared = renameCaseSettings(existing.caseSettings, name, newName);
if (!order && !last && !picked && !shared) return undefined;
return {
...existing,
...(order ? { caseOrder: order } : {}),
...(last ? { lastUsedCase: last } : {}),
...(picked ? { lastPickedCase: picked } : {}),
...(shared ? { caseSettings: shared } : {}),
};
});
if (after) {
const keys = ['caseOrder', 'lastUsedCase'].filter((k) => after[k] !== before[k]);
// caseSettings is undone by renaming its entry back (below), not by restoring the whole map, so a
// per-case save from another device that landed since is kept.
const keys = ['caseOrder', 'lastUsedCase', 'lastPickedCase'].filter((k) => after[k] !== before[k]);
settingsUndo = Object.fromEntries(keys.map((k) => [k, before[k]]));
sharedRenamed = after.caseSettings !== before.caseSettings;
}
}
if (isLinked) {
@@ -2049,7 +2071,12 @@ export function registerCaseRoutes(
// The folder moved but the registries did not follow: put settings.json and the folder back
// rather than leave a case whose name and folder disagree.
const undo = settingsUndo;
if (undo) await mutateSettingsFile((current) => ({ ...current, ...undo })).catch(() => {});
if (undo) {
await mutateSettingsFile((current) => {
const back = sharedRenamed ? renameCaseSettings(current.caseSettings, newName, name) : null;
return { ...current, ...undo, ...(back ? { caseSettings: back } : {}) };
}).catch(() => {});
}
if (moveFolder) {
const movedBack = await fs.rename(newPath, oldPath).then(
() => true,
+23 -4
View File
@@ -49,6 +49,7 @@ import {
SETTINGS_PATH,
} from '../route-helpers.js';
import { mutateSettingsFile, settingsWriteErrorResponse } from '../settings-writer.js';
import { mergeCaseSettings, SharedCaseLimitError } from '../case-state-sync.js';
import { SseEvent } from '../sse-events.js';
import { getInstallInfo, checkForUpdate, startUpdate, getUpdateStatusForApi } from '../self-update.js';
@@ -160,8 +161,8 @@ export function resolveSpanUrl(hostHeader: string | undefined, fallbackPort = '3
* What GET /api/settings answers a NON-ADMIN in multi-user mode: settings.json minus what
* belongs to the server's admin rather than to the reader. `voiceSettings.apiKey` is the
* Deepgram key an admin saved, a credential (the reader's browser seeded it into its own
* voice config and dictated on the admin's account); `caseOrder` and `lastUsedCase` name the
* admin's cases. Everything else stays readable, because the page reads the synced feature
* voice config and dictated on the admin's account); `caseOrder`, `lastUsedCase`, `lastPickedCase` and `caseSettings` name
* the admin's cases. Everything else stays readable, because the page reads the synced feature
* switches from it. Writes are admin-only there (PUT /api/settings). Exported for unit testing.
*/
export function settingsForNonAdmin(settings: unknown): unknown {
@@ -169,6 +170,8 @@ export function settingsForNonAdmin(settings: unknown): unknown {
const out: Record<string, unknown> = { ...(settings as Record<string, unknown>) };
delete out.caseOrder;
delete out.lastUsedCase;
delete out.lastPickedCase;
delete out.caseSettings;
const voice = out.voiceSettings;
if (voice && typeof voice === 'object' && !Array.isArray(voice) && 'apiKey' in voice) {
const voiceOut: Record<string, unknown> = { ...(voice as Record<string, unknown>) };
@@ -1141,8 +1144,18 @@ export function registerSystemRoutes(
// It must never write tunnelEnabled:true: a concurrent stop has written false by now,
// and true on disk would show a stopped tunnel as on. The stored value stays as it is.
if (settings.tunnelEnabled === true && !tunnelEnableAuthorized) delete settingsToStore.tunnelEnabled;
const { before: existing } = await mutateSettingsFile((current) => ({ ...current, ...settingsToStore }));
const merged = { ...existing, ...settingsToStore };
// caseSettings merges per case inside the serialised writer, so two devices saving different cases both land.
const { caseSettings: incomingCases, ...plainSettings } = settingsToStore;
// The map is read and written only while syncCaseState is on, so a PUT that carries it with the setting off
// (as stored, or as this same PUT sets it) leaves the stored map alone.
const { before: existing, after } = await mutateSettingsFile((current) => ({
...current,
...plainSettings,
...(incomingCases && (plainSettings.syncCaseState ?? current.syncCaseState) === true
? { caseSettings: mergeCaseSettings(current.caseSettings, incomingCases as never) }
: {}),
}));
const merged = after ?? { ...existing, ...plainSettings };
// tmux 3.7+ resizes tracked panes; older versions apply this to new panes.
// Already-evicted history cannot be recovered on either version.
@@ -1244,6 +1257,12 @@ export function registerSystemRoutes(
return {};
} catch (err) {
if (err instanceof SharedCaseLimitError) {
throw Object.assign(err, {
statusCode: 400,
body: createErrorResponse(ApiErrorCode.INVALID_INPUT, err.message),
});
}
return settingsWriteErrorResponse(err);
}
});