R8 the Mastodon API made real in decePub: featured tags, notify, languages, recommendations, notes, suggestions, the direct stream
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
This commit is contained in:
1 parent
566b0e33ea
commit
d33697e3fc
28 files changed
+1784
-52
No files matched your search
@@ -29,9 +29,11 @@ namespace decePubClient.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Accounts as the current persona sees them: a profile, its relationship to the persona, its posts, following,
|
||||
/// blocking and muting it, the persona's follow requests, followers and following, and search across the fediverse (a remote handle or URL is
|
||||
/// resolved by PrivaPub, never by the browser). Lists come back empty and values null on failure; nothing throws.
|
||||
/// Accounts as the current persona sees them: a profile, its relationship to the persona, its posts and the hashtags it
|
||||
/// features, following, blocking and muting it, what the persona keeps of it for itself alone (being told of its posts,
|
||||
/// the languages of them it reads, its recommendation, its private note), the persona's follow requests, followers and
|
||||
/// following, and search across the fediverse (a remote handle or URL is resolved by PrivaPub, never by the browser).
|
||||
/// Lists come back empty and values null on failure; nothing throws.
|
||||
/// </summary>
|
||||
public class AccountService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
|
||||
{
|
||||
@@ -93,16 +95,84 @@ namespace decePubClient.Services
|
||||
}
|
||||
|
||||
/// <summary>Follows the account, its boosts shown in the home feed or not; Data is the new Relationship.</summary>
|
||||
public async Task<WebResult> Follow(string accountId, bool showReblogs, CancellationToken cancellationToken = default)
|
||||
public Task<WebResult> Follow(string accountId, bool showReblogs, CancellationToken cancellationToken = default) =>
|
||||
FollowWith(accountId, [new("reblogs", Flag(showReblogs))], cancellationToken);
|
||||
|
||||
/// <summary>Follows the account with everything a follow carries: its boosts shown or not, the persona told of its new
|
||||
/// posts or not, and the languages of them that reach the home feed (none, every language), as a Mastodon follows
|
||||
/// export names them. Data is the new Relationship.</summary>
|
||||
public Task<WebResult> Follow(string accountId, bool showReblogs, bool notify, IReadOnlyCollection<string> languages,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
FollowWith(accountId, [new("reblogs", Flag(showReblogs)), new("notify", Flag(notify)), .. LanguageFields(languages)], cancellationToken);
|
||||
|
||||
/// <summary>Tells the persona of each new post of an account it follows (a `status` notification), or stops. The
|
||||
/// follow's boosts stay as they are: a follow always sets them, so the caller gives the relationship's own. Data is the
|
||||
/// new Relationship.</summary>
|
||||
public Task<WebResult> SetNotify(string accountId, bool showReblogs, bool notify, CancellationToken cancellationToken = default) =>
|
||||
FollowWith(accountId, [new("reblogs", Flag(showReblogs)), new("notify", Flag(notify))], cancellationToken);
|
||||
|
||||
/// <summary>Lets only the posts of an account the persona follows written in these languages reach its home feed
|
||||
/// (a post whose language is unknown still does); none is every language. The follow's boosts stay as they are, as in
|
||||
/// SetNotify. Data is the new Relationship.</summary>
|
||||
public Task<WebResult> SetLanguages(string accountId, bool showReblogs, IReadOnlyCollection<string> languages, CancellationToken cancellationToken = default) =>
|
||||
FollowWith(accountId, [new("reblogs", Flag(showReblogs)), .. LanguageFields(languages)], cancellationToken);
|
||||
|
||||
// an empty value says the list is given, and empty: PrivaPub keeps no language, every language
|
||||
static IEnumerable<KeyValuePair<string, string>> LanguageFields(IReadOnlyCollection<string> languages) => languages.Count == 0
|
||||
? [new("languages[]", string.Empty)]
|
||||
: languages.Select(language => new KeyValuePair<string, string>("languages[]", language));
|
||||
|
||||
async Task<WebResult> FollowWith(string accountId, List<KeyValuePair<string, string>> fields, CancellationToken cancellationToken)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.Mastodon.POST_M_AccountAction(accountId, "follow"),
|
||||
new[] { new KeyValuePair<string, string>("reblogs", showReblogs ? "true" : "false") }, cancellationToken: cancellationToken);
|
||||
using var response = await httpService.Post(APIs.Mastodon.POST_M_AccountAction(accountId, "follow"), fields, cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.MastodonReadFromJsonAsync<Relationship>(cancellationToken) };
|
||||
}
|
||||
|
||||
static string Flag(bool value) => value ? "true" : "false";
|
||||
|
||||
/// <summary>Keeps an account the persona follows in its recommendations (Mastodon's endorsements), or takes it out. They
|
||||
/// are the persona's alone: PrivaPub lists them to nobody else and never federates them; it refuses (422) an account the
|
||||
/// persona doesn't follow. Data is the new Relationship.</summary>
|
||||
public async Task<WebResult> Endorse(string accountId, bool endorse, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.Mastodon.POST_M_AccountAction(accountId, endorse ? "endorse" : "unendorse"),
|
||||
cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.MastodonReadFromJsonAsync<Relationship>(cancellationToken) };
|
||||
}
|
||||
|
||||
/// <summary>The persona's private note on an account: NoteLength characters at most (PrivaPub refuses more with a 422),
|
||||
/// empty takes it away. Shown to the persona alone, never federated. Data is the new Relationship.</summary>
|
||||
public async Task<WebResult> SetNote(string accountId, string comment, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.Mastodon.POST_M_AccountNote(accountId),
|
||||
new[] { new KeyValuePair<string, string>("comment", comment?.Trim() ?? string.Empty) }, cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.MastodonReadFromJsonAsync<Relationship>(cancellationToken) };
|
||||
}
|
||||
|
||||
/// <summary>The longest private note PrivaPub keeps (Mastodon's AccountNote limit).</summary>
|
||||
public const int NoteLength = 2000;
|
||||
|
||||
/// <summary>The hashtags the account features on its profile: a persona's own, or what an account elsewhere's server
|
||||
/// told of its own; none for a community.</summary>
|
||||
public async Task<List<FeaturedTag>> FeaturedTags(string accountId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Get(APIs.Mastodon.GET_M_AccountFeaturedTags(accountId), cancellationToken: cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await response.MastodonReadFromJsonAsync<List<FeaturedTag>>(cancellationToken) ?? [];
|
||||
|
||||
await LogFailure(response, nameof(FeaturedTags), cancellationToken);
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>Follows, unfollows, blocks, unblocks, mutes or unmutes; Data is the new Relationship.</summary>
|
||||
public async Task<WebResult> Act(string accountId, AccountAction action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -121,6 +121,32 @@ namespace decePubClient.Services
|
||||
return new WebResult();
|
||||
}
|
||||
|
||||
/// <summary>How one of the root's personas reads what others post (its reading preferences: media and content
|
||||
/// warnings), or null when they can't be read.</summary>
|
||||
public async Task<ViewPersonaPreferences> PersonaPreferences(string personaId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Get(APIs.ClientApi.GET_PersonaPreferences, [$"avatarId={Uri.EscapeDataString(personaId)}"],
|
||||
cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
await LogFailure(response, nameof(PersonaPreferences), cancellationToken);
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.DefaultReadFromJsonAsync<ViewPersonaPreferences>(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Saves a persona's reading preferences, which Mastodon's API only reads (/api/v1/preferences); Data is the
|
||||
/// ViewPersonaPreferences PrivaPub keeps. A mode it doesn't know is its 422.</summary>
|
||||
public async Task<WebResult> SavePersonaPreferences(PersonaPreferencesForm form, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.ClientApi.POST_PersonaPreferences, form, cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.DefaultReadFromJsonAsync<ViewPersonaPreferences>(cancellationToken) };
|
||||
}
|
||||
|
||||
/// <summary>Acts as the persona from now on, exchanging a token for it unless it has one.</summary>
|
||||
public async Task<WebResult> UsePersona(string personaId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -18,9 +18,11 @@ namespace decePubClient.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// 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)
|
||||
{
|
||||
@@ -44,6 +46,77 @@ namespace decePubClient.Services
|
||||
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);
|
||||
|
||||
|
||||
@@ -150,6 +150,42 @@ namespace decePubClient.Services
|
||||
return await RelayList(response, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>The accounts the staff suggest first to every persona, in their order; one no longer shown (deleted,
|
||||
/// suspended or silenced since) has IsShown false.</summary>
|
||||
public async Task<List<ViewStaffPick>> StaffPicks(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Get(APIs.ClientApi.GET_AdminStaffPicks, cancellationToken: cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await response.DefaultReadFromJsonAsync<List<ViewStaffPick>>(cancellationToken) ?? [];
|
||||
|
||||
await LogFailure(response, nameof(StaffPicks), cancellationToken);
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>Suggests an account to every persona, by its Mastodon id: a persona here or a person elsewhere. PrivaPub
|
||||
/// answers 404 for no such account, 409 for one picked already, 422 past its 40. Data is the picks now.</summary>
|
||||
public async Task<WebResult> AddStaffPick(string accountId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.ClientApi.POST_AdminStaffPickInsert, new StaffPickForm { AccountId = accountId },
|
||||
cancellationToken: cancellationToken);
|
||||
return await StaffPickList(response, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>No longer suggests the account (it can be picked again); Data is the picks now.</summary>
|
||||
public async Task<WebResult> RemoveStaffPick(string accountId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Post(APIs.ClientApi.POST_M_AdminStaffPickDelete(accountId), cancellationToken: cancellationToken);
|
||||
return await StaffPickList(response, cancellationToken);
|
||||
}
|
||||
|
||||
async Task<WebResult> StaffPickList(HttpResponseMessage response, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.DefaultReadFromJsonAsync<List<ViewStaffPick>>(cancellationToken) ?? [] };
|
||||
}
|
||||
|
||||
async Task<WebResult> Saved(HttpResponseMessage response, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace decePubClient.Services
|
||||
[
|
||||
Csv(AccountHeader, "Show boosts", "Notify on new posts", "Languages"),
|
||||
.. followed.Select(a => relationships.GetValueOrDefault(a.Id) is { } r
|
||||
? Csv(Full(a.Acct), Flag(r.ShowingReblogs), Flag(r.Notifying), null)
|
||||
? Csv(Full(a.Acct), Flag(r.ShowingReblogs), Flag(r.Notifying), r.WantedLanguages.Count == 0 ? null : string.Join(", ", r.WantedLanguages))
|
||||
: Csv(Full(a.Acct), "true", "false", null))
|
||||
];
|
||||
}
|
||||
@@ -184,7 +184,15 @@ namespace decePubClient.Services
|
||||
if (await accounts.Lookup(row[0], cancellationToken) is not { } account)
|
||||
return Unknown(row[0]);
|
||||
var showReblogs = row.Count < 2 || !row[1].Trim().Equals("false", StringComparison.OrdinalIgnoreCase);
|
||||
return Failure(row[0], await accounts.Follow(account.Id, showReblogs, cancellationToken));
|
||||
if (row.Count < 3)
|
||||
return Failure(row[0], await accounts.Follow(account.Id, showReblogs, cancellationToken));
|
||||
// Mastodon's own columns, each changing the follow only when the file has it: told of its new posts, and the
|
||||
// languages of them that come home (empty: every one)
|
||||
var notify = row[2].Trim().Equals("true", StringComparison.OrdinalIgnoreCase);
|
||||
if (row.Count < 4)
|
||||
return Failure(row[0], await accounts.SetNotify(account.Id, showReblogs, notify, cancellationToken));
|
||||
var languages = row[3].Split([',', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
return Failure(row[0], await accounts.Follow(account.Id, showReblogs, notify, languages, cancellationToken));
|
||||
}
|
||||
|
||||
async Task<string> AccountRow(string acct, AccountAction action, CancellationToken cancellationToken)
|
||||
|
||||
@@ -12,9 +12,11 @@ namespace decePubClient.Services
|
||||
/// <summary>One stream of PrivaPub's streaming API: its name and, for a hashtag or a list, which one.</summary>
|
||||
public sealed record StreamSubscription(string Stream, string Tag = default, string List = default);
|
||||
|
||||
/// <summary>What a stream told: the stream it came on, the event (update, notification, status.update, delete) and
|
||||
/// what it is about (a Status or a Notification for those, the deleted id for delete).</summary>
|
||||
public sealed record StreamMessage(string Event, Status Status = default, Notification Notification = default, string DeletedId = default);
|
||||
/// <summary>What a stream told: the event (update, notification, status.update, delete, and on the direct stream
|
||||
/// conversation) and what it is about (a Status, a Notification or a Conversation for those, the deleted id for
|
||||
/// delete).</summary>
|
||||
public sealed record StreamMessage(string Event, Status Status = default, Notification Notification = default, string DeletedId = default,
|
||||
Conversation Conversation = default);
|
||||
|
||||
/// <summary>What the user stream told of the administrators' announcements: one new or changed (announcement), one
|
||||
/// gone (announcement.delete, its id), or a reaction's count now (announcement.reaction).</summary>
|
||||
@@ -215,6 +217,8 @@ namespace decePubClient.Services
|
||||
{
|
||||
"update" or "status.update" => new StreamMessage(name, Status: JsonSerializer.Deserialize<Status>(payload, SUtility.MastodonSerializer)),
|
||||
"notification" => new StreamMessage(name, Notification: JsonSerializer.Deserialize<Notification>(payload, SUtility.MastodonSerializer)),
|
||||
// a direct conversation with a new message, as /api/v1/conversations lists it (the direct stream)
|
||||
"conversation" => new StreamMessage(name, Conversation: JsonSerializer.Deserialize<Conversation>(payload, SUtility.MastodonSerializer)),
|
||||
"delete" => new StreamMessage(name, DeletedId: payload),
|
||||
_ => null
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user