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>
This commit is contained in:
Paul Schneider 2026-11-16 14:56:39 +00:00
commit 4493ac0149
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
5 changed files with 76 additions and 7 deletions

View file

@ -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();

View file

@ -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;
}