feat(server): publish card imports as cardEvent

BRIEF §14.3 lists cardEvent among the WebSocket events, but the server had
none. A client importing a card into the server now reports its start,
progress (at most once a second, a report still waiting replaced by the
next) and end to the new POST /api/v1/uploads/cards (Upload permission;
import id, card name, state, done, total, failed), and the server publishes
a CardEvent naming the account and client to every connected client; the
protocol client parses it and offers ReportCardAsync. A report that fails is
logged and never stops the import; a server without the endpoint gets no
more reports (SRS-10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 09:40:53 +00:00
1 parent 01e17c9961
commit bb55e47b47
9 files changed
+376 -6

No files matched your search

+19 -1
View File
@@ -1,5 +1,6 @@
using Titano.Core.Model;
using Titano.Protocol;
using Titano.Server.Events;
using Titano.Server.Security;
using Titano.Server.Storage;
@@ -7,10 +8,13 @@ namespace Titano.Server.Api;
/// <summary>
/// <c>/api/v1/uploads</c> (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
/// which start and complete through the node endpoints.
/// which start and complete through the node endpoints. <c>POST /uploads/cards</c> takes the reports of a card import a
/// client streams through these uploads and tells every connected client (BRIEF §14.3 <c>cardEvent</c>).
/// </summary>
public static class UploadEndpoints
{
private const int MaxCardName = 200;
public static void Map(RouteGroupBuilder api)
{
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
@@ -23,6 +27,7 @@ public static class UploadEndpoints
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
.Requires(Permissions.Upload);
uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
}
private static async Task<IResult> StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
@@ -32,6 +37,19 @@ public static class UploadEndpoints
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
}
/// <summary>A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204.</summary>
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, EventHub events)
{
if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
var card = report.Card?.Trim();
if (string.IsNullOrEmpty(card) || card.Length > MaxCardName || card.Any(char.IsControl)) return ApiErrors.BadRequest(context, "bad_request", "card");
if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
return ApiErrors.BadRequest(context, "bad_request", "done");
events.Publish(new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username, caller.ClientName));
return Results.NoContent();
}
/// <summary>
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.