using System.Net;
using collAnon.Client.Services;
using decePubClient.Extensions;
using decePubClient.Models;
using decePubClient.Models.Mastodon;
using PrivaPub.ClientModels;
namespace decePubClient.Services
{
/// Which order the directory lists accounts in.
public enum DirectoryOrder
{
/// By their latest public post.
Active,
/// By the day they say they joined.
New
}
///
/// What there is to find on PrivaPub: hashtags (their week, following them), what is trending there (hashtags, posts
/// and links, from public posts it already holds) and the directory of accounts that let themselves be discovered.
/// Lists come back empty and values null on failure; nothing throws.
///
public class DiscoveryService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
{
public async Task Tag(string name, CancellationToken cancellationToken = default)
{
using var response = await httpService.GetAnon(APIs.Mastodon.GET_M_Tag(name), cancellationToken: cancellationToken);
if (response.IsSuccessStatusCode)
return await response.MastodonReadFromJsonAsync(cancellationToken);
await LogFailure(response, nameof(Tag), cancellationToken);
return null;
}
/// Follows or stops following a hashtag; Data is the Tag.
public async Task FollowTag(string name, bool follow, CancellationToken cancellationToken = default)
{
using var response = await httpService.Post(APIs.Mastodon.POST_M_TagAction(name, follow ? "follow" : "unfollow"), cancellationToken: cancellationToken);
if (!response.IsSuccessStatusCode)
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
return new WebResult { Data = await response.MastodonReadFromJsonAsync(cancellationToken) };
}
public Task> FollowedTags(CancellationToken cancellationToken = default) =>
List(APIs.Mastodon.GET_FollowedTags, ["limit=200"], nameof(FollowedTags), cancellationToken);
public Task> TrendingTags(CancellationToken cancellationToken = default) =>
List(APIs.Mastodon.GET_TrendingTags, ["limit=20"], nameof(TrendingTags), cancellationToken);
public Task> TrendingStatuses(CancellationToken cancellationToken = default) =>
List(APIs.Mastodon.GET_TrendingStatuses, ["limit=40"], nameof(TrendingStatuses), cancellationToken);
public Task> TrendingLinks(CancellationToken cancellationToken = default) =>
List(APIs.Mastodon.GET_TrendingLinks, ["limit=20"], nameof(TrendingLinks), cancellationToken);
public const int DirectoryPage = 40;
public Task> Directory(DirectoryOrder order, bool localOnly, int offset, CancellationToken cancellationToken = default) =>
List(APIs.Mastodon.GET_Directory,
[$"order={(order is DirectoryOrder.New ? "new" : "active")}", $"local={(localOnly ? "true" : "false")}", $"limit={DirectoryPage}", $"offset={offset}"],
nameof(Directory), cancellationToken);
async Task> List(string route, string[] parameters, string caller, CancellationToken cancellationToken)
{
using var response = await httpService.GetAnon(route, parameters, cancellationToken: cancellationToken);
if (response.IsSuccessStatusCode)
return await response.MastodonReadFromJsonAsync>(cancellationToken) ?? [];
await LogFailure(response, caller, cancellationToken);
return [];
}
async Task LogFailure(HttpResponseMessage response, string caller, CancellationToken cancellationToken)
{
var result = await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
if (result.StatusCode is not ((int)HttpStatusCode.Gone or (int)HttpStatusCode.NotFound))
await logger.ProcessWarning(result.ToString(localizer), $"{nameof(DiscoveryService)}.{caller}", cancellationToken);
}
}
}