using decePubClient.Extensions;
using decePubClient.Models;
using decePubClient.Models.Mastodon;
namespace decePubClient.Services
{
/// How a post reached PrivaPub (/api/privapub/v1/statuses/{id}/provenance), read on demand and kept for the session.
public class ProvenanceService(IHttpService httpService)
{
readonly Dictionary provenances = [];
public Provenance Known(string statusId) => statusId is not null ? provenances.GetValueOrDefault(statusId) : null;
/// The post's provenance, or null when PrivaPub has none to show.
public async Task Get(string statusId, CancellationToken cancellationToken = default)
{
if (Known(statusId) is { } known)
return known;
using var response = await httpService.GetAnon(APIs.PrivaPub.GET_M_Provenance(statusId), cancellationToken: cancellationToken);
if (!response.IsSuccessStatusCode)
return null;
var provenance = await response.MastodonReadFromJsonAsync(cancellationToken);
if (provenance is not null)
provenances[statusId] = provenance;
return provenance;
}
}
}