using System.Net; 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, featuring them on the persona's profile), /// what is trending there (hashtags, posts and links, from public posts it already holds), the directory of accounts that /// let themselves be discovered, and who to follow (PrivaPub's suggestions: the staff's picks, past interactions, popular /// accounts here; never the persona itself nor a sibling persona of its login). 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) }; } /// Features a hashtag on the persona's profile, or stops (Mastodon 4.4's tags/:name/feature); its followers are /// told, as featured tags are public. PrivaPub refuses (422) one past its limit. Data is the Tag, its Featuring set. public async Task FeatureTag(string name, bool feature, CancellationToken cancellationToken = default) { using var response = await httpService.Post(APIs.Mastodon.POST_M_TagAction(name, feature ? "feature" : "unfeature"), cancellationToken: cancellationToken); if (!response.IsSuccessStatusCode) return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken); return new WebResult { Data = await response.MastodonReadFromJsonAsync(cancellationToken) }; } /// The hashtags the persona features on its profile, with how often it used them. public async Task> FeaturedTags(CancellationToken cancellationToken = default) { using var response = await httpService.Get(APIs.Mastodon.GET_FeaturedTags, cancellationToken: cancellationToken); if (response.IsSuccessStatusCode) return await response.MastodonReadFromJsonAsync>(cancellationToken) ?? []; await LogFailure(response, nameof(FeaturedTags), cancellationToken); return []; } /// Features a hashtag by its name (a # before it is fine); one featured already is answered as it is. PrivaPub /// refuses (422) a name that is no hashtag and one past its limit. Data is the FeaturedTag. public async Task AddFeaturedTag(string name, CancellationToken cancellationToken = default) { using var response = await httpService.Post(APIs.Mastodon.POST_FeaturedTag, new[] { new KeyValuePair("name", name?.Trim().TrimStart('#')) }, cancellationToken: cancellationToken); if (!response.IsSuccessStatusCode) return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken); return new WebResult { Data = await response.MastodonReadFromJsonAsync(cancellationToken) }; } /// Stops featuring a hashtag, by the featured tag's id; its followers are told. public async Task RemoveFeaturedTag(string featuredTagId, CancellationToken cancellationToken = default) { using var response = await httpService.Delete(APIs.Mastodon.DELETE_M_FeaturedTag(featuredTagId), cancellationToken: cancellationToken); return response.IsSuccessStatusCode ? new WebResult() : await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken); } /// Hashtags the persona used lately and does not feature, the most used first. public async Task> FeaturedTagSuggestions(CancellationToken cancellationToken = default) { using var response = await httpService.Get(APIs.Mastodon.GET_FeaturedTagSuggestions, cancellationToken: cancellationToken); if (response.IsSuccessStatusCode) return await response.MastodonReadFromJsonAsync>(cancellationToken) ?? []; await LogFailure(response, nameof(FeaturedTagSuggestions), cancellationToken); return []; } /// Accounts to follow, best first, each with why (/api/v2/suggestions). public async Task> Suggestions(CancellationToken cancellationToken = default) { using var response = await httpService.Get(APIs.Mastodon.GET_Suggestions, ["limit=40"], cancellationToken: cancellationToken); if (response.IsSuccessStatusCode) return await response.MastodonReadFromJsonAsync>(cancellationToken) ?? []; await LogFailure(response, nameof(Suggestions), cancellationToken); return []; } /// Never suggests the account to this persona again; there is no undoing it, but the account can still be /// followed from its profile. public async Task DismissSuggestion(string accountId, CancellationToken cancellationToken = default) { using var response = await httpService.Delete(APIs.Mastodon.DELETE_M_Suggestion(accountId), cancellationToken: cancellationToken); return response.IsSuccessStatusCode ? new WebResult() : await response.Content.ReadWebResult(response.StatusCode, localizer, 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); } } }