Files
Chronos/src/Chronos.Data/Processing/ProjectStore.cs
T
Alby96andClaude Opus 5.5 7e4a3b481d
CI / Build and test (push) Successful in 9m15s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 15m35s
CI / PostgreSQL 16 integration (push) Successful in 7m26s
CI / PostgreSQL 17 integration (push) Successful in 6m47s
CI / Docker image (push) Successful in 59s
Release / Server image (push) Successful in 1m24s
feat: owner review 2 phase 4a: Reorganize sequences after import (#12)
- Reorganize… on the web panel and the desktop: folder, file and title templates applied to existing sequences, with a dry run and an explicit Apply that runs as a job.
- One naming rule (FrameNaming) for import, upload and Reorganize.
- Crash-safe plan and file journals, held plans, stale-plan refusal; exports move with their folder.
- Writers refuse a folder another program moved or gave to another sequence.
- Server: POST /api/v1/archive/reorganize with a bounded plan cache; template rules.
- Phase 3 docs and translations merged; strings in all 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-27 21:58:39 +01:00

73 lines
3.9 KiB
C#

using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Repositories;
namespace Chronos.Data.Processing;
/// <summary>
/// The project of a sequence (BRIEF §7 <c>project.json</c>), mirrored in the index's settings column that the
/// server's API edits. The index copy is read first; writes go to both, under the sequence lock and with the index's
/// settings version as an optimistic check, so a concurrent edit is applied on top of, never overwritten.
/// </summary>
internal sealed class ProjectStore(ChronosDatabase db, SequenceWriter writer)
{
private const int Attempts = 3;
/// <summary>The settings in force: the index copy, else <c>project.json</c>, else defaults.</summary>
/// <exception cref="InvalidDataException">The settings are not valid JSON or come from a newer Chronos.</exception>
public static ProjectSettings Read(Sequence sequence, SequencePaths folder) =>
Parse(sequence.SettingsJson ?? (File.Exists(folder.ProjectPath) ? AtomicFile.ReadAllText(folder.ProjectPath) : null));
/// <exception cref="InvalidDataException">The settings are not valid JSON or come from a newer Chronos.</exception>
public static ProjectSettings Parse(string? json)
{
if (string.IsNullOrWhiteSpace(json)) return new ProjectSettings();
try
{
return ProjectSettings.FromJson(json);
}
catch (JsonException ex)
{
throw new InvalidDataException(ex.Message, ex);
}
}
/// <summary>Applies <paramref name="edit"/> to the settings in force and stores the result when it differs; false when nothing changed.</summary>
/// <exception cref="InvalidDataException">The stored settings cannot be read; nothing is written.</exception>
public async Task<bool> UpdateAsync(ArchivePaths paths, Sequence sequence, Func<ProjectSettings, ProjectSettings> edit, CancellationToken cancellationToken)
{
await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false))
return await UpdateLockedAsync(paths, sequence, edit, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// <see cref="UpdateAsync"/> for a caller that holds the sequence lock already. The file goes to the folder of the row
/// read under the lock: a Reorganize moves the folder and its row under the same lock.
/// </summary>
/// <exception cref="InvalidDataException">The stored settings cannot be read; nothing is written.</exception>
public async Task<bool> UpdateLockedAsync(ArchivePaths paths, Sequence sequence, Func<ProjectSettings, ProjectSettings> edit, CancellationToken cancellationToken)
{
for (var attempt = 1; ; attempt++)
{
var row = await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) ?? sequence;
var folder = paths.Sequence(row.Folder);
var current = Read(row, folder);
var before = current.ToJson();
var edited = edit(current);
var after = edited.ToJson();
if (after == before) return false;
AtomicFile.WriteAllText(folder.ProjectPath, after);
// The Library shows the project's hero frame (advice fills it, a sync takes another PC's).
var hero = await HeroFrame.EditAsync(db, sequence.Id, current.HeroFrame, edited.HeroFrame, cancellationToken).ConfigureAwait(false);
var result = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = after, HeroFrameId = hero }, row.SettingsVersion,
cancellationToken).ConfigureAwait(false);
// A conflict means the API changed the settings meanwhile: apply the edit again on top of them.
if (result.Outcome != SequenceEditOutcome.Conflict || attempt == Attempts) return true;
}
}
}