feat/calls #62
5 changed files with 76 additions and 7 deletions
Add refined replay draft API client
Create lives with RefineReplay and RetentionDays, save and publish drafts, delete published originals and read the owner-only draft source by bounded, verified byte ranges. PodcastReplayMediaInput can stream the draft source. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
commit
4493ac0149
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
### Added
|
||||
|
||||
* [Yavsc.Api.Client/Podcasts] Add the refined replay client: create a live with `RefineReplay` and `RetentionDays`, save and publish drafts, delete published originals, and read the owner-only draft source by bounded, verified byte ranges. `PodcastReplayMediaInput` can stream the draft source for the upcoming PostIt editor.
|
||||
|
||||
* [Yavsc.Server/Podcasts] Apply the retention of refined replays: `RetentionDays` (0–365, default 7; migration `PodcastReplayRetention` adds `LiveFlow.RetentionDays`) sets `OriginalsExpireAt` from the end of the live, or from publication with 0 days. The replay worker then deletes the segments, source and draft of published emissions (`OriginalsDeleted`, the MP4 stays playable but no longer re-editable) and deletes emissions never published by the deadline, marking their flow deleted. The owner can purge published originals early with `DELETE podcasts/{id}/originals`. The web catalogue keeps showing the published MP4 after the purge.
|
||||
|
||||
* [Yavsc.Server/Podcasts] Add the private replay draft for emissions opted into refinement (`RefineReplay`, ignored for OBS; migration `PodcastRefineReplay` adds `LiveFlow.RefineReplay`). After the live, or after an interruption, the segments are remuxed into an owner-only `source.mp4` draft (`Preparing`, `Ready`, `Publishing`, `Failed`); the owner saves revisioned marks and removed ranges (`PUT podcasts/{id}/draft`, 409 on stale revision, marks inside removed ranges dropped), streams the source (`GET/HEAD podcasts/{id}/draft/source.mp4`) and queues an idempotent publication (`POST podcasts/{id}/draft/publish`). The worker renders or copies the source within quota and atomically replaces `replay.mp4`, keeping the previous replay on failure; re-edition is supported. Until publication, playlist and segments return 403 to other users, drafts are hidden from listeners, and the web catalogue shows "Replay en préparation". The FFmpeg editor now detects audio itself and keeps the last range open to the real end of the media.
|
||||
|
|
|
|||
|
|
@ -271,7 +271,13 @@ Le brouillon privé est implémenté côté serveur ; l'éditeur PostIt reste à
|
|||
émission jamais publiée est supprimée et son `LiveFlow` marqué supprimé.
|
||||
`DELETE podcasts/{id}/originals` permet à l'auteur la même purge dès qu'un
|
||||
MP4 est publié (409 sinon, sans effet si déjà faite).
|
||||
- **Reste à faire** : chapitres, client et éditeur PostIt (réglage de
|
||||
- **Client d'API** : `PodcastApiClient` expose `CreateAsync(CreatePodcastRequest)`
|
||||
(raffinage et conservation), `SaveDraftAsync`, `PublishDraftAsync`,
|
||||
`DeleteOriginalsAsync` et `DownloadDraftSourceRangeAsync`, lecture
|
||||
authentifiée par plages de 256 Kio au plus de la source du brouillon, avec
|
||||
les mêmes contrôles que le MP4 publié. `PodcastReplayMediaInput` lit cette
|
||||
source avec `draftSource: true` pour l'éditeur.
|
||||
- **Reste à faire** : chapitres, interface et éditeur PostIt (réglage de
|
||||
conservation, échéance affichée, proposition de suppression après publication).
|
||||
|
||||
Une émission interrompue doit rester récupérable par son auteur. Lorsqu'un
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using Avalonia.Headless;
|
|||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Interactivity;
|
||||
using PostIt.Services;
|
||||
using Yavsc.Abstract.Streaming;
|
||||
using Yavsc.Api.Client;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
|
@ -138,6 +139,31 @@ public sealed class PodcastReplayTests
|
|||
api.DownloadReplayRangeAsync(Id, 0, 256 * 1024 + 1, transport.Bytes.Length, _ct));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Draft_client_targets_owner_routes_and_reads_the_draft_source_by_range()
|
||||
{
|
||||
await using var transport = new RangeTransport();
|
||||
var api = new PodcastApiClient(transport, () => "https://api.example/");
|
||||
var prefix = $"https://api.example/api/v1/podcasts";
|
||||
await api.CreateAsync(new CreatePodcastRequest { Title = "Live", RefineReplay = true, RetentionDays = 0 }, _ct);
|
||||
var save = new SavePodcastDraftRequest { Revision = 2, Marks = [1000], RemovedRanges = [new(0, 500)] };
|
||||
await api.SaveDraftAsync(Id, save, _ct);
|
||||
await api.PublishDraftAsync(Id, 3, _ct);
|
||||
await api.DeleteOriginalsAsync(Id, _ct);
|
||||
Assert.Equal([$"POST {prefix}", $"PUT {prefix}/{Id}/draft", $"POST {prefix}/{Id}/draft/publish",
|
||||
$"DELETE {prefix}/{Id}/originals"], transport.Calls.Select(call => call.Call));
|
||||
var create = Assert.IsType<CreatePodcastRequest>(transport.Calls[0].Body);
|
||||
Assert.True(create.RefineReplay);
|
||||
Assert.Equal(0, create.RetentionDays);
|
||||
Assert.Same(save, transport.Calls[1].Body);
|
||||
Assert.Equal(3, Assert.IsType<PublishPodcastDraftRequest>(transport.Calls[2].Body).Revision);
|
||||
Assert.Null(transport.Calls[3].Body);
|
||||
|
||||
Assert.Equal(transport.Bytes.Skip(5).Take(4), await api.DownloadDraftSourceRangeAsync(Id, 5, 4, transport.Bytes.Length, _ct));
|
||||
Assert.Equal($"{prefix}/{Id}/draft/source.mp4", transport.Url);
|
||||
Assert.Equal("bytes=5-8", transport.Range);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("full")]
|
||||
[InlineData("range")]
|
||||
|
|
@ -315,7 +341,12 @@ public sealed class PodcastReplayTests
|
|||
Failure == "range" ? from + 1 : from, to, Failure == "size" ? Bytes.Length + 1 : Bytes.Length);
|
||||
return Task.FromResult(response);
|
||||
}
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public List<(string Call, object? Body)> Calls { get; } = [];
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
||||
{
|
||||
Calls.Add(($"{method} {path}", body));
|
||||
return Task.FromResult(default(T)!);
|
||||
}
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, Func<HttpContent> factory, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public Task CallAsync(HttpMethod method, string path, object? body = null, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public Task CallAsync(HttpMethod method, string path, Func<HttpContent> factory, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
|
|
|
|||
|
|
@ -11,17 +11,20 @@ public sealed class PodcastReplayMediaInput : MediaInput
|
|||
private readonly PodcastApiClient _api;
|
||||
private readonly Guid _id;
|
||||
private readonly long _length;
|
||||
private readonly bool _draftSource;
|
||||
private readonly CancellationTokenSource _cancellation = new();
|
||||
private byte[] _buffer = [];
|
||||
private long _bufferStart;
|
||||
private long _position;
|
||||
public event Action<string>? Failed;
|
||||
|
||||
public PodcastReplayMediaInput(PodcastApiClient api, Guid id, long length)
|
||||
/// <param name="draftSource">Reads the owner-only draft source instead of the published MP4.</param>
|
||||
public PodcastReplayMediaInput(PodcastApiClient api, Guid id, long length, bool draftSource = false)
|
||||
{
|
||||
_api = api;
|
||||
_id = id;
|
||||
_length = length;
|
||||
_draftSource = draftSource;
|
||||
CanSeek = true;
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +53,9 @@ public sealed class PodcastReplayMediaInput : MediaInput
|
|||
{
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(_cancellation.Token);
|
||||
timeout.CancelAfter(TimeSpan.FromSeconds(20));
|
||||
_buffer = _api.DownloadReplayRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token)
|
||||
_buffer = (_draftSource
|
||||
? _api.DownloadDraftSourceRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token)
|
||||
: _api.DownloadReplayRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token))
|
||||
.GetAwaiter().GetResult();
|
||||
_bufferStart = _position;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ public sealed class PodcastApiClient(IYavscApiClient api, Func<string> baseAddre
|
|||
=> api.CallAsync<List<PodcastResponse>>(HttpMethod.Get, Absolute(Prefix), ct: ct);
|
||||
|
||||
public Task<PodcastResponse> CreateAsync(string title, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute(Prefix), new CreatePodcastRequest { Title = title }, ct);
|
||||
=> CreateAsync(new CreatePodcastRequest { Title = title }, ct);
|
||||
|
||||
/// <summary>Starts an immediate live, optionally refined with its retention.</summary>
|
||||
public Task<PodcastResponse> CreateAsync(CreatePodcastRequest request, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute(Prefix), request, ct);
|
||||
|
||||
public Task<PodcastResponse> PrepareAsync(PreparePodcastRequest request, Guid? id = null, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(id.HasValue ? HttpMethod.Put : HttpMethod.Post,
|
||||
|
|
@ -56,15 +60,36 @@ public sealed class PodcastApiClient(IYavscApiClient api, Func<string> baseAddre
|
|||
public Task RetryReplayAsync(Guid id, CancellationToken ct = default)
|
||||
=> api.CallAsync(HttpMethod.Post, Absolute($"{Prefix}/{id}/replay/retry"), ct: ct);
|
||||
|
||||
public async Task<byte[]> DownloadReplayRangeAsync(Guid id, long offset, int count, long size,
|
||||
/// <summary>Saves the marks and removed ranges of the owned draft; a stale revision fails with 409.</summary>
|
||||
public Task<PodcastResponse> SaveDraftAsync(Guid id, SavePodcastDraftRequest request, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Put, Absolute($"{Prefix}/{id}/draft"), request, ct);
|
||||
|
||||
/// <summary>Queues the publication of a saved draft revision; repeating it is harmless.</summary>
|
||||
public Task<PodcastResponse> PublishDraftAsync(Guid id, int revision, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute($"{Prefix}/{id}/draft/publish"),
|
||||
new PublishPodcastDraftRequest { Revision = revision }, ct);
|
||||
|
||||
/// <summary>Deletes the originals and draft of a published refined replay; the MP4 remains.</summary>
|
||||
public Task<PodcastResponse> DeleteOriginalsAsync(Guid id, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Delete, Absolute($"{Prefix}/{id}/originals"), ct: ct);
|
||||
|
||||
public Task<byte[]> DownloadReplayRangeAsync(Guid id, long offset, int count, long size,
|
||||
CancellationToken ct = default)
|
||||
=> DownloadRangeAsync($"{Prefix}/{id}/replay.mp4", offset, count, size, ct);
|
||||
|
||||
/// <summary>Reads a byte range of the owner-only seekable source of a refined replay draft.</summary>
|
||||
public Task<byte[]> DownloadDraftSourceRangeAsync(Guid id, long offset, int count, long size,
|
||||
CancellationToken ct = default)
|
||||
=> DownloadRangeAsync($"{Prefix}/{id}/draft/source.mp4", offset, count, size, ct);
|
||||
|
||||
private async Task<byte[]> DownloadRangeAsync(string path, long offset, int count, long size, CancellationToken ct)
|
||||
{
|
||||
if (offset < 0 || count <= 0 || count > 256 * 1024 || offset >= size)
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Invalid replay byte range.");
|
||||
var end = offset + Math.Min(count, size - offset) - 1;
|
||||
using var response = await api.SendAuthenticatedAsync(() =>
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, Absolute($"{Prefix}/{id}/replay.mp4"));
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, Absolute(path));
|
||||
request.Headers.Range = new RangeHeaderValue(offset, end);
|
||||
return request;
|
||||
}, ct).ConfigureAwait(false);
|
||||
|
|
|
|||
Loading…
Reference in a new issue