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
@@ -13,38 +13,28 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
[ApiController,
|
||||
Route("clientapi/avatar/private"),
|
||||
Authorize(Policy = Policies.IsUser)]
|
||||
public class PrivateAvatarController : ControllerBase
|
||||
public class PrivateAvatarController(
|
||||
IPrivateAvatarUsersService privateAvatarUsersService,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<PrivateAvatarController> logger) : ControllerBase
|
||||
{
|
||||
readonly ILogger<PrivateAvatarController> _logger;
|
||||
readonly IPrivateAvatarUsersService _privateAvatarUsersService;
|
||||
readonly IStringLocalizer _localizer;
|
||||
|
||||
public PrivateAvatarController(IPrivateAvatarUsersService privateAvatarUsersService,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<PrivateAvatarController> logger)
|
||||
{
|
||||
_privateAvatarUsersService = privateAvatarUsersService;
|
||||
_localizer = localizer;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet, Route("/clientapi/avatar/private/list")]
|
||||
public async Task<IActionResult> GetAvatars(CancellationToken token)
|
||||
{
|
||||
var result = await _privateAvatarUsersService.GetRootAvatars(User.GetUserId(), token);
|
||||
var result = await privateAvatarUsersService.GetRootAvatars(User.GetUserId(), token);
|
||||
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
||||
}
|
||||
|
||||
[HttpPost, Route("/clientapi/avatar/private/insert")]
|
||||
public async Task<IActionResult> InsertAvatar(InsertAvatarForm model)
|
||||
public async Task<IActionResult> InsertAvatar(InsertAvatarForm model, CancellationToken token = default)
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(result.Invalidate(_localizer["Invalid model."]));
|
||||
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
||||
try
|
||||
{
|
||||
model.RootId = User.GetUserId();
|
||||
result = await _privateAvatarUsersService.InsertAvatar(model);
|
||||
result = await privateAvatarUsersService.InsertAvatar(model, token);
|
||||
if (!result.IsValid)
|
||||
return StatusCode(result.StatusCode, result);
|
||||
|
||||
@@ -52,8 +42,8 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PrivateAvatarController)}.{nameof(InsertAvatar)}()");
|
||||
return BadRequest(result.Invalidate(_localizer["Something went wrong."]));
|
||||
logger.LogError(ex, $"{nameof(PrivateAvatarController)}.{nameof(InsertAvatar)}()");
|
||||
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,13 +54,13 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
[FromServices] Services.IPersonaRemoval personas, [FromServices] StaticServices.DbEntities dbEntities, CancellationToken token)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new WebResult().Invalidate(_localizer["Invalid model."]));
|
||||
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
||||
var rootId = User.GetUserId();
|
||||
if (!await users.PasswordMatches(rootId, form.Password))
|
||||
return StatusCode(StatusCodes.Status403Forbidden, new WebResult().Invalidate(_localizer[Services.RootUsersService.NoMatch], StatusCodes.Status403Forbidden));
|
||||
if (!await users.PasswordMatches(rootId, form.Password, token))
|
||||
return StatusCode(StatusCodes.Status403Forbidden, new WebResult().Invalidate(localizer[Services.RootUsersService.NoMatch], StatusCodes.Status403Forbidden));
|
||||
if (!await dbEntities.RootToAvatars.Match(ra => ra.RootId == rootId && ra.AvatarId == form.AvatarId).ExecuteAnyAsync(token)
|
||||
|| !await personas.Remove(form.AvatarId, CancellationToken.None))
|
||||
return NotFound(new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound));
|
||||
return NotFound(new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound));
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -80,7 +70,7 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
{
|
||||
var avatar = await OwnPersona(avatarId, dbEntities, token);
|
||||
return avatar == default
|
||||
? NotFound(new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound))
|
||||
? NotFound(new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound))
|
||||
: Ok(PreferencesOf(avatar));
|
||||
}
|
||||
|
||||
@@ -88,12 +78,12 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
public async Task<IActionResult> SavePreferences(PersonaPreferencesForm form, [FromServices] StaticServices.DbEntities dbEntities, CancellationToken token)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new WebResult().Invalidate(_localizer["Invalid model."]));
|
||||
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
||||
if (!Models.User.ExpandMediaModes.IsKnown(form.ExpandMedia))
|
||||
return UnprocessableEntity(new WebResult().Invalidate(_localizer["Media are shown as default, show_all or hide_all."], StatusCodes.Status422UnprocessableEntity));
|
||||
return UnprocessableEntity(new WebResult().Invalidate(localizer["Media are shown as default, show_all or hide_all."], StatusCodes.Status422UnprocessableEntity));
|
||||
var avatar = await OwnPersona(form.AvatarId, dbEntities, token);
|
||||
if (avatar == default)
|
||||
return NotFound(new WebResult().Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound));
|
||||
return NotFound(new WebResult().Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound));
|
||||
await MongoDB.Entities.DB.Default.Update<Models.User.Avatar>().MatchID(avatar.ID)
|
||||
.Modify(a => a.Settings.ExpandMedia, form.ExpandMedia)
|
||||
.Modify(a => a.Settings.ExpandSpoilers, form.ExpandSpoilers)
|
||||
@@ -120,15 +110,15 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
};
|
||||
|
||||
[HttpPost, Route("/clientapi/avatar/private/update")]
|
||||
public async Task<IActionResult> UpdateAvatar(UpdateAvatarForm model)
|
||||
public async Task<IActionResult> UpdateAvatar(UpdateAvatarForm model, CancellationToken token = default)
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(result.Invalidate(_localizer["Invalid model."]));
|
||||
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
||||
try
|
||||
{
|
||||
model.RootId = User.GetUserId();
|
||||
result = await _privateAvatarUsersService.UpdateAvatar(model);
|
||||
result = await privateAvatarUsersService.UpdateAvatar(model, token);
|
||||
if (!result.IsValid)
|
||||
return StatusCode(result.StatusCode, result);
|
||||
|
||||
@@ -136,8 +126,8 @@ namespace PrivaPub.Controllers.ClientToServer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PrivateAvatarController)}.{nameof(UpdateAvatar)}()");
|
||||
return BadRequest(result.Invalidate(_localizer["Something went wrong."]));
|
||||
logger.LogError(ex, $"{nameof(PrivateAvatarController)}.{nameof(UpdateAvatar)}()");
|
||||
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user