Build / Build (push) Successful in 36s
Every answer of the Mastodon API and the client API now has its JSON shape generated when the server is built, instead of worked out by reflection while it runs; the bytes sent are unchanged. The small answers that were anonymous objects (the instance documents, notification policies, reports, lists, tags, trends, errors and the empty answers) are named types. A type without generated metadata fails at once in development and on the pasture, and in production is logged and still answered. Three lists that were sent as lazy sequences (moderation reports, the published domain blocks and the languages) are now built first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
70 lines
3.0 KiB
C#
70 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using MongoDB.Driver;
|
|
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Api.Mastodon.Entities;
|
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
|
using PrivaPub.Api.Mastodon.Mappers;
|
|
using PrivaPub.Domain.Instance;
|
|
using PrivaPub.Domain.Relationships;
|
|
using PrivaPub.Domain.Social;
|
|
using PrivaPub.Models.Social;
|
|
|
|
namespace PrivaPub.Api.Mastodon.Controllers
|
|
{
|
|
// Follow suggestions as Mastodon's API has them (completion program R8; who and why in Domain/Social/Suggestions): v1 the
|
|
// accounts, v2 each with its reasons; a dismissed account is never suggested to that persona again.
|
|
public class SuggestionsController(
|
|
MastodonMapper mapper,
|
|
IInstanceSettings settings) : MastodonController
|
|
{
|
|
[HttpGet(ApiPaths.Suggestions.GET_V1), Scope("read")]
|
|
public async Task<IActionResult> V1(CancellationToken token) => Json((await Suggested(token)).Select(s => s.Account).ToList());
|
|
|
|
[HttpGet(ApiPaths.Suggestions.GET_V2), Scope("read")]
|
|
public async Task<IActionResult> V2(CancellationToken token) => Json(await Suggested(token));
|
|
|
|
[HttpDelete(ApiPaths.Suggestions.DELETE_M_Dismiss), Scope("read")]
|
|
public async Task<IActionResult> Dismiss(string accountId, CancellationToken token)
|
|
{
|
|
if (!string.IsNullOrEmpty(accountId) && accountId.Length <= 64)
|
|
try
|
|
{
|
|
await DB.Default.Update<SuggestionDismissal>().Match(d => d.AvatarId == MyId && d.AccountId == accountId)
|
|
.Modify(b => b.SetOnInsert(d => d.CreatedAt, DateTime.UtcNow)).Option(o => o.IsUpsert = true).ExecuteAsync(token);
|
|
}
|
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
|
{
|
|
}
|
|
return Json(new EmptyEntity());
|
|
}
|
|
|
|
// the candidates mapped a few at a time, until the page is full: an account no longer shown (deleted, suspended), one
|
|
// shown limited (silenced, or its server), a community outside the staff's picks, and one the persona hides (its
|
|
// blocked servers) are passed over
|
|
async Task<List<Suggestion>> Suggested(CancellationToken token)
|
|
{
|
|
var limit = Limit(40, 80);
|
|
var candidates = await Suggestions.For(Me, settings.Current.StaffPicks, token);
|
|
var suggested = new List<Suggestion>();
|
|
for (var start = 0; start < candidates.Count && suggested.Count < limit; start += limit * 2)
|
|
{
|
|
var chunk = candidates.Skip(start).Take(limit * 2).ToList();
|
|
var accounts = await mapper.Accounts(chunk.Select(c => c.AccountId), token);
|
|
var hidden = await Hidden.AuthorsHiddenFrom(MyId, accounts.Values.Select(a => a.Uri), forNotifications: false, token);
|
|
foreach (var candidate in chunk)
|
|
{
|
|
if (accounts.GetValueOrDefault(candidate.AccountId) is not { } account || account.Limited == true || hidden.Contains(account.Uri)
|
|
|| account.Group && candidate.Source != Suggestions.Staff)
|
|
continue;
|
|
suggested.Add(new Suggestion { Source = candidate.Source, Sources = candidate.Sources, Account = account });
|
|
if (suggested.Count >= limit)
|
|
break;
|
|
}
|
|
}
|
|
return suggested;
|
|
}
|
|
}
|
|
}
|