Build / Build (push) Successful in 35s
All 384 routes, the Mastodon API, the client API and the frozen federation paths, are read from one class with one nested class per controller. Their values are unchanged, checked one by one against the old literals; decePubClient keeps its own APIs class. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
70 lines
2.9 KiB
C#
70 lines
2.9 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 { });
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|