30 lines
1.1 KiB
C#
30 lines
1.1 KiB
C#
using decePubClient.Extensions;
|
|||
|
|
using decePubClient.Models;
|
||
|
|
using decePubClient.Models.Mastodon;
|
||
|
|
|
||
|
|
namespace decePubClient.Services
|
||
|
|
{
|
||
|
|
/// <summary>How a post reached PrivaPub (/api/privapub/v1/statuses/{id}/provenance), read on demand and kept for the session.</summary>
|
||
|
|
public class ProvenanceService(IHttpService httpService)
|
||
|
|
{
|
||
|
|
readonly Dictionary<string, Provenance> provenances = [];
|
||
|
|
|
||
|
|
public Provenance Known(string statusId) => statusId is not null ? provenances.GetValueOrDefault(statusId) : null;
|
||
|
|
|
||
|
|
/// <summary>The post's provenance, or null when PrivaPub has none to show.</summary>
|
||
|
|
public async Task<Provenance> 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<Provenance>(cancellationToken);
|
||
|
|
if (provenance is not null)
|
||
|
|
provenances[statusId] = provenance;
|
||
|
|
return provenance;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|