diff --git a/src/Titano.Data/Processing/HeroFrame.cs b/src/Titano.Data/Processing/HeroFrame.cs new file mode 100644 index 00000000..4cd49eca --- /dev/null +++ b/src/Titano.Data/Processing/HeroFrame.cs @@ -0,0 +1,26 @@ +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Data.Repositories; + +namespace Titano.Data.Processing; + +/// +/// The index's hero frame (, the frame the Library shows) follows the project's hero +/// (, a frame index; frame indices are positions). +/// +public static class HeroFrame +{ + /// + /// The of a project whose hero went from to + /// : null when it did not change, else the frame at the new index, or + /// (the first frame shows) when there is none. + /// + public static async Task EditAsync(TitanoDatabase db, Guid sequenceId, int? before, int? after, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(db); + if (before == after) return null; + if (after is not >= 0) return Guid.Empty; + var frames = await db.Frames.ListBySequenceAsync(sequenceId, after.Value - 1, 1, cancellationToken).ConfigureAwait(false); + return frames is [{ } frame] && frame.Index == after ? frame.Id : Guid.Empty; + } +} diff --git a/src/Titano.Data/Processing/ProjectStore.cs b/src/Titano.Data/Processing/ProjectStore.cs index 1479d459..9204c0f9 100644 --- a/src/Titano.Data/Processing/ProjectStore.cs +++ b/src/Titano.Data/Processing/ProjectStore.cs @@ -53,7 +53,7 @@ internal sealed class ProjectStore(TitanoDatabase db, SequenceWriter writer) 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 = edited.HeroFrame == current.HeroFrame ? (Guid?)null : await HeroFrameIdAsync(sequence.Id, edited.HeroFrame, cancellationToken).ConfigureAwait(false); + 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. @@ -61,12 +61,4 @@ internal sealed class ProjectStore(TitanoDatabase db, SequenceWriter writer) } } } - - /// The id of the frame at (frame indices are positions), or to clear the hero. - private async Task HeroFrameIdAsync(Guid sequenceId, int? index, CancellationToken cancellationToken) - { - if (index is not >= 0) return Guid.Empty; - var frames = await db.Frames.ListBySequenceAsync(sequenceId, index.Value - 1, 1, cancellationToken).ConfigureAwait(false); - return frames is [{ } frame] && frame.Index == index ? frame.Id : Guid.Empty; - } } diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs index dd7f68d6..b080ea22 100644 --- a/src/Titano.Server/Api/SequenceEndpoints.cs +++ b/src/Titano.Server/Api/SequenceEndpoints.cs @@ -3,7 +3,9 @@ using System.Globalization; using System.Text; using System.Text.Json; using Titano.Core.Model; +using Titano.Core.Project; using Titano.Data; +using Titano.Data.Processing; using Titano.Data.Repositories; using Titano.Protocol; using Titano.Server.Configuration; @@ -131,7 +133,9 @@ public static class SequenceEndpoints Rating = request.Rating, Notes = request.Notes, SettingsJson = request.SettingsJson, - HeroFrameId = request.HeroFrameId, + // A client that sends only the settings (the sync of a local copy) moves the hero frame with the project's hero. + HeroFrameId = request.HeroFrameId + ?? (request.SettingsJson is { } settings ? await SettingsHeroAsync(db, id, settings, context.RequestAborted) : null), }; var result = await db.Sequences.EditAsync(id, edit, expected, context.RequestAborted); switch (result.Outcome) @@ -293,6 +297,22 @@ public static class SequenceEndpoints return false; } + /// The hero frame edit of new settings: the frame of their hero when it is not the stored settings' hero. + private static async Task SettingsHeroAsync(TitanoDatabase db, Guid id, string settingsJson, CancellationToken ct) + { + if (await db.Sequences.GetAsync(id, ct) is not { } current) return null; + try + { + var before = string.IsNullOrWhiteSpace(current.SettingsJson) ? null : ProjectSettings.FromJson(current.SettingsJson).HeroFrame; + return await HeroFrame.EditAsync(db, id, before, ProjectSettings.FromJson(settingsJson).HeroFrame, ct); + } + catch (Exception ex) when (ex is JsonException or InvalidDataException) + { + // Not a project of this Titano (a newer one, or free JSON): its hero is unknown here, the stored one stays. + return null; + } + } + private static bool IsJson(string text) { try @@ -314,6 +334,7 @@ public static class SequenceEndpoints if (r.Rating is not null) fields.Add("rating"); if (r.Notes is not null) fields.Add("notes"); if (r.SettingsJson is not null) fields.Add("settings"); + if (r.HeroFrameId is not null) fields.Add("hero"); return string.Join(", ", fields); } diff --git a/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs index a87edf7c..26018cc8 100644 --- a/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs +++ b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs @@ -73,6 +73,33 @@ public sealed class RemoteEditingTests(TestContext context) Assert.Null(cleared.HeroFrameId); } + [Test] + public async Task SettingsAloneMoveTheHeroFrameWithTheProjectsHero() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, token) = await server.UserAsync("alberto", "editor"); + using var client = Client(server, token); + await Uploads.UploadAsync(editor, Uploads.Photo(2000, 46), "a.CR3", sessionKey: "pier"); + var second = await Uploads.UploadAsync(editor, Uploads.Photo(2000, 47), "b.CR3", sessionKey: "pier"); + var id = second.SequenceId; + var read = await client.GetSequenceAsync(id, context.Cancellation); + + // The sync of a local copy sends the project only. + var project = new ProjectSettings { HeroFrame = second.Index }; + var hero = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, project.ToJson(), read.SettingsVersion), + context.Cancellation); + Assert.Equal(second.FrameId, hero.HeroFrameId); + + var exposure = project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.3 } }; + var kept = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, exposure.ToJson(), hero.SettingsVersion), + context.Cancellation); + Assert.Equal(second.FrameId, kept.HeroFrameId, "other changes keep the hero"); + + var cleared = await client.UpdateSequenceAsync(id, new UpdateSequenceRequest(null, null, null, null, (exposure with { HeroFrame = null }).ToJson(), + kept.SettingsVersion), context.Cancellation); + Assert.Null(cleared.HeroFrameId, "no hero shows the first frame"); + } + [Test] public async Task JobsPostedOnePerPresetReadBackAsTheClientsJobs() {