Lists: made on /lists, read as a tab of the feed
A persona's lists (PrivaPub's /api/v1/lists): made, renamed, given a replies policy and made exclusive on /lists, filled from the accounts it follows (accounts/search?following=true), and read as the feed's Lists tab. A followed account's profile has a Lists menu to put it in or take it out. ListService does the calls; the e2e tests check each step against PrivaPub's own answer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
011bafb6af
commit
e2491de16e
18 files changed
+914
-9
No files matched your search
@@ -0,0 +1,127 @@
|
||||
using System.Net;
|
||||
|
||||
using collAnon.Client.Services;
|
||||
|
||||
using decePubClient.Extensions;
|
||||
using decePubClient.Models;
|
||||
using decePubClient.Models.Mastodon;
|
||||
|
||||
using PrivaPub.ClientModels;
|
||||
|
||||
namespace decePubClient.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// The current persona's lists (Mastodon's /api/v1/lists): made, renamed and deleted, their members (only accounts the
|
||||
/// persona follows; PrivaPub drops a member when the follow ends), and the lists an account is in. A list's posts are a
|
||||
/// feed of their own (TimelineService, TimelineType.Lists). Lists come back empty on failure; nothing throws.
|
||||
/// </summary>
|
||||
public class ListService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
|
||||
{
|
||||
/// <summary>The most members PrivaPub keeps in one list, and so the most read back.</summary>
|
||||
public const int MaxMembers = 500;
|
||||
const int MembersPerPage = 80;
|
||||
|
||||
public async Task<List<MastodonList>> All(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Get(APIs.Mastodon.GET_Lists, cancellationToken: cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await response.MastodonReadFromJsonAsync<List<MastodonList>>(cancellationToken) ?? [];
|
||||
|
||||
await LogFailure(response, nameof(All), cancellationToken);
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>Makes the list when it has no id yet, or saves its title, replies policy and exclusivity; Data is the list.</summary>
|
||||
public async Task<WebResult> Save(MastodonList list, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var form = new MastodonList { Title = list.Title?.Trim(), RepliesPolicy = list.RepliesPolicy, Exclusive = list.Exclusive };
|
||||
using var response = list.Id is null
|
||||
? await httpService.Post(APIs.Mastodon.POST_List, form, cancellationToken: cancellationToken)
|
||||
: await httpService.Put(APIs.Mastodon.PUT_M_List(list.Id), form, cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult { Data = await response.MastodonReadFromJsonAsync<MastodonList>(cancellationToken) };
|
||||
}
|
||||
|
||||
public async Task<WebResult> Delete(string listId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Delete(APIs.Mastodon.DELETE_M_List(listId), cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult();
|
||||
}
|
||||
|
||||
/// <summary>Every member of the list, page by page, up to MaxMembers.</summary>
|
||||
public async Task<List<Account>> Members(string listId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var members = new List<Account>();
|
||||
var maxId = default(string);
|
||||
while (members.Count < MaxMembers)
|
||||
{
|
||||
string[] parameters = maxId is null ? [$"limit={MembersPerPage}"] : [$"limit={MembersPerPage}", $"max_id={Uri.EscapeDataString(maxId)}"];
|
||||
using var response = await httpService.Get(APIs.Mastodon.M_ListAccounts(listId), parameters, cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
await LogFailure(response, nameof(Members), cancellationToken);
|
||||
return members;
|
||||
}
|
||||
|
||||
var page = await response.MastodonReadFromJsonAsync<List<Account>>(cancellationToken) ?? [];
|
||||
members.AddRange(page);
|
||||
maxId = response.PageCursors().MaxId;
|
||||
if (page.Count < MembersPerPage || maxId is null)
|
||||
return members;
|
||||
}
|
||||
return members;
|
||||
}
|
||||
|
||||
/// <summary>Adds (or removes) an account the persona follows to (from) the list.</summary>
|
||||
public async Task<WebResult> SetMember(string listId, string accountId, bool member, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = member
|
||||
? await httpService.Post(APIs.Mastodon.M_ListAccounts(listId), new ListAccountsForm { AccountIds = [accountId] },
|
||||
cancellationToken: cancellationToken)
|
||||
: await httpService.Delete(APIs.Mastodon.M_ListAccounts(listId), [$"account_ids[]={Uri.EscapeDataString(accountId)}"],
|
||||
cancellationToken: cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||||
|
||||
return new WebResult();
|
||||
}
|
||||
|
||||
/// <summary>The persona's lists the account is in.</summary>
|
||||
public async Task<List<MastodonList>> Of(string accountId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await httpService.Get(APIs.Mastodon.GET_M_AccountLists(accountId), cancellationToken: cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await response.MastodonReadFromJsonAsync<List<MastodonList>>(cancellationToken) ?? [];
|
||||
|
||||
await LogFailure(response, nameof(Of), cancellationToken);
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>Accounts the persona follows whose name, display name or server starts with the query (PrivaPub's
|
||||
/// following=true; nothing is resolved).</summary>
|
||||
public async Task<List<Account>> FindFollowed(string query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
return [];
|
||||
using var response = await httpService.Get(APIs.Mastodon.GET_AccountSearch,
|
||||
[$"q={Uri.EscapeDataString(query.Trim())}", "following=true", "resolve=false", "limit=20"], cancellationToken: cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return await response.MastodonReadFromJsonAsync<List<Account>>(cancellationToken) ?? [];
|
||||
|
||||
await LogFailure(response, nameof(FindFollowed), 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(ListService)}.{caller}", cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,9 @@ using PrivaPub.ClientModels.Post;
|
||||
|
||||
namespace decePubClient.Services
|
||||
{
|
||||
/// <summary>What a feed shows: its kind, and the group, hashtag or position some kinds need.</summary>
|
||||
/// <summary>What a feed shows: its kind, and the group, hashtag, list or position some kinds need.</summary>
|
||||
public sealed record FeedQuery(TimelineType Type, string PersonaId, string GroupId = default, bool IsCommunity = false, string Tag = default,
|
||||
double? Latitude = default, double? Longitude = default);
|
||||
double? Latitude = default, double? Longitude = default, string ListId = default);
|
||||
|
||||
/// <summary>
|
||||
/// Pages of every feed the client shows, as FeedPosts. Mastodon lists page through max_id (older) and min_id (newer)
|
||||
@@ -41,6 +41,8 @@ namespace decePubClient.Services
|
||||
TimelineType.Bookmarks => await Statuses(APIs.Mastodon.GET_Bookmarks, [], maxId, minId, cancellationToken),
|
||||
TimelineType.Tag when query.Tag is { Length: > 0 } tag =>
|
||||
await Statuses(APIs.Mastodon.GET_M_TagTimeline(tag), [], maxId, minId, cancellationToken),
|
||||
TimelineType.Lists when query.ListId is { Length: > 0 } listId =>
|
||||
await Statuses(APIs.Mastodon.GET_M_ListTimeline(listId), [], maxId, minId, cancellationToken),
|
||||
TimelineType.Groups when query.GroupId is { Length: > 0 } groupId && query.IsCommunity =>
|
||||
await Statuses(APIs.Mastodon.GET_M_AccountStatuses(groupId), [], maxId, minId, cancellationToken),
|
||||
TimelineType.Groups when query.GroupId is { Length: > 0 } && maxId is null => await Circle(query, cancellationToken),
|
||||
|
||||
Reference in new issue
Block a user