House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. 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
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -53,57 +53,41 @@ namespace PrivaPub.Domain.Moderation
|
||||
// and nothing is delivered to its own inbox. It is told nothing.
|
||||
//
|
||||
// Every act is kept in the staff's log (ModerationLog), without who acted when the persona is the acting login's own.
|
||||
public class AccountModeration : IAccountModeration
|
||||
public class AccountModeration(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IModerationLog log,
|
||||
IOpenIddictTokenManager openIddictTokenManager,
|
||||
IOpenIddictAuthorizationManager authorizations,
|
||||
IStatusService statuses,
|
||||
IDomainBlocks domainBlocks,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<AccountModeration> logger) : IAccountModeration
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IModerationLog _log;
|
||||
readonly IOpenIddictTokenManager _tokens;
|
||||
readonly IOpenIddictAuthorizationManager _authorizations;
|
||||
readonly IStatusService _statuses;
|
||||
readonly IDomainBlocks _domainBlocks;
|
||||
readonly IStringLocalizer<GenericRes> _localizer;
|
||||
readonly ILogger<AccountModeration> _logger;
|
||||
|
||||
public AccountModeration(DbEntities dbEntities, ILocalActorService localActors, IModerationLog log, IOpenIddictTokenManager tokens,
|
||||
IOpenIddictAuthorizationManager authorizations, IStatusService statuses, IDomainBlocks domainBlocks, IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<AccountModeration> logger)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_log = log;
|
||||
_tokens = tokens;
|
||||
_authorizations = authorizations;
|
||||
_statuses = statuses;
|
||||
_domainBlocks = domainBlocks;
|
||||
_localizer = localizer;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<WebResult> Moderate(string byRootId, string accountId, string action, string note, string warning, string reportId,
|
||||
CancellationToken token)
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!ModerationActions.IsAccountAction(action))
|
||||
return result.Invalidate(_localizer["Unknown moderation action."], StatusCodes.Status422UnprocessableEntity);
|
||||
return result.Invalidate(localizer["Unknown moderation action."], StatusCodes.Status422UnprocessableEntity);
|
||||
try
|
||||
{
|
||||
var avatar = string.IsNullOrEmpty(accountId) ? default : await _dbEntities.Avatars.MatchID(accountId).Match(a => !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
|
||||
var avatar = string.IsNullOrEmpty(accountId) ? default : await dbEntities.Avatars.MatchID(accountId).Match(a => !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
|
||||
if (avatar != default)
|
||||
{
|
||||
result.Data = await Persona(byRootId, avatar, action, note, warning, reportId, token);
|
||||
return result;
|
||||
}
|
||||
var account = string.IsNullOrEmpty(accountId) ? default : await _dbEntities.ForeignAvatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
var account = string.IsNullOrEmpty(accountId) ? default : await dbEntities.ForeignAvatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
if (account == default)
|
||||
return result.Invalidate(_localizer["Account not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Account not found."], StatusCodes.Status404NotFound);
|
||||
result.Data = await Remote(byRootId, account, action, note, reportId, token);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Service}.{Method}({Action})", nameof(AccountModeration), nameof(Moderate), action);
|
||||
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||
logger.LogError(ex, "{Service}.{Method}({Action})", nameof(AccountModeration), nameof(Moderate), action);
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,18 +132,18 @@ namespace PrivaPub.Domain.Moderation
|
||||
.Modify(p => p.AuthorLimited, silenced.HasValue)
|
||||
.ExecuteAsync(CancellationToken.None);
|
||||
await Restrictions.Reload(CancellationToken.None);
|
||||
var persona = _localActors.FromAvatar(avatar);
|
||||
var persona = localActors.FromAvatar(avatar);
|
||||
if (action == ModerationActions.Suspend)
|
||||
{
|
||||
// nothing acts as it from now on: its Mastodon tokens end and its open streams close
|
||||
await _tokens.RevokeBySubjectAsync(avatar.ID, CancellationToken.None);
|
||||
await _authorizations.RevokeBySubjectAsync(avatar.ID, CancellationToken.None);
|
||||
await Social.WebPush.Forget(new[] { avatar.ID });//and its Web Push subscriptions with them (R9): it is pushed nothing
|
||||
await openIddictTokenManager.RevokeBySubjectAsync(avatar.ID, CancellationToken.None);
|
||||
await authorizations.RevokeBySubjectAsync(avatar.ID, CancellationToken.None);
|
||||
await Social.WebPush.Forget(new[] { avatar.ID }, token);//and its Web Push subscriptions with them (R9): it is pushed nothing
|
||||
Streams.CloseFor(avatar.ID);
|
||||
}
|
||||
if (action is ModerationActions.Silence or ModerationActions.Suspend)
|
||||
await Social.Notifications.Warn(avatar.ID, persona.Uri, action, warning, default, token);
|
||||
await _log.Write(byRootId, action switch
|
||||
await log.Write(byRootId, action switch
|
||||
{
|
||||
ModerationActions.Silence => AdminActions.PersonaSilence,
|
||||
ModerationActions.Unsilence => AdminActions.PersonaUnsilence,
|
||||
@@ -212,7 +196,7 @@ namespace PrivaPub.Domain.Moderation
|
||||
.Modify(p => p.AuthorLimited, limited)
|
||||
.ExecuteAsync(CancellationToken.None);
|
||||
await Restrictions.Reload(CancellationToken.None);
|
||||
await _log.Write(byRootId, action switch
|
||||
await log.Write(byRootId, action switch
|
||||
{
|
||||
ModerationActions.Silence => AdminActions.AccountSilence,
|
||||
ModerationActions.Unsilence => AdminActions.AccountUnsilence,
|
||||
@@ -227,22 +211,22 @@ namespace PrivaPub.Domain.Moderation
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
var post = string.IsNullOrEmpty(postId) ? default : await _dbEntities.Posts.MatchID(postId).ExecuteFirstAsync(token);
|
||||
var post = string.IsNullOrEmpty(postId) ? default : await dbEntities.Posts.MatchID(postId).ExecuteFirstAsync(token);
|
||||
if (post == default || post.DeletedAt.HasValue || post.ModeratedAt.HasValue)
|
||||
return result.Invalidate(_localizer["Post not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Post not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
if (!post.IsFederatedCopy)
|
||||
{
|
||||
var author = await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
var author = await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (author == default)
|
||||
return result.Invalidate(_localizer["Post not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Post not found."], StatusCodes.Status404NotFound);
|
||||
// deleted as its author deletes it: emptied, out of every timeline, and a Delete to its audience (but from a
|
||||
// suspended persona, which delivers nothing)
|
||||
var outcome = await _statuses.Remove(author, post.ID, CancellationToken.None);
|
||||
var outcome = await statuses.Remove(author, post.ID, CancellationToken.None);
|
||||
if (!outcome.Ok)
|
||||
return result.Invalidate(_localizer["Post not found."], outcome.Status);
|
||||
return result.Invalidate(localizer["Post not found."], outcome.Status);
|
||||
await Social.Notifications.Warn(author.Id, author.Uri, "delete_statuses", warning, new[] { post.ID }, token);
|
||||
await _log.Write(byRootId, AdminActions.PostRemove, AdminTargets.Post, post.ID, $"@{author.Handle}: {post.ObjectURI}", reportId, note,
|
||||
await log.Write(byRootId, AdminActions.PostRemove, AdminTargets.Post, post.ID, $"@{author.Handle}: {post.ObjectURI}", reportId, note,
|
||||
CancellationToken.None);
|
||||
return result;
|
||||
}
|
||||
@@ -250,13 +234,13 @@ namespace PrivaPub.Domain.Moderation
|
||||
// a copy of a post from elsewhere: kept (its address stays known, so it is never taken in again), hidden here
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.ModeratedAt, DateTime.UtcNow).ExecuteAsync(CancellationToken.None);
|
||||
await Fanout.Deleting(post, CancellationToken.None);
|
||||
await _log.Write(byRootId, AdminActions.PostRemove, AdminTargets.Post, post.ID, post.Url ?? post.ObjectURI, reportId, note, CancellationToken.None);
|
||||
await log.Write(byRootId, AdminActions.PostRemove, AdminTargets.Post, post.ID, post.Url ?? post.ObjectURI, reportId, note, CancellationToken.None);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Service}.{Method}()", nameof(AccountModeration), nameof(RemovePost));
|
||||
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||
logger.LogError(ex, "{Service}.{Method}()", nameof(AccountModeration), nameof(RemovePost));
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,7 +249,7 @@ namespace PrivaPub.Domain.Moderation
|
||||
domain = DomainBlocks.Normalise(domain);
|
||||
if (string.IsNullOrEmpty(domain))
|
||||
return;
|
||||
var silenced = _domainBlocks.Find(domain)?.Severity == DomainBlockSeverity.Silence;
|
||||
var silenced = domainBlocks.Find(domain)?.Severity == DomainBlockSeverity.Silence;
|
||||
// the posts of that server and of its subdomains, by their authors' addresses
|
||||
var pattern = ModerationFlags.OfDomain(domain);
|
||||
await DB.Default.Update<PostEntity>().Match(f => f.Regex(p => p.ActorURI, pattern) & f.Eq(p => p.IsFederatedCopy, true))
|
||||
@@ -283,21 +267,21 @@ namespace PrivaPub.Domain.Moderation
|
||||
}
|
||||
|
||||
bool IsDomainSilenced(string actorUri) =>
|
||||
Uri.TryCreate(actorUri, UriKind.Absolute, out var uri) && _domainBlocks.Find(uri.Host)?.Severity == DomainBlockSeverity.Silence;
|
||||
Uri.TryCreate(actorUri, UriKind.Absolute, out var uri) && domainBlocks.Find(uri.Host)?.Severity == DomainBlockSeverity.Silence;
|
||||
|
||||
public async Task<ViewModeratedAccount> View(string accountId, CancellationToken token)
|
||||
{
|
||||
var avatar = await _dbEntities.Avatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
var avatar = await dbEntities.Avatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
if (avatar != default)
|
||||
return View(avatar);
|
||||
var account = await _dbEntities.ForeignAvatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
var account = await dbEntities.ForeignAvatars.MatchID(accountId).ExecuteFirstAsync(token);
|
||||
return account == default ? default : View(account);
|
||||
}
|
||||
|
||||
// a persona as moderators see it: never its login
|
||||
public ViewModeratedAccount View(Avatar avatar)
|
||||
{
|
||||
var persona = _localActors.FromAvatar(avatar);
|
||||
var persona = localActors.FromAvatar(avatar);
|
||||
return new ViewModeratedAccount
|
||||
{
|
||||
Id = avatar.ID,
|
||||
@@ -315,7 +299,7 @@ namespace PrivaPub.Domain.Moderation
|
||||
|
||||
public ViewModeratedAccount View(ForeignAvatar account)
|
||||
{
|
||||
var block = Uri.TryCreate(account.ActorURI, UriKind.Absolute, out var uri) ? _domainBlocks.Find(uri.Host) : default;
|
||||
var block = Uri.TryCreate(account.ActorURI, UriKind.Absolute, out var uri) ? domainBlocks.Find(uri.Host) : default;
|
||||
return new ViewModeratedAccount
|
||||
{
|
||||
Id = account.ID,
|
||||
|
||||
@@ -17,17 +17,12 @@ namespace PrivaPub.Domain.Moderation
|
||||
// (ModeratorController's /clientapi/moderator/log). An entry never holds an IP address, and never pairs a root with its
|
||||
// own personas: an act on a persona that the acting root owns, on a post of it, a group it owns or a report about it
|
||||
// (R6), is kept without who acted.
|
||||
public class ModerationLog : IModerationLog
|
||||
public class ModerationLog(
|
||||
ILogger<ModerationLog> logger) : IModerationLog
|
||||
{
|
||||
const int MaxLabel = 200;
|
||||
const int MaxNote = 1000;
|
||||
|
||||
readonly ILogger<ModerationLog> _logger;
|
||||
|
||||
public ModerationLog(ILogger<ModerationLog> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Write(string byRootId, string action, string targetKind, string targetId, string targetLabel, string reportId = default,
|
||||
string note = default, CancellationToken token = default)
|
||||
@@ -49,7 +44,7 @@ namespace PrivaPub.Domain.Moderation
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "The staff's log could not keep a {Action}", action);
|
||||
logger.LogWarning(ex, "The staff's log could not keep a {Action}", action);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user