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
90 lines
3.6 KiB
C#
90 lines
3.6 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
using MongoDB.Entities;
|
|
|
|
using PrivaPub.ClientModels;
|
|
using PrivaPub.ClientModels.Admin;
|
|
using PrivaPub.Domain.Moderation;
|
|
using PrivaPub.Extensions;
|
|
using PrivaPub.Federation.Moderation;
|
|
using PrivaPub.Models;
|
|
using PrivaPub.Models.Federation;
|
|
using PrivaPub.Resources;
|
|
|
|
namespace PrivaPub.Controllers.ClientToServer
|
|
{
|
|
[ApiController,
|
|
Route(ApiPaths.DomainBlock.Prefix),
|
|
Authorize(Policy = Policies.IsAdmin)]
|
|
public class DomainBlockController(
|
|
IDomainBlocks domainBlocks,
|
|
IStringLocalizer<GenericRes> localizer,
|
|
Domain.Media.IMediaProxy mediaProxy,
|
|
IModerationLog log,
|
|
IAccountModeration moderation) : ControllerBase
|
|
{
|
|
[HttpGet, Route(ApiPaths.DomainBlock.GET_List)]
|
|
public async Task<IActionResult> List(CancellationToken token) =>
|
|
Ok((await DB.Default.Find<DomainBlock>().Sort(b => b.Domain, Order.Ascending).ExecuteAsync(token)).Select(ToView).ToList());
|
|
|
|
[HttpPost, Route(ApiPaths.DomainBlock.POST_Insert)]
|
|
public async Task<IActionResult> Insert(DomainBlockForm form, CancellationToken token)
|
|
{
|
|
var domain = DomainBlocks.Normalise(form.Domain);
|
|
if (!ModelState.IsValid || Uri.CheckHostName(domain) != UriHostNameType.Dns)
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
var block = await DB.Default.UpdateAndGet<DomainBlock>()
|
|
.Match(b => b.Domain == domain)
|
|
.Modify(b => b.Domain, domain)
|
|
.Modify(b => b.Severity, form.Suspend ? DomainBlockSeverity.Suspend : DomainBlockSeverity.Silence)
|
|
.Modify(b => b.RejectMedia, form.RejectMedia)
|
|
.Modify(b => b.PublicComment, form.PublicComment)
|
|
.Modify(b => b.PrivateComment, form.PrivateComment)
|
|
.Modify(b => b.Published, form.Published)
|
|
.Modify(b => b.Obfuscate, form.Obfuscate)
|
|
.Modify(b => b.SetOnInsert(x => x.CreatedAt, DateTime.UtcNow))
|
|
.Option(o => o.IsUpsert = true)
|
|
.ExecuteAsync(token);
|
|
await domainBlocks.Reload(token);
|
|
// a silenced server's posts are limited from now on, and no longer once it is not (R6)
|
|
await moderation.DomainChanged(domain, token);
|
|
// what the media proxy holds of it goes with the block
|
|
if (block.Severity == DomainBlockSeverity.Suspend || block.RejectMedia)
|
|
mediaProxy.Purge(domain);
|
|
await log.Write(User.GetUserId(), AdminActions.DomainBlockInsert, AdminTargets.Domain, domain, domain,
|
|
note: string.Join(", ", new[] { block.Severity.ToString().ToLowerInvariant(), block.RejectMedia ? "media rejected" : default,
|
|
block.Published ? (block.Obfuscate ? "published, obfuscated" : "published") : default }.Where(n => n != default)), token: token);
|
|
return Ok(ToView(block));
|
|
}
|
|
|
|
[HttpPost, Route(ApiPaths.DomainBlock.POST_Delete)]
|
|
public async Task<IActionResult> Delete([FromQuery] string domain, CancellationToken token)
|
|
{
|
|
domain = DomainBlocks.Normalise(domain);
|
|
var deleted = await DB.Default.DeleteAsync<DomainBlock>(b => b.Domain == domain);
|
|
await domainBlocks.Reload(token);
|
|
if (deleted.DeletedCount > 0)
|
|
await moderation.DomainChanged(domain, token);
|
|
if (deleted.DeletedCount > 0)
|
|
await log.Write(User.GetUserId(), AdminActions.DomainBlockDelete, AdminTargets.Domain, domain, domain, token: token);
|
|
return Ok();
|
|
}
|
|
|
|
static ViewDomainBlock ToView(DomainBlock block) => new()
|
|
{
|
|
Id = block.ID,
|
|
Domain = block.Domain,
|
|
Severity = block.Severity.ToString(),
|
|
RejectMedia = block.RejectMedia,
|
|
PublicComment = block.PublicComment,
|
|
PrivateComment = block.PrivateComment,
|
|
Published = block.Published,
|
|
Obfuscate = block.Obfuscate,
|
|
CreatedAt = block.CreatedAt
|
|
};
|
|
}
|
|
}
|