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
@@ -35,35 +35,22 @@ namespace PrivaPub.Services
|
||||
Task<WebResult> GetDms(string rootUserId, string avatarId, string dmGroupId, CancellationToken token);
|
||||
}
|
||||
|
||||
public class PostsService : IPostsService
|
||||
public class PostsService(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IStatusService statuses,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<PostsService> logger) : IPostsService
|
||||
{
|
||||
const int PageSize = 50;
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IStatusService _statuses;
|
||||
readonly IStringLocalizer<GenericRes> _localizer;
|
||||
readonly ILogger<PostsService> _logger;
|
||||
|
||||
public PostsService(DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IStatusService statuses,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<PostsService> logger)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_statuses = statuses;
|
||||
_localizer = localizer;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<WebResult> InsertPost(string rootUserId, InsertPostForm form, CancellationToken token)
|
||||
{
|
||||
var author = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||
if (author == default)
|
||||
return new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return await Answer(() => _statuses.Publish(author, new StatusDraft
|
||||
return new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return await Answer(() => statuses.Publish(author, new StatusDraft
|
||||
{
|
||||
Text = form.Text,
|
||||
Title = form.Title,
|
||||
@@ -75,15 +62,15 @@ namespace PrivaPub.Services
|
||||
Latitude = form.Latitude,
|
||||
Longitude = form.Longitude,
|
||||
RangeKm = form.RangeKm
|
||||
}, token), nameof(InsertPost));
|
||||
}, token), nameof(InsertPost), token);
|
||||
}
|
||||
|
||||
public async Task<WebResult> DeletePost(string rootUserId, DeletePostForm form, CancellationToken token)
|
||||
{
|
||||
var author = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||
if (author == default)
|
||||
return new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
var result = await Answer(() => _statuses.Remove(author, form.PostId, token), nameof(DeletePost));
|
||||
return new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
var result = await Answer(() => statuses.Remove(author, form.PostId, token), nameof(DeletePost), token);
|
||||
result.Data = default;
|
||||
return result;
|
||||
}
|
||||
@@ -92,14 +79,14 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var author = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||
if (author == default)
|
||||
return new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return await Answer(() => _statuses.Edit(author, form.PostId, new StatusDraft
|
||||
return new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return await Answer(() => statuses.Edit(author, form.PostId, new StatusDraft
|
||||
{
|
||||
Text = form.Text,
|
||||
Title = form.Title,
|
||||
SpoilerText = form.SpoilerText,
|
||||
Sensitive = form.HasContentWarning
|
||||
}, token), nameof(UpdatePost));
|
||||
}, token), nameof(UpdatePost), token);
|
||||
}
|
||||
|
||||
public async Task<WebResult> GetPosts(string rootUserId, string avatarId, string groupId, CancellationToken token)
|
||||
@@ -109,18 +96,18 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var avatar = await OwnedAvatar(rootUserId, avatarId, token);
|
||||
if (avatar == default)
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
var query = _dbEntities.Posts;
|
||||
var query = dbEntities.Posts;
|
||||
if (string.IsNullOrEmpty(groupId))
|
||||
query.Match(p => p.GroupUserId == avatar.Id && p.Visibility != PostVisibility.Direct);
|
||||
else
|
||||
{
|
||||
var isMember = await _dbEntities.Groups
|
||||
var isMember = await dbEntities.Groups
|
||||
.Match(g => g.ID == groupId && g.Members.Any(m => !m.IsForeign && m.AvatarId == avatar.Id))
|
||||
.ExecuteAnyAsync(token);
|
||||
if (!isMember)
|
||||
return result.Invalidate(_localizer["Group not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
|
||||
query.Match(p => p.GroupId == groupId);
|
||||
}
|
||||
|
||||
@@ -130,8 +117,8 @@ namespace PrivaPub.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetPosts)}");
|
||||
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||
logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetPosts)}");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,17 +126,17 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var author = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||
if (author == default)
|
||||
return new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
if (string.IsNullOrEmpty(form.DmGroupId) && form.Recipients.Count == 0)
|
||||
return new WebResult().Invalidate(_localizer["At least one recipient is required."]);
|
||||
return await Answer(() => _statuses.Publish(author, new StatusDraft
|
||||
return new WebResult().Invalidate(localizer["At least one recipient is required."]);
|
||||
return await Answer(() => statuses.Publish(author, new StatusDraft
|
||||
{
|
||||
Text = form.Text,
|
||||
Sensitive = form.HasContentWarning,
|
||||
Visibility = PostVisibility.Direct,
|
||||
ConversationId = form.DmGroupId,
|
||||
Recipients = form.Recipients
|
||||
}, token), nameof(InsertDm));
|
||||
}, token), nameof(InsertDm), token);
|
||||
}
|
||||
|
||||
public async Task<WebResult> GetDms(string rootUserId, string avatarId, string dmGroupId, CancellationToken token)
|
||||
@@ -159,11 +146,11 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var avatar = await OwnedAvatar(rootUserId, avatarId, token);
|
||||
if (avatar == default)
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
if (string.IsNullOrEmpty(dmGroupId))
|
||||
{
|
||||
var groups = await _dbEntities.DmGroups
|
||||
var groups = await dbEntities.DmGroups
|
||||
.Match(g => !g.DeletionAt.HasValue && g.Members.Any(m => !m.IsForeign && m.AvatarId == avatar.Id))
|
||||
.Sort(g => g.UpdatedAt, Order.Descending)
|
||||
.ExecuteAsync(token);
|
||||
@@ -176,13 +163,13 @@ namespace PrivaPub.Services
|
||||
return result;
|
||||
}
|
||||
|
||||
var isMember = await _dbEntities.DmGroups
|
||||
var isMember = await dbEntities.DmGroups
|
||||
.Match(g => g.ID == dmGroupId && g.Members.Any(m => !m.IsForeign && m.AvatarId == avatar.Id))
|
||||
.ExecuteAnyAsync(token);
|
||||
if (!isMember)
|
||||
return result.Invalidate(_localizer["Conversation not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Conversation not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
var dms = await _dbEntities.Posts
|
||||
var dms = await dbEntities.Posts
|
||||
.Match(p => p.ConversationId == dmGroupId && p.Visibility == PostVisibility.Direct)
|
||||
.Match(VisibilityPolicy.IsShown)
|
||||
.Sort(p => p.CreationDate, Order.Descending)
|
||||
@@ -193,8 +180,8 @@ namespace PrivaPub.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetDms)}");
|
||||
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||
logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetDms)}");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,9 +192,9 @@ namespace PrivaPub.Services
|
||||
var result = new WebResult();
|
||||
var viewer = await OwnedAvatar(rootUserId, avatarId, token);
|
||||
if (viewer == default)
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
if (latitude is < -90 or > 90 || longitude is < -180 or > 180)
|
||||
return result.Invalidate(_localizer["Invalid model."]);
|
||||
return result.Invalidate(localizer["Invalid model."]);
|
||||
|
||||
var query = new MongoDB.Bson.BsonDocument
|
||||
{
|
||||
@@ -229,21 +216,21 @@ namespace PrivaPub.Services
|
||||
return result;
|
||||
}
|
||||
|
||||
async Task<WebResult> Answer(Func<Task<StatusOutcome>> action, string operation)
|
||||
async Task<WebResult> Answer(Func<Task<StatusOutcome>> action, string operation, CancellationToken token = default)
|
||||
{
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
var outcome = await action();
|
||||
if (!outcome.Ok)
|
||||
return result.Invalidate(_localizer[outcome.Error], outcome.Status);
|
||||
return result.Invalidate(localizer[outcome.Error], outcome.Status);
|
||||
result.Data = ToView(outcome.Post);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{Service}.{Operation}", nameof(PostsService), operation);
|
||||
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||
logger.LogError(ex, "{Service}.{Operation}", nameof(PostsService), operation);
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,10 +248,10 @@ namespace PrivaPub.Services
|
||||
{
|
||||
if (string.IsNullOrEmpty(rootUserId) || string.IsNullOrEmpty(avatarId))
|
||||
return default;
|
||||
if (!await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token))
|
||||
if (!await dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token))
|
||||
return default;
|
||||
// a suspended persona does nothing through the client API either (R6)
|
||||
return await _localActors.FindById(LocalActorKind.Person, avatarId, token) is { Suspended: false } persona ? persona : default;
|
||||
return await localActors.FindById(LocalActorKind.Person, avatarId, token) is { Suspended: false } persona ? persona : default;
|
||||
}
|
||||
|
||||
static ViewPost ToView(PostEntity post) => new()
|
||||
|
||||
Reference in new issue
Block a user