feat(app): workspace projects whose home is a server, pushed and pulled like this PC's archive
Owner review 2, R8, phase 5b (client part), ported onto the final phase 5a.
A Chronos Server with the "projects" capability is now the home of
workspace projects. A server sequence is edited in the workspace like a
local one, not inside the library, and its versions sync with the server
"like git".
- Data: ServerProjectHome is an IProjectHome over ChronosClient.
- These are HomeUnavailableException, so the workspace shows Offline: a
server out of reach, an expired sign-in, a 503 (lease, offline archive,
projects_unreadable), a 409 folder_in_use, and a server without projects.
- 404 sequence/project/version and refused names are ProjectException;
other refusals keep their meaning.
- A deleted project's head is 404 project, so final 5a's HeadAsync NotFound
rule reads it as Gone. The original 5b's IsLiveAsync and
WorkspaceStatus.Gone are dropped. IProjectHome gains rename and delete.
- Data: WorkspaceJobs.CopyFromServer downloads into the workspace, through
the desktop's session, the proxies of one frames version (or the
thumbnails standing in for them), with the aliases of the server's
conversions. It is rebuilt on final 5a's copy handler:
- the copy:frame:fv:folder checkpoint, EnsureSpace (from the server's proxy
sizes), WritingMedia, and the cancelled-copy clean-up;
- the editor's current file is never overwritten;
- each file is hashed as it arrives and checked on disk, four at a time.
It never writes this PC's archive. Originals are still refused until 5c.
- Data: SyncAgent passes on the server's projectChanged events, and after a
lost event stream it reports that changes may have been missed.
- App: SyncHost gives the server home over one session client. The Server
screen offers Open in workspace, disabled with its reason on older
servers, whose sequences keep opening with proxies.
- App: Edit opens or checks out server projects with final 5a's refusals:
unreadable copies, a disabled damaged or newer Main, a refused folder. The
"sequence left the index" refusal now applies only to projects of this
PC's archive.
- App: the picker and the chip follow projectChanged (Server newer, Not on
server). Every state, conflict, delete, remove and failure text uses its
".Server" form when one exists (WorkspaceText.ForHome). That includes 5a's
newer-format conflict, the save conflict, the taken-values toast, Gone and
version-too-large.
- App: a render of a server project first offers to send what is not on the
server, then renders the version the server holds. Offline, Gone, Damaged
and missing media hold the render. Analyses and renders run as server jobs
(ServerJobs, shared with RemoteSequenceSource).
- Pruning keeps final 5a's rule: a version is pruned only after HoldsAsync
reads it back.
- Strings in en and it; ARCHITECTURE, DECISIONS and CHANGELOG.
Tests:
- ServerProjectHome error mapping, using a fake handler.
- Against the in-process server:
- two workspaces on one project: fast-forward, replay and merge, keep mine,
take server, save both, restore;
- the copy job verifying, resuming and following new frames;
- a project deleted on the server (Gone from its head; a push sends
nothing);
- an offline server.
- SyncAgent project events.
- App tests for the host, the project menu and renders of a server home.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2ecc409a3
commit
26bab759fe
48 files changed
+3243
-409
No files matched your search
@@ -0,0 +1,192 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Protocol.Client;
|
||||
|
||||
namespace Chronos.Data.Projects;
|
||||
|
||||
/// <summary>
|
||||
/// A sequence of a Chronos Server as its projects' home (owner review 2, R8, phase 5b): the server's
|
||||
/// <c>/api/v1/sequences/{sid}/projects</c> through <see cref="ChronosClient"/>, read into the model a workspace works with
|
||||
/// (<see cref="ProjectWire"/>), so a workspace pushes to and pulls from a server as it does with this PC's archive. A
|
||||
/// server out of reach (no answer, a timeout, a certificate it no longer presents, an answer that is not the API's), a
|
||||
/// sign-in that expired, a sequence the server holds busy or an archive offline there (503), a project file it cannot write
|
||||
/// for now and a server that keeps no projects are <see cref="HomeUnavailableException"/>s: the workspace shows Offline and
|
||||
/// its work waits. A sequence, project or version the server does not have, and a name it refuses, are
|
||||
/// <see cref="ProjectException"/>s as on this PC's archive; anything else (an account without the right, a request the
|
||||
/// server takes for wrong) keeps its <see cref="ChronosApiException"/>. A push answers its outcome without throwing (a
|
||||
/// divergence carries the head). The client is the caller's: the home never disposes it, and a client disposed meanwhile
|
||||
/// (the session ended) is a home out of reach.
|
||||
/// </summary>
|
||||
public sealed class ServerProjectHome : IProjectHome
|
||||
{
|
||||
/// <summary>The largest page of versions the server answers.</summary>
|
||||
private const int MaxPage = 200;
|
||||
|
||||
/// <param name="serverName">The server's name as its <c>server/info</c> gives it, for the project file; the address's host when null.</param>
|
||||
public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(client);
|
||||
Client = client;
|
||||
SequenceId = sequenceId;
|
||||
Server = SyncService.ServerKey(client.BaseAddress);
|
||||
ServerName = string.IsNullOrWhiteSpace(serverName) ? client.BaseAddress.Host : serverName.Trim();
|
||||
}
|
||||
|
||||
public ChronosClient Client { get; }
|
||||
|
||||
/// <summary>The server's sequence.</summary>
|
||||
public Guid SequenceId { get; }
|
||||
|
||||
/// <summary>The server as workspace projects and sync links name it (<see cref="SyncService.ServerKey"/>).</summary>
|
||||
public string Server { get; }
|
||||
|
||||
public string ServerName { get; }
|
||||
|
||||
/// <summary>The home of a workspace project of this server's sequence.</summary>
|
||||
public BundleHome Home => new(BundleHome.ServerKind, SequenceId, null, Server);
|
||||
|
||||
/// <remarks>
|
||||
/// A project file names an archive by id: a server's sequences carry one made from the server's key
|
||||
/// (<see cref="ArchiveIdOf"/>), with the server's name and no path.
|
||||
/// </remarks>
|
||||
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) =>
|
||||
Reach(async () =>
|
||||
{
|
||||
var sequence = await Client.GetSequenceAsync(SequenceId, cancellationToken).ConfigureAwait(false);
|
||||
return new HomeSequence(new BundleArchive(ArchiveIdOf(Server), ServerName, null), new BundleSequence(sequence.Id, sequence.Folder, sequence.DisplayTitle,
|
||||
sequence.FrameCount), Home);
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The archive id a server's sequences carry in a workspace's project file, which names one: the same for every
|
||||
/// sequence of <paramref name="server"/> (<see cref="SyncService.ServerKey"/>), made from its key (a version 8 UUID),
|
||||
/// never one of this PC's archives.
|
||||
/// </summary>
|
||||
public static Guid ArchiveIdOf(string server)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
var hash = System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes("chronos-server-archive:" + server));
|
||||
hash[6] = (byte)((hash[6] & 0x0F) | 0x80);
|
||||
hash[8] = (byte)((hash[8] & 0x3F) | 0x80);
|
||||
return new Guid(hash.AsSpan(0, 16), bigEndian: true);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) =>
|
||||
Reach<IReadOnlyList<SequenceProject>>(async () =>
|
||||
[.. (await Client.ListProjectsAsync(SequenceId, cancellationToken).ConfigureAwait(false)).Select(ProjectWire.ToProject)], cancellationToken);
|
||||
|
||||
/// <remarks>The server records the account that makes the project as its maker, whatever <paramref name="author"/> says.</remarks>
|
||||
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
||||
Reach(async () => ProjectWire.ToProject(await Client.CreateProjectAsync(SequenceId, new CreateProjectRequest(name, fromVersionId), cancellationToken)
|
||||
.ConfigureAwait(false)), cancellationToken);
|
||||
|
||||
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) =>
|
||||
Reach(async () => ProjectWire.ToVersion(await Client.GetProjectHeadAsync(SequenceId, projectId, cancellationToken).ConfigureAwait(false)), cancellationToken);
|
||||
|
||||
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
||||
Reach(async () => await Client.GetProjectVersionAsync(SequenceId, projectId, versionId, cancellationToken).ConfigureAwait(false) is { } version
|
||||
? ProjectWire.ToVersion(version)
|
||||
: null, cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
||||
Reach<IReadOnlyList<ProjectVersion>>(async () =>
|
||||
[
|
||||
.. (await Client.ListProjectVersionsAsync(SequenceId, projectId, after: Math.Max(0, afterNumber), limit: Page(limit), ct: cancellationToken)
|
||||
.ConfigureAwait(false)).Items.Select(ProjectWire.ToVersion),
|
||||
], cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
||||
Reach<IReadOnlyList<ProjectVersion>>(async () =>
|
||||
[
|
||||
.. (await Client.ListProjectVersionsAsync(SequenceId, projectId, before: beforeNumber is { } before ? Math.Max(0, before) : null, limit: Page(limit),
|
||||
ct: cancellationToken).ConfigureAwait(false)).Items.Select(ProjectWire.ToVersion),
|
||||
], cancellationToken);
|
||||
|
||||
/// <remarks>The server numbers the version (head + 1) and records the account that pushed it as its author.</remarks>
|
||||
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(version);
|
||||
return Reach(async () => ProjectWire.ToResult(await Client.PushProjectVersionAsync(SequenceId, projectId, ProjectWire.ToDto(version), cancellationToken)
|
||||
.ConfigureAwait(false)), cancellationToken);
|
||||
}
|
||||
|
||||
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) =>
|
||||
Reach(async () => ProjectWire.ToHomeFrames(await Client.GetProjectFramesAsync(SequenceId, cancellationToken).ConfigureAwait(false)), cancellationToken);
|
||||
|
||||
public Task<SequenceProject> RenameAsync(Guid projectId, string name, CancellationToken cancellationToken) =>
|
||||
Reach(async () => ProjectWire.ToProject(await Client.RenameProjectAsync(SequenceId, projectId, name, cancellationToken).ConfigureAwait(false)),
|
||||
cancellationToken);
|
||||
|
||||
public Task DeleteAsync(Guid projectId, CancellationToken cancellationToken) =>
|
||||
Reach(async () =>
|
||||
{
|
||||
await Client.DeleteProjectAsync(SequenceId, projectId, cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="exception"/> says a server is out of reach for now (see the class remarks), not that it
|
||||
/// refused: what a workspace waits out.
|
||||
/// </summary>
|
||||
public static bool IsUnavailable(Exception exception, CancellationToken cancellationToken = default) => exception switch
|
||||
{
|
||||
HomeUnavailableException => true,
|
||||
// A refusal for good, which is an IOException too.
|
||||
Workspaces.WorkspaceSyncException => false,
|
||||
ChronosApiException api => Unavailable(api),
|
||||
UntrustedCertificateException or HttpRequestException or JsonException or ObjectDisposedException => true,
|
||||
// A timeout of the client's (not the caller's cancellation).
|
||||
OperationCanceledException => !cancellationToken.IsCancellationRequested,
|
||||
IOException => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
private static int Page(int limit) => Math.Clamp(limit, 1, MaxPage);
|
||||
|
||||
/// <summary>A refusal of the server as this PC's archive gives it, or a server out of reach.</summary>
|
||||
private static async Task<T> Reach<T>(Func<Task<T>> call, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call().ConfigureAwait(false);
|
||||
}
|
||||
catch (ChronosApiException ex) when (Refusal(ex) is { } refusal)
|
||||
{
|
||||
throw refusal;
|
||||
}
|
||||
catch (Exception ex) when (ex is not ProjectException && IsUnavailable(ex, cancellationToken))
|
||||
{
|
||||
throw new HomeUnavailableException(ex is ChronosApiException { Status: HttpStatusCode.Unauthorized }
|
||||
? $"The sign-in on the server expired: {ex.Message}"
|
||||
: $"The server cannot be reached: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The project error a refusal stands for (a sequence, project or version the server does not have, a name it refuses); null for any other.</summary>
|
||||
private static ProjectException? Refusal(ChronosApiException ex) => (ex.Status, ex.Error.Code, ex.Error.Details) switch
|
||||
{
|
||||
(HttpStatusCode.NotFound, _, "sequence") => new ProjectException(ProjectError.SequenceNotFound, ex.Message),
|
||||
(HttpStatusCode.NotFound, _, "project" or "version") => new ProjectException(ProjectError.NotFound, ex.Message),
|
||||
(HttpStatusCode.BadRequest, ProjectErrors.InvalidName, _) => new ProjectException(ProjectError.InvalidName, ex.Message),
|
||||
(HttpStatusCode.Conflict, ProjectErrors.NameTaken, _) => new ProjectException(ProjectError.NameTaken, ex.Message),
|
||||
(HttpStatusCode.Conflict, ProjectErrors.MainCannotBeDeleted, _) => new ProjectException(ProjectError.MainCannotBeDeleted, ex.Message),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// An answer that says the server cannot serve the call for now: busy, its archive offline or its sequence's folder
|
||||
/// moved (503), a gateway or a timeout, the sign-in expired (401), a project file it cannot write now (409
|
||||
/// <c>folder_in_use</c>), a server without the projects (404 with nothing named: the API's fallback), or a success
|
||||
/// that is no answer of the API (a portal's or a proxy's page).
|
||||
/// </summary>
|
||||
private static bool Unavailable(ChronosApiException ex) =>
|
||||
ex.IsTransient
|
||||
|| ex.Status == HttpStatusCode.Unauthorized
|
||||
|| (int)ex.Status is >= 200 and < 300
|
||||
|| ex is { Status: HttpStatusCode.Conflict, Error.Code: "folder_in_use" }
|
||||
|| ex is { Status: HttpStatusCode.NotFound, Error.Details: null or "" };
|
||||
}
|
||||
Reference in new issue
Block a user