A profile shows the hashtags an account features as chips opening their feed; under another account's header is what the persona keeps of it for itself alone: for one it follows, a bell (follow with notify), the languages of its posts that reach the home feed (follow with languages[]) and Recommend (endorsements, private here), and for any account a private note saved on blur or with Save. Settings edits the persona's featured hashtags (added by name or from those used lately, 10 at most) and its reading preferences (/clientapi/avatar/private/preferences), which CascadingState keeps per persona and the card applies to sensitive media and content warnings. A hashtag's tab features it on the profile. Explore has Who to follow (/api/v2/suggestions, dismissed for good after asking); Administration has Staff picks. The Direct tab follows the direct stream's conversation events; status notifications show the post. The follows CSV carries notify and languages. Verified: Release build, 0 errors (the 3 warnings were there before). Not run against PrivaPub or in the pasture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WpVCoDeM1qUTRp4fXQdfof
157 lines
8.3 KiB
C#
157 lines
8.3 KiB
C#
using System.Net;
|
|
|
|
using decePubClient.Extensions;
|
|
using decePubClient.Models;
|
|
using decePubClient.Models.Mastodon;
|
|
|
|
using PrivaPub.ClientModels;
|
|
|
|
namespace decePubClient.Services
|
|
{
|
|
/// <summary>Which order the directory lists accounts in.</summary>
|
|
public enum DirectoryOrder
|
|
{
|
|
/// <summary>By their latest public post.</summary>
|
|
Active,
|
|
/// <summary>By the day they say they joined.</summary>
|
|
New
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public class DiscoveryService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
|
|
{
|
|
public async Task<Tag> 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<Tag>(cancellationToken);
|
|
|
|
await LogFailure(response, nameof(Tag), cancellationToken);
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Follows or stops following a hashtag; Data is the Tag.</summary>
|
|
public async Task<WebResult> 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<Tag>(cancellationToken) };
|
|
}
|
|
|
|
/// <summary>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.</summary>
|
|
public async Task<WebResult> 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<Tag>(cancellationToken) };
|
|
}
|
|
|
|
/// <summary>The hashtags the persona features on its profile, with how often it used them.</summary>
|
|
public async Task<List<FeaturedTag>> FeaturedTags(CancellationToken cancellationToken = default)
|
|
{
|
|
using var response = await httpService.Get(APIs.Mastodon.GET_FeaturedTags, cancellationToken: cancellationToken);
|
|
if (response.IsSuccessStatusCode)
|
|
return await response.MastodonReadFromJsonAsync<List<FeaturedTag>>(cancellationToken) ?? [];
|
|
|
|
await LogFailure(response, nameof(FeaturedTags), cancellationToken);
|
|
return [];
|
|
}
|
|
|
|
/// <summary>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.</summary>
|
|
public async Task<WebResult> AddFeaturedTag(string name, CancellationToken cancellationToken = default)
|
|
{
|
|
using var response = await httpService.Post(APIs.Mastodon.POST_FeaturedTag,
|
|
new[] { new KeyValuePair<string, string>("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<FeaturedTag>(cancellationToken) };
|
|
}
|
|
|
|
/// <summary>Stops featuring a hashtag, by the featured tag's id; its followers are told.</summary>
|
|
public async Task<WebResult> 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);
|
|
}
|
|
|
|
/// <summary>Hashtags the persona used lately and does not feature, the most used first.</summary>
|
|
public async Task<List<Tag>> FeaturedTagSuggestions(CancellationToken cancellationToken = default)
|
|
{
|
|
using var response = await httpService.Get(APIs.Mastodon.GET_FeaturedTagSuggestions, cancellationToken: cancellationToken);
|
|
if (response.IsSuccessStatusCode)
|
|
return await response.MastodonReadFromJsonAsync<List<Tag>>(cancellationToken) ?? [];
|
|
|
|
await LogFailure(response, nameof(FeaturedTagSuggestions), cancellationToken);
|
|
return [];
|
|
}
|
|
|
|
/// <summary>Accounts to follow, best first, each with why (/api/v2/suggestions).</summary>
|
|
public async Task<List<Suggestion>> 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<List<Suggestion>>(cancellationToken) ?? [];
|
|
|
|
await LogFailure(response, nameof(Suggestions), cancellationToken);
|
|
return [];
|
|
}
|
|
|
|
/// <summary>Never suggests the account to this persona again; there is no undoing it, but the account can still be
|
|
/// followed from its profile.</summary>
|
|
public async Task<WebResult> 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<List<Tag>> FollowedTags(CancellationToken cancellationToken = default) =>
|
|
List<Tag>(APIs.Mastodon.GET_FollowedTags, ["limit=200"], nameof(FollowedTags), cancellationToken);
|
|
|
|
public Task<List<Tag>> TrendingTags(CancellationToken cancellationToken = default) =>
|
|
List<Tag>(APIs.Mastodon.GET_TrendingTags, ["limit=20"], nameof(TrendingTags), cancellationToken);
|
|
|
|
public Task<List<Status>> TrendingStatuses(CancellationToken cancellationToken = default) =>
|
|
List<Status>(APIs.Mastodon.GET_TrendingStatuses, ["limit=40"], nameof(TrendingStatuses), cancellationToken);
|
|
|
|
public Task<List<TrendLink>> TrendingLinks(CancellationToken cancellationToken = default) =>
|
|
List<TrendLink>(APIs.Mastodon.GET_TrendingLinks, ["limit=20"], nameof(TrendingLinks), cancellationToken);
|
|
|
|
public const int DirectoryPage = 40;
|
|
|
|
public Task<List<Account>> Directory(DirectoryOrder order, bool localOnly, int offset, CancellationToken cancellationToken = default) =>
|
|
List<Account>(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<T>> List<T>(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<List<T>>(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);
|
|
}
|
|
}
|
|
}
|