fix(sync): keep project positions on their photos when settings cross copies
Settings synced between a PC and its server sequence were stored as they
came, in the other copy's frame order. When the inserted photos arrived
later (a download, an upload), the index moved those already-correct
positions a second time, so exclusions, the hero and camera keyframes
landed on other photos (V-21, CD-2).
- Settings are translated by photo (SHA-256, else size and capture time)
to the frames of the copy they go to: the server's before they are
compared or applied here, this PC's to the server's frames as its
manifest lists them before a push (SyncPositions).
- A position on a photo the receiving copy lacks waits in project.json as
a pending frame and takes its place when the photo arrives, on an
insertion or an append in step with the index; the Library's hero
frame follows. A hero chosen meanwhile wins, and advice does not fill
a hero while one waits. Old projects without the member are unchanged.
- Pushes send expectedFramesVersion; PATCH /api/v1/sequences/{id} answers
409 frames_conflict when the frames changed since, and the next
comparison translates again.
- A pulled payload equal to the local copy is settled without the
archive: an offline or read-only archive no longer fails the
comparison (V-10).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
25 files changed
+1091
-51
No files matched your search
@@ -0,0 +1,88 @@
|
||||
namespace Titano.Core.Project;
|
||||
|
||||
/// <summary>
|
||||
/// A photo as every copy of a sequence knows it (BRIEF §14.5: originals are content-addressed): its SHA-256 once hashed,
|
||||
/// else its size and capture time (a folder adopted and not verified yet). Frame ids and positions differ between a
|
||||
/// server sequence and a PC's copy of it; the photo does not.
|
||||
/// </summary>
|
||||
/// <param name="Sha256">Hex SHA-256; null or empty while the copy has not hashed the file.</param>
|
||||
public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt)
|
||||
{
|
||||
public bool IsHashed => !string.IsNullOrEmpty(Sha256);
|
||||
|
||||
/// <summary>
|
||||
/// The same photo: the same SHA-256 when both are hashed, otherwise the same size and capture time (to the
|
||||
/// millisecond, as databases keep it).
|
||||
/// </summary>
|
||||
public bool Matches(FramePhoto other)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(other);
|
||||
if (IsHashed && other.IsHashed) return string.Equals(Sha256, other.Sha256, StringComparison.OrdinalIgnoreCase);
|
||||
return Size == other.Size && CapturedAt is { } mine && other.CapturedAt is { } theirs && Millisecond(mine) == Millisecond(theirs);
|
||||
}
|
||||
|
||||
internal static long Millisecond(DateTime time) => time.Ticks / TimeSpan.TicksPerMillisecond;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among
|
||||
/// them. Immutable once built, so parallel workers may share it.
|
||||
/// </summary>
|
||||
public sealed class FramePhotos
|
||||
{
|
||||
private readonly FramePhoto?[] _byIndex;
|
||||
private readonly Dictionary<string, int> _bySha = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<(long Size, long Time), List<int>> _bySizeAndTime = [];
|
||||
|
||||
/// <param name="frames">Each frame's index and photo, in any order.</param>
|
||||
public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frames);
|
||||
var list = frames.Where(f => f.Index >= 0).ToList();
|
||||
_byIndex = new FramePhoto?[list.Count == 0 ? 0 : list.Max(f => f.Index) + 1];
|
||||
foreach (var (index, photo) in list) _byIndex[index] = photo;
|
||||
for (var i = 0; i < _byIndex.Length; i++)
|
||||
{
|
||||
if (_byIndex[i] is not { } photo) continue;
|
||||
if (photo.IsHashed) _bySha.TryAdd(photo.Sha256!.ToLowerInvariant(), i);
|
||||
if (photo.CapturedAt is not { } time) continue;
|
||||
var key = (photo.Size, FramePhoto.Millisecond(time));
|
||||
if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = [];
|
||||
same.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count => _byIndex.Length;
|
||||
|
||||
/// <summary>The photo at <paramref name="index"/>; null when no frame has that index.</summary>
|
||||
public FramePhoto? this[int index] => (uint)index < (uint)_byIndex.Length ? _byIndex[index] : null;
|
||||
|
||||
/// <summary>Some frame is not hashed yet: matching it needs capture times.</summary>
|
||||
public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false });
|
||||
|
||||
/// <summary>The index of the first frame holding <paramref name="photo"/> (<see cref="FramePhoto.Matches"/>); null when none does.</summary>
|
||||
public int? IndexOf(FramePhoto photo)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(photo);
|
||||
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed;
|
||||
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return null;
|
||||
foreach (var index in candidates)
|
||||
{
|
||||
if (_byIndex[index]!.Matches(photo)) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
|
||||
public bool SameAs(FramePhotos other)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(other);
|
||||
if (other.Count != Count) return false;
|
||||
for (var i = 0; i < Count; i++)
|
||||
{
|
||||
var (mine, theirs) = (this[i], other[i]);
|
||||
if (mine is null != theirs is null || (mine is not null && !mine.Matches(theirs!))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user