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
178 lines
8.4 KiB
C#
178 lines
8.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using MongoDB.Driver;
|
|
using MongoDB.Entities;
|
|
|
|
using EmptyEntity = PrivaPub.Api.Mastodon.Entities.EmptyEntity;
|
|
using MergedEntity = PrivaPub.Api.Mastodon.Entities.MergedEntity;
|
|
using NotificationPolicyEntity = PrivaPub.Api.Mastodon.Entities.NotificationPolicyEntity;
|
|
using NotificationPolicySummaryEntity = PrivaPub.Api.Mastodon.Entities.NotificationPolicySummaryEntity;
|
|
using NotificationPolicyV1Entity = PrivaPub.Api.Mastodon.Entities.NotificationPolicyV1Entity;
|
|
using NotificationRequestEntity = PrivaPub.Api.Mastodon.Entities.NotificationRequestEntity;
|
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
|
using PrivaPub.Api.Mastodon.Mappers;
|
|
using PrivaPub.Domain.Social;
|
|
using PrivaPub.Models.Social;
|
|
using PrivaPub.StaticServices;
|
|
|
|
namespace PrivaPub.Api.Mastodon.Controllers
|
|
{
|
|
// Mastodon's notification policy and requests (4.3): what the persona's notifications from strangers, new accounts,
|
|
// unasked private mentions and silenced accounts become, and the requests that gather the filtered ones, one per
|
|
// account, let in or dismissed
|
|
public class NotificationRequestsController(
|
|
MastodonMapper mapper,
|
|
DbEntities dbEntities) : MastodonController
|
|
{
|
|
static string Name(NotificationAction action) => action switch
|
|
{
|
|
NotificationAction.Filter => "filter",
|
|
NotificationAction.Drop => "drop",
|
|
_ => "accept"
|
|
};
|
|
|
|
static NotificationAction? Parse(string value) => value?.ToLowerInvariant() switch
|
|
{
|
|
"accept" => NotificationAction.Accept,
|
|
"filter" => NotificationAction.Filter,
|
|
"drop" => NotificationAction.Drop,
|
|
_ => null
|
|
};
|
|
|
|
async Task<object> Summary(CancellationToken token) => new NotificationPolicySummaryEntity
|
|
{
|
|
PendingRequestsCount = await DB.Default.CountAsync<NotificationRequest>(r => r.AvatarId == MyId, token),
|
|
PendingNotificationsCount = await DB.Default.CountAsync<Notification>(n => n.AvatarId == MyId && n.Filtered, token)
|
|
};
|
|
|
|
async Task<object> V2(NotificationPolicy policy, CancellationToken token) => new NotificationPolicyEntity
|
|
{
|
|
ForNotFollowing = Name(policy.ForNotFollowing),
|
|
ForNotFollowers = Name(policy.ForNotFollowers),
|
|
ForNewAccounts = Name(policy.ForNewAccounts),
|
|
ForPrivateMentions = Name(policy.ForPrivateMentions),
|
|
ForLimitedAccounts = Name(policy.ForLimitedAccounts),
|
|
Summary = await Summary(token)
|
|
};
|
|
|
|
// the first version spoke of filtering only: dropped is filtered for it
|
|
async Task<object> V1(NotificationPolicy policy, CancellationToken token) => new NotificationPolicyV1Entity
|
|
{
|
|
FilterNotFollowing = policy.ForNotFollowing != NotificationAction.Accept,
|
|
FilterNotFollowers = policy.ForNotFollowers != NotificationAction.Accept,
|
|
FilterNewAccounts = policy.ForNewAccounts != NotificationAction.Accept,
|
|
FilterPrivateMentions = policy.ForPrivateMentions != NotificationAction.Accept,
|
|
Summary = await Summary(token)
|
|
};
|
|
|
|
[HttpGet(ApiPaths.NotificationRequests.GET_Policy), Scope("read:notifications")]
|
|
public async Task<IActionResult> Policy(CancellationToken token) => Json(await V2(await NotificationPolicies.Of(MyId, token), token));
|
|
|
|
[HttpPatch(ApiPaths.NotificationRequests.PATCH_SetPolicy), HttpPut(ApiPaths.NotificationRequests.PUT_SetPolicy), Scope("write:notifications")]
|
|
public async Task<IActionResult> SetPolicy(CancellationToken token)
|
|
{
|
|
var policy = await NotificationPolicies.Of(MyId, token);
|
|
policy.ForNotFollowing = Parse(Params.Get("for_not_following")) ?? policy.ForNotFollowing;
|
|
policy.ForNotFollowers = Parse(Params.Get("for_not_followers")) ?? policy.ForNotFollowers;
|
|
policy.ForNewAccounts = Parse(Params.Get("for_new_accounts")) ?? policy.ForNewAccounts;
|
|
policy.ForPrivateMentions = Parse(Params.Get("for_private_mentions")) ?? policy.ForPrivateMentions;
|
|
policy.ForLimitedAccounts = Parse(Params.Get("for_limited_accounts")) ?? policy.ForLimitedAccounts;
|
|
await NotificationPolicies.Save(policy, token);
|
|
return Json(await V2(policy, token));
|
|
}
|
|
|
|
[HttpGet(ApiPaths.NotificationRequests.GET_PolicyV1), Scope("read:notifications")]
|
|
public async Task<IActionResult> PolicyV1(CancellationToken token) => Json(await V1(await NotificationPolicies.Of(MyId, token), token));
|
|
|
|
[HttpPut(ApiPaths.NotificationRequests.PUT_SetPolicyV1), HttpPatch(ApiPaths.NotificationRequests.PATCH_SetPolicyV1), Scope("write:notifications")]
|
|
public async Task<IActionResult> SetPolicyV1(CancellationToken token)
|
|
{
|
|
var policy = await NotificationPolicies.Of(MyId, token);
|
|
static NotificationAction Filter(bool? filter, NotificationAction current) =>
|
|
filter switch { true => current == NotificationAction.Drop ? NotificationAction.Drop : NotificationAction.Filter, false => NotificationAction.Accept, _ => current };
|
|
policy.ForNotFollowing = Filter(Params.Bool("filter_not_following"), policy.ForNotFollowing);
|
|
policy.ForNotFollowers = Filter(Params.Bool("filter_not_followers"), policy.ForNotFollowers);
|
|
policy.ForNewAccounts = Filter(Params.Bool("filter_new_accounts"), policy.ForNewAccounts);
|
|
policy.ForPrivateMentions = Filter(Params.Bool("filter_private_mentions"), policy.ForPrivateMentions);
|
|
await NotificationPolicies.Save(policy, token);
|
|
return Json(await V1(policy, token));
|
|
}
|
|
|
|
async Task<object> Map(NotificationRequest request, CancellationToken token)
|
|
{
|
|
var account = await mapper.Account(request.FromAccountId, token);
|
|
if (account == default)
|
|
return default;
|
|
var post = string.IsNullOrEmpty(request.LastPostId) ? default
|
|
: await dbEntities.Posts.Match(p => p.ID == request.LastPostId && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
|
return new NotificationRequestEntity
|
|
{
|
|
Id = request.ID,
|
|
CreatedAt = MastodonJson.Time(request.CreatedAt),
|
|
UpdatedAt = MastodonJson.Time(request.UpdatedAt),
|
|
NotificationsCount = request.NotificationsCount.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
|
Account = account,
|
|
LastStatus = post == default ? default : await mapper.Status(post, MyId, token)
|
|
};
|
|
}
|
|
|
|
[HttpGet(ApiPaths.NotificationRequests.GET_Requests), Scope("read:notifications")]
|
|
public async Task<IActionResult> Requests(CancellationToken token)
|
|
{
|
|
var requests = await Page.From(Params, Limit(40, 80)).Fetch(DB.Default.Find<NotificationRequest>().Match(r => r.AvatarId == MyId), r => r.ID, token);
|
|
Link("/api/v1/notifications/requests", requests.LastOrDefault()?.ID, requests.FirstOrDefault()?.ID);
|
|
var mapped = new List<object>();
|
|
foreach (var request in requests)
|
|
if (await Map(request, token) is { } one)
|
|
mapped.Add(one);
|
|
return Json(mapped);
|
|
}
|
|
|
|
Task<NotificationRequest> Mine(string id, CancellationToken token) =>
|
|
DB.Default.Find<NotificationRequest>().Match(r => r.ID == id && r.AvatarId == MyId).ExecuteFirstAsync(token);
|
|
|
|
[HttpGet(ApiPaths.NotificationRequests.GET_Merged), Scope("read:notifications")]
|
|
public IActionResult Merged() => Json(new MergedEntity { Merged = true });
|
|
|
|
[HttpGet(ApiPaths.NotificationRequests.GET_M_Request), Scope("read:notifications")]
|
|
public async Task<IActionResult> Request(string id, CancellationToken token) =>
|
|
await Mine(id, token) is { } request && await Map(request, token) is { } mapped ? Json(mapped) : NotFoundError();
|
|
|
|
[HttpPost(ApiPaths.NotificationRequests.POST_M_Accept), Scope("write:notifications")]
|
|
public async Task<IActionResult> Accept(string id, CancellationToken token)
|
|
{
|
|
if (await Mine(id, token) is not { } request)
|
|
return NotFoundError();
|
|
await NotificationPolicies.Accept(request, token);
|
|
return Json(new EmptyEntity());
|
|
}
|
|
|
|
[HttpPost(ApiPaths.NotificationRequests.POST_M_Dismiss), Scope("write:notifications")]
|
|
public async Task<IActionResult> Dismiss(string id, CancellationToken token)
|
|
{
|
|
if (await Mine(id, token) is not { } request)
|
|
return NotFoundError();
|
|
await NotificationPolicies.Dismiss(request, token);
|
|
return Json(new EmptyEntity());
|
|
}
|
|
|
|
[HttpPost(ApiPaths.NotificationRequests.POST_AcceptMany), Scope("write:notifications")]
|
|
public async Task<IActionResult> AcceptMany(CancellationToken token)
|
|
{
|
|
foreach (var id in Params.List("id").Take(100))
|
|
if (await Mine(id, token) is { } request)
|
|
await NotificationPolicies.Accept(request, token);
|
|
return Json(new EmptyEntity());
|
|
}
|
|
|
|
[HttpPost(ApiPaths.NotificationRequests.POST_DismissMany), Scope("write:notifications")]
|
|
public async Task<IActionResult> DismissMany(CancellationToken token)
|
|
{
|
|
foreach (var id in Params.List("id").Take(100))
|
|
if (await Mine(id, token) is { } request)
|
|
await NotificationPolicies.Dismiss(request, token);
|
|
return Json(new EmptyEntity());
|
|
}
|
|
}
|
|
}
|