2026-10-01 12:13:57 +02:00
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
using MongoDB.Entities ;
using PrivaPub.ClientModels ;
2026-10-05 05:32:32 +02:00
using PrivaPub.ClientModels.Admin ;
2026-10-17 04:02:47 +02:00
using PrivaPub.Domain.Instance ;
using PrivaPub.Domain.Moderation ;
2026-10-01 12:13:57 +02:00
using PrivaPub.Extensions ;
2026-10-17 04:02:47 +02:00
using PrivaPub.Models ;
2026-10-01 12:13:57 +02:00
using PrivaPub.Models.Social ;
2026-10-17 04:02:47 +02:00
using PrivaPub.Models.User ;
2023-02-18 08:52:17 +01:00
2023-02-19 00:43:43 +01:00
namespace PrivaPub.Controllers.ClientToServer
2023-02-18 08:52:17 +01:00
{
[ApiController,
2026-11-10 01:59:20 +01:00
Route(ApiPaths.Moderator.Prefix),
2026-10-01 12:13:57 +02:00
Authorize(Policy = Policies.IsModerator)]
2026-11-10 01:24:43 +01:00
public class ModeratorController (
IInstanceSettings settings ,
IModerationLog moderationLog ,
IAccountModeration accountModeration ,
Federation . Actors . ILocalActorService localActors ,
Microsoft . Extensions . Localization . IStringLocalizer < Resources . GenericRes > localizer ) : ControllerBase
2023-02-18 08:52:17 +01:00
{
2026-10-17 04:02:47 +02:00
const int MaxLogPage = 100 ;
2026-10-17 05:23:45 +02:00
const int MaxAccountsPage = 100 ;
2026-10-17 04:02:47 +02:00
2026-11-10 01:59:20 +01:00
[HttpGet, Route(ApiPaths.Moderator.GET_Reports)]
2026-10-17 04:02:47 +02:00
public async Task < IActionResult > Reports ([ FromQuery ] bool resolved , CancellationToken token )
{
2026-11-10 01:24:43 +01:00
var rules = settings . Current . Rules . ToDictionary ( r => r . Id , r => r . Text ?? string . Empty );
2026-10-17 04:02:47 +02:00
return Ok (( await DB . Default . Find < Report >(). Match ( r => r . IsResolved == resolved ). Sort ( r => r . ID , Order . Descending ). Limit ( 100 ). ExecuteAsync ( token ))
2026-10-05 05:32:32 +02:00
. Select ( r => new ViewReport
2026-10-01 12:13:57 +02:00
{
2026-10-05 05:32:32 +02:00
ID = r . ID ,
2026-10-01 12:13:57 +02:00
Reporter = r . ReporterAvatarId != default ? "local" : r . ReporterActorURI ,
2026-10-05 05:32:32 +02:00
TargetAccountId = r . TargetAccountId ,
TargetActorURI = r . TargetActorURI ,
PostIds = r . PostIds ,
ObjectURIs = r . ObjectURIs ,
Category = r . Category ,
2026-10-17 04:02:47 +02:00
RuleIds = r . RuleIds ?? new (),
Rules = ( r . RuleIds ?? new ()). Select ( id => rules . GetValueOrDefault ( id , string . Empty )). ToList (),
2026-10-05 05:32:32 +02:00
Comment = r . Comment ,
Forwarded = r . Forwarded ,
IsResolved = r . IsResolved ,
2026-10-17 05:23:45 +02:00
Resolution = r . Resolution ,
ResolutionNote = r . ResolutionNote ,
2026-10-05 05:32:32 +02:00
CreatedAt = r . CreatedAt ,
2026-10-07 21:10:58 +02:00
ResolvedAt = r . ResolvedAt ,
RemoteResolvedAt = r . RemoteResolvedAt ,
RemoteResolvedBy = r . RemoteResolvedBy
2026-11-10 07:28:02 +01:00
}). ToList ());
2026-10-17 04:02:47 +02:00
}
2026-10-01 12:13:57 +02:00
2026-10-17 05:23:45 +02:00
// A report resolved, and with it what the staff does about it (R6): nothing more, the reported account silenced or
// suspended, or the reported posts removed. The act comes first: one that fails leaves the report open. The body is
// optional (a report only marked resolved, as before R6).
2026-11-10 01:59:20 +01:00
[HttpPost, Route(ApiPaths.Moderator.POST_M_Resolve)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > Resolve ( string id , [ FromBody ( EmptyBodyBehavior = Microsoft . AspNetCore . Mvc . ModelBinding . EmptyBodyBehavior . Allow )] ResolveReportForm form ,
CancellationToken token )
2026-10-01 12:13:57 +02:00
{
2026-10-17 05:23:45 +02:00
form ??= new ResolveReportForm ();
var action = string . IsNullOrWhiteSpace ( form . Action ) ? ModerationActions . None : form . Action . Trim (). ToLowerInvariant ();
if (! ModerationActions . IsResolution ( action ))
2026-11-10 08:11:11 +01:00
return UnprocessableEntity ( new WebResult (). Invalidate ( 17001. ToCodedError ( localizer [ "Unknown moderation action." ]), StatusCodes . Status422UnprocessableEntity ));
2026-10-17 04:02:47 +02:00
var by = User . GetUserId ();
2026-10-17 05:23:45 +02:00
var report = await DB . Default . Find < Report >(). MatchID ( id ). ExecuteFirstAsync ( token );
if ( report == default )
return NotFound ();
switch ( action )
{
case ModerationActions . Silence or ModerationActions . Suspend :
2026-11-10 01:24:43 +01:00
var moderated = await accountModeration . Moderate ( by , report . TargetAccountId , action , form . Note , form . Warning , report . ID , token );
2026-10-17 05:23:45 +02:00
if (! moderated . IsValid )
return StatusCode ( moderated . StatusCode , moderated );
break ;
case ModerationActions . RemovePosts :
if ( report . PostIds . Count == 0 )
2026-11-10 08:11:11 +01:00
return UnprocessableEntity ( new WebResult (). Invalidate ( 17002. ToCodedError ( localizer [ "The report names no posts." ]), StatusCodes . Status422UnprocessableEntity ));
2026-10-17 05:23:45 +02:00
foreach ( var postId in report . PostIds )
{
2026-11-10 01:24:43 +01:00
var removed = await accountModeration . RemovePost ( by , postId , form . Note , form . Warning , report . ID , token );
2026-10-17 05:23:45 +02:00
if (! removed . IsValid && removed . StatusCode != StatusCodes . Status404NotFound ) //one already gone is removed
return StatusCode ( removed . StatusCode , removed );
}
break ;
}
await DB . Default . Update < Report >(). MatchID ( id )
2026-10-01 12:13:57 +02:00
. Modify ( r => r . IsResolved , true )
. Modify ( r => r . ResolvedAt , DateTime . UtcNow )
2026-10-17 04:02:47 +02:00
. Modify ( r => r . ResolvedBy , by )
2026-10-17 05:23:45 +02:00
. Modify ( r => r . Resolution , action )
. Modify ( r => r . ResolutionNote , string . IsNullOrWhiteSpace ( form . Note ) ? null : form . Note . Trim ())
. ExecuteAsync ( CancellationToken . None );
2026-11-10 01:24:43 +01:00
await moderationLog . Write ( by , AdminActions . ReportResolve , AdminTargets . Report , id , report . Category , reportId : id ,
2026-10-17 05:23:45 +02:00
note : action == ModerationActions . None ? form . Note : string . IsNullOrWhiteSpace ( form . Note ) ? action : $"{action}: {form.Note.Trim()}" ,
token : CancellationToken . None );
2026-10-17 04:02:47 +02:00
return Ok ();
}
2026-10-17 05:23:45 +02:00
// Accounts to moderate, found by a name (a persona's, or user@host for an account elsewhere) or an actor's address:
// personas here, never their logins, and accounts elsewhere PrivaPub holds
2026-11-10 01:59:20 +01:00
[HttpGet, Route(ApiPaths.Moderator.GET_Accounts)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > Accounts ([ FromQuery ] string q , [ FromQuery ] int limit = 20 , CancellationToken token = default )
{
limit = Math . Clamp ( limit , 1 , 40 );
q = q ?. Trim (). TrimStart ( '@' );
var items = new List < ViewModeratedAccount >();
if ( string . IsNullOrEmpty ( q ) || q . Length > 300 )
return Ok ( new ViewModeratedAccountsPage { Items = items });
if ( Uri . TryCreate ( q , UriKind . Absolute , out var address ) && address . Scheme is "https" or "http" )
{
2026-11-10 01:24:43 +01:00
if ( await localActors . FindByUri ( q , token ) is { Kind : Models . Federation . LocalActorKind . Person } local
2026-10-17 05:23:45 +02:00
&& await DB . Default . Find < Avatar >(). MatchID ( local . Id ). ExecuteFirstAsync ( token ) is { } persona )
2026-11-10 01:24:43 +01:00
items . Add ( accountModeration . View ( persona ));
items . AddRange (( await DB . Default . Find < ForeignAvatar >(). Match ( f => f . ActorURI == q || f . Url == q ). Limit ( limit ). ExecuteAsync ( token )). Select ( accountModeration . View ));
2026-10-17 05:23:45 +02:00
return Ok ( new ViewModeratedAccountsPage { Items = items . Take ( limit ). ToList () });
}
var parts = q . Split ( '@' , 2 );
var name = parts [ 0 ];
var domain = parts . Length > 1 ? parts [ 1 ]. ToLowerInvariant () : default ;
2026-11-10 01:24:43 +01:00
var here = new Uri ( localActors . BaseAddress ). Authority ;
2026-10-17 05:23:45 +02:00
if ( domain == default || domain == here )
{
var lowered = name . ToLowerInvariant ();
items . AddRange (( await DB . Default . Find < Avatar >(). Match ( a => a . UserName . StartsWith ( lowered ) && ! a . DeletionAt . HasValue )
2026-11-10 01:24:43 +01:00
. Sort ( a => a . UserName , Order . Ascending ). Limit ( limit ). ExecuteAsync ( token )). Select ( accountModeration . View ));
2026-10-17 05:23:45 +02:00
}
if ( domain != here && items . Count < limit && name . Length > 0 )
{
var userName = new MongoDB . Bson . BsonRegularExpression ( "^" + System . Text . RegularExpressions . Regex . Escape ( name ), "i" );
var remote = DB . Default . Find < ForeignAvatar >(). Match ( f => f . Regex ( a => a . UserName , userName ));
if ( domain != default )
remote . Match ( f => f . Regex ( a => a . Domain , new MongoDB . Bson . BsonRegularExpression ( "^" + System . Text . RegularExpressions . Regex . Escape ( domain ), "i" )));
items . AddRange (( await remote . Match ( f => ! f . DeletionAt . HasValue ). Sort ( f => f . UserName , Order . Ascending ). Limit ( limit - items . Count ). ExecuteAsync ( token ))
2026-11-10 01:24:43 +01:00
. Select ( accountModeration . View ));
2026-10-17 05:23:45 +02:00
}
return Ok ( new ViewModeratedAccountsPage { Items = items });
}
// what the staff silenced or suspended, here and elsewhere, most recently moderated first (a banned login's personas
// are never among them: the ban is the login's, and only administrators see logins)
2026-11-10 01:59:20 +01:00
[HttpGet, Route(ApiPaths.Moderator.GET_Moderated)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > Moderated ([ FromQuery ] int offset = 0 , [ FromQuery ] int limit = 50 , CancellationToken token = default )
{
offset = Math . Max ( 0 , offset );
limit = Math . Clamp ( limit , 1 , MaxAccountsPage );
var locals = await DB . Default . Find < Avatar >(). Match ( a => ( a . SilencedAt != null || a . SuspendedAt != null ) && a . DeletionAt == null ). ExecuteAsync ( token );
var remotes = await DB . Default . Find < ForeignAvatar >(). Match ( f => f . SilencedAt != null || f . SuspendedAt != null ). ExecuteAsync ( token );
2026-11-10 01:24:43 +01:00
var all = locals . Select ( accountModeration . View ). Concat ( remotes . Select ( accountModeration . View ))
2026-10-17 05:23:45 +02:00
. OrderByDescending ( a => Latest ( a . SilencedAt , a . SuspendedAt )). ThenBy ( a => a . Id , StringComparer . Ordinal ). ToList ();
return Ok ( new ViewModeratedAccountsPage
{
Items = all . Skip ( offset ). Take ( limit ). ToList (),
Next = all . Count > offset + limit ? offset + limit : default ( int? )
});
}
static DateTime Latest ( DateTime ? a , DateTime ? b ) => ( a ?? DateTime . MinValue ) > ( b ?? DateTime . MinValue ) ? a ?? DateTime . MinValue : b ?? DateTime . MinValue ;
2026-11-10 01:59:20 +01:00
[HttpGet, Route(ApiPaths.Moderator.GET_M_Account)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > Account ( string id , CancellationToken token ) =>
2026-11-10 01:24:43 +01:00
await accountModeration . View ( id , token ) is { } account ? Ok ( account ) : NotFound ();
2026-10-17 05:23:45 +02:00
// silence, unsilence, suspend or unsuspend a persona here or an account elsewhere
2026-11-10 01:59:20 +01:00
[HttpPost, Route(ApiPaths.Moderator.POST_Moderate)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > Moderate ( ModerateAccountForm form , CancellationToken token )
{
if (! ModelState . IsValid )
2026-11-10 08:11:11 +01:00
return BadRequest ( new WebResult (). Invalidate ( 17003. ToCodedError ( localizer [ "Invalid model." ])));
2026-11-10 01:24:43 +01:00
var result = await accountModeration . Moderate ( User . GetUserId (), form . AccountId , form . Action . Trim (). ToLowerInvariant (), form . Note , form . Warning , default , token );
2026-10-17 05:23:45 +02:00
return result . IsValid ? Ok ( result . Data ) : StatusCode ( result . StatusCode , result );
}
// a post taken down: deleted when it is ours (the deletion federates), hidden here when it is a copy
2026-11-10 01:59:20 +01:00
[HttpPost, Route(ApiPaths.Moderator.POST_RemovePost)]
2026-10-17 05:23:45 +02:00
public async Task < IActionResult > RemovePost ( RemovePostForm form , CancellationToken token )
{
if (! ModelState . IsValid )
2026-11-10 08:11:11 +01:00
return BadRequest ( new WebResult (). Invalidate ( 17004. ToCodedError ( localizer [ "Invalid model." ])));
2026-11-10 01:24:43 +01:00
var result = await accountModeration . RemovePost ( User . GetUserId (), form . PostId , form . Note , form . Warning , default , token );
2026-10-17 05:23:45 +02:00
return result . IsValid ? Ok () : StatusCode ( result . StatusCode , result );
}
2026-10-17 04:02:47 +02:00
// The staff's log, newest first, a page at a time (before: the last id of the page before). Administrators read all
// of it; a moderator who isn't one reads what isn't about a login, and who acted only when it was the command line or
// themselves.
2026-11-10 01:59:20 +01:00
[HttpGet, Route(ApiPaths.Moderator.GET_Log)]
2026-10-17 04:02:47 +02:00
public async Task < IActionResult > Log ([ FromQuery ] string before , [ FromQuery ] int limit = 50 , CancellationToken token = default )
{
var me = await DB . Default . Find < RootUser >(). MatchID ( User . GetUserId ()). ExecuteFirstAsync ( token );
if ( me is not { IsBanned : false , DeletedAt : null } || ! me . Policies . Contains ( Policies . IsModerator ))
return Forbid ();
var admin = me . Policies . Contains ( Policies . IsAdmin );
limit = Math . Clamp ( limit , 1 , MaxLogPage );
var query = DB . Default . Find < AdminAction >();
if (! string . IsNullOrEmpty ( before ))
{
if (! MongoDB . Bson . ObjectId . TryParse ( before , out _ ))
return BadRequest ();
query . Match ( f => f . Lt ( a => a . ID , before ));
}
if (! admin )
query . Match ( a => a . TargetKind != AdminTargets . Root );
var actions = await query . Sort ( a => a . ID , Order . Descending ). Limit ( limit ). ExecuteAsync ( token );
var actorIds = actions . Select ( a => a . ByRootId ). Where ( id => id != default && id != AdminAction . Cli ). Distinct (). ToList ();
var names = admin && actorIds . Count > 0
? ( await DB . Default . Find < RootUser >(). Match ( u => actorIds . Contains ( u . ID )). Project ( u => u . Include ( x => x . UserName )). ExecuteAsync ( token ))
. ToDictionary ( u => u . ID , u => u . UserName )
: new Dictionary < string , string >();
return Ok ( new ViewAdminActionsPage
{
Items = actions . Select ( a => new ViewAdminAction
{
Id = a . ID ,
At = a . At ,
By = a . ByRootId == AdminAction . Cli ? AdminAction . Cli
: a . ByRootId == default ? default
: admin ? names . GetValueOrDefault ( a . ByRootId , a . ByRootId )
: a . ByRootId == me . ID ? me . UserName
: default ,
ByYou = a . ByRootId == me . ID ,
Action = a . Action ,
TargetKind = a . TargetKind ,
TargetId = a . TargetId ,
TargetLabel = a . TargetLabel ,
ReportId = a . ReportId ,
Note = a . Note
}). ToList (),
Next = actions . Count == limit ? actions [^ 1 ]. ID : default
});
2026-10-01 12:13:57 +02:00
}
2023-02-18 08:52:17 +01:00
}
}