Every failure the server answers with names its own error code
Build / Build (push) Successful in 34s
Deploy / privapub.thepra.dev (push) Successful in 57s
Deploy / Release notes on the Releases tab (push) Successful in 3s

Each refusal and failure the client API sends now starts with a code no other failure uses, such as "[ERR0030006] That
code is not valid.", so a screenshot or a report points at the exact place it came from. Nothing another program reads
changes: ActivityPub, the Mastodon API's errors, sign-in tokens and the error kinds decePubClient branches on stay as they
were. The house-rules checks move to 1.1.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-11-10 08:11:24 +01:00
1 parent f3fdeaf038
commit c2f439d1e9
25 files changed
+268 -224

No files matched your search

@@ -35,13 +35,13 @@ namespace PrivaPub.Services.ClientToServer.Private
{
var userName = form.UserName?.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(userName) || !userName.All(c => char.IsAsciiLetterOrDigit(c) || c == '_'))
return result.Invalidate(localizer["The username may contain only letters, digits and underscores."]);
return result.Invalidate(26001.ToCodedError(localizer["The username may contain only letters, digits and underscores."]));
if (await localActors.IsUserNameTaken(userName, default))
return result.Invalidate(localizer["The username '{0}' is already taken.", userName]);
return result.Invalidate(26002.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
if (!await IsValidRootUser(form.RootId, token))
return result.Invalidate(localizer["You can't do this action because of your account status."]);
return result.Invalidate(26003.ToCodedError(localizer["You can't do this action because of your account status."]));
var (privateKey, publicKey) = Keys.NewKeyPair();
var newAvatar = new Avatar
@@ -59,7 +59,7 @@ namespace PrivaPub.Services.ClientToServer.Private
newAvatar.Emojis = PrivaPub.Domain.Content.LocalEmojis.OfProfile(newAvatar);
newAvatar.ID = (string)newAvatar.GenerateNewID();
if (!await localActors.TryReserveUserName(userName, LocalActorKind.Person, newAvatar.ID, default))
return result.Invalidate(localizer["The username '{0}' is already taken.", userName]);
return result.Invalidate(26004.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
if (form.Settings is { IsDefault: false })
Apply(newAvatar.Settings, form.Settings);
@@ -77,7 +77,7 @@ namespace PrivaPub.Services.ClientToServer.Private
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(InsertAvatar)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(26005.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -87,20 +87,20 @@ namespace PrivaPub.Services.ClientToServer.Private
try
{
if (!await IsValidRootUser(form.RootId, token))
return result.Invalidate(localizer["You can't do this action because of your account status."]);
return result.Invalidate(26006.ToCodedError(localizer["You can't do this action because of your account status."]));
var owns = await dbEntities.RootToAvatars
.Match(ra => ra.RootId == form.RootId && ra.AvatarId == form.AvatarId)
.ExecuteAnyAsync(token);
if (!owns)
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(26007.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var avatar = await dbEntities.Avatars.MatchID(form.AvatarId).ExecuteFirstAsync(token);
if (avatar == default || avatar.DeletionAt.HasValue)
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(26008.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
// a suspended persona's actor stays as it was until the suspension is lifted (R6)
if (avatar.SuspendedAt.HasValue)
return result.Invalidate(localizer["This persona is suspended."], StatusCodes.Status403Forbidden);
return result.Invalidate(26009.ToCodedError(localizer["This persona is suspended."]), StatusCodes.Status403Forbidden);
avatar.Name = form.Name;
avatar.Biography = form.Biography;
@@ -119,7 +119,7 @@ namespace PrivaPub.Services.ClientToServer.Private
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(UpdateAvatar)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(26010.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -140,7 +140,7 @@ namespace PrivaPub.Services.ClientToServer.Private
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(GetRootAvatars)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(26011.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
+30 -30
View File
@@ -45,7 +45,7 @@ namespace PrivaPub.Services
try
{
if (!await OwnsAvatar(rootUserId, avatarId, token))
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27001.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var groups = await dbEntities.Groups
.Match(g => !g.DeletionAt.HasValue && g.Members.Any(m => !m.IsForeign && m.AvatarId == avatarId))
@@ -56,7 +56,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(GetGroups)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27002.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -66,11 +66,11 @@ namespace PrivaPub.Services
try
{
if (!await OwnsAvatar(rootUserId, form.AvatarId, token))
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27003.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var userName = form.UserName.ToLowerInvariant();
if (await localActors.IsUserNameTaken(userName, token))
return result.Invalidate(localizer["The username '{0}' is already taken.", userName]);
return result.Invalidate(27004.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
var (privateKey, publicKey) = Keys.NewKeyPair();
var group = new GroupEntity
@@ -92,7 +92,7 @@ namespace PrivaPub.Services
};
group.ID = (string)group.GenerateNewID();
if (!await localActors.TryReserveUserName(userName, LocalActorKind.Group, group.ID, token))
return result.Invalidate(localizer["The username '{0}' is already taken.", userName]);
return result.Invalidate(27005.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
var actor = localActors.FromGroup(group);
group.Url = actor.Uri;
group.InboxURL = actor.Inbox;
@@ -105,7 +105,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(InsertGroup)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27006.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -116,7 +116,7 @@ namespace PrivaPub.Services
{
var group = await ManagedGroup(rootUserId, form.AvatarId, form.GroupId, token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27007.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
if (form.Name != default)
group.Name = form.Name;
@@ -143,7 +143,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(UpdateGroup)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27008.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -153,13 +153,13 @@ namespace PrivaPub.Services
try
{
if (!await OwnsAvatar(rootUserId, form.AvatarId, token))
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27009.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var group = await dbEntities.Groups
.Match(g => g.InvitationCode == form.InvitationCode && !g.DeletionAt.HasValue)
.ExecuteFirstAsync(token);
if (group == default)
return result.Invalidate(localizer["Invalid invitation."], StatusCodes.Status404NotFound);
return result.Invalidate(27010.ToCodedError(localizer["Invalid invitation."]), StatusCodes.Status404NotFound);
if (!string.IsNullOrEmpty(group.HashedInvitationPassword))
{
@@ -167,7 +167,7 @@ namespace PrivaPub.Services
? (false, false)
: passwordHasher.Check(group.HashedInvitationPassword, form.InvitationPassword);
if (!verified)
return result.Invalidate(localizer["Invalid password."], StatusCodes.Status406NotAcceptable);
return result.Invalidate(27011.ToCodedError(localizer["Invalid password."]), StatusCodes.Status406NotAcceptable);
}
if (!group.Members.Any(m => !m.IsForeign && m.AvatarId == form.AvatarId))
@@ -184,7 +184,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(JoinGroup)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27012.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -221,13 +221,13 @@ namespace PrivaPub.Services
try
{
if (!await OwnsAvatar(rootUserId, form.AvatarId, token))
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27013.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var group = await dbEntities.Groups.MatchID(form.GroupId).ExecuteFirstAsync(token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27014.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
if (group.OwnerAvatarId == form.AvatarId)
return result.Invalidate(localizer["The owner cannot leave the group."]);
return result.Invalidate(27015.ToCodedError(localizer["The owner cannot leave the group."]));
group.Members.RemoveAll(m => !m.IsForeign && m.AvatarId == form.AvatarId);
group.UpdatedAt = DateTime.UtcNow;
@@ -243,7 +243,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(LeaveGroup)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27016.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -254,13 +254,13 @@ namespace PrivaPub.Services
{
var group = await ManagedGroup(rootUserId, form.AvatarId, form.GroupId, token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27017.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
var follower = await dbEntities.Followers
.Match(f => f.LocalActorId == group.ID && f.LocalActorKind == LocalActorKind.Group && f.ActorURI == form.MemberActorURI)
.ExecuteFirstAsync(token);
if (follower == default)
return result.Invalidate(localizer["Request not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27018.ToCodedError(localizer["Request not found."]), StatusCodes.Status404NotFound);
follower.IsAccepted = true;
await DB.Default.SaveAsync(follower, token);
@@ -285,7 +285,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(ApproveMember)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27019.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -296,10 +296,10 @@ namespace PrivaPub.Services
? default
: await dbEntities.Groups.Match(g => g.InvitationCode == invitationCode && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (group == default)
return result.Invalidate(localizer["Invalid invitation."], StatusCodes.Status404NotFound);
return result.Invalidate(27020.ToCodedError(localizer["Invalid invitation."]), StatusCodes.Status404NotFound);
if (!string.IsNullOrEmpty(group.HashedInvitationPassword)
&& (string.IsNullOrEmpty(invitationPassword) || !passwordHasher.Check(group.HashedInvitationPassword, invitationPassword).verified))
return result.Invalidate(localizer["Invalid password."], StatusCodes.Status406NotAcceptable);
return result.Invalidate(27021.ToCodedError(localizer["Invalid password."]), StatusCodes.Status406NotAcceptable);
result.Data = group;
return result;
}
@@ -313,7 +313,7 @@ namespace PrivaPub.Services
{
var group = await ManagedGroup(rootUserId, avatarId, groupId, token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27022.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
var members = new List<ViewGroupMember>();
foreach (var member in group.Members)
@@ -346,7 +346,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(GetMembers)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27023.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -370,12 +370,12 @@ namespace PrivaPub.Services
{
var group = await ManagedGroup(rootUserId, form.AvatarId, form.GroupId, token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27024.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
var request = await dbEntities.Followers
.Match(f => f.LocalActorId == group.ID && f.LocalActorKind == LocalActorKind.Group && f.ActorURI == form.MemberActorURI && !f.IsAccepted)
.ExecuteFirstAsync(token);
if (request == default)
return result.Invalidate(localizer["Request not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27025.ToCodedError(localizer["Request not found."]), StatusCodes.Status404NotFound);
await DB.Default.DeleteAsync<Follower>(request.ID);
await RejectFollow(group, request, token);
return result;
@@ -383,7 +383,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(RejectMember)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27026.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -396,15 +396,15 @@ namespace PrivaPub.Services
{
var group = await ManagedGroup(rootUserId, form.AvatarId, form.GroupId, token);
if (group == default)
return result.Invalidate(localizer["Group not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27027.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
var localMember = await localActors.FindByUri(form.MemberActorURI, token);
var member = group.Members.FirstOrDefault(m => localMember is { Kind: LocalActorKind.Person }
? !m.IsForeign && m.AvatarId == localMember.Id
: m.IsForeign && m.AvatarId == form.MemberActorURI);
if (member == default)
return result.Invalidate(localizer["Member not found."], StatusCodes.Status404NotFound);
return result.Invalidate(27028.ToCodedError(localizer["Member not found."]), StatusCodes.Status404NotFound);
if (member.Role == GroupRole.Owner)
return result.Invalidate(localizer["The owner cannot leave the group."]);
return result.Invalidate(27029.ToCodedError(localizer["The owner cannot leave the group."]));
group.Members.Remove(member);
group.UpdatedAt = DateTime.UtcNow;
@@ -426,7 +426,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GroupUsersService)}.{nameof(RemoveMember)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(27030.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
+2 -2
View File
@@ -53,7 +53,7 @@ namespace PrivaPub.Services
try
{
var localizer = context.HttpContext.RequestServices.GetRequiredService<IStringLocalizer<GenericRes>>();
var webResult = new WebResult().Invalidate(localizer["Unauthorized."], StatusCodes.Status401Unauthorized);
var webResult = new WebResult().Invalidate(28001.ToCodedError(localizer["Unauthorized."]), StatusCodes.Status401Unauthorized);
context.Response.ContentType = contentType;
await context.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(webResult, Web)));
}
@@ -69,7 +69,7 @@ namespace PrivaPub.Services
try
{
var localizer = context.HttpContext.RequestServices.GetRequiredService<IStringLocalizer<GenericRes>>();
var webResult = new WebResult().Invalidate(localizer["Forbidden."], StatusCodes.Status403Forbidden);
var webResult = new WebResult().Invalidate(28002.ToCodedError(localizer["Forbidden."]), StatusCodes.Status403Forbidden);
context.Response.ContentType = contentType;
await context.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(webResult, Web)));
}
+15 -15
View File
@@ -49,7 +49,7 @@ 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(29001.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
return await Answer(() => statuses.Publish(author, new StatusDraft
{
Text = form.Text,
@@ -69,7 +69,7 @@ 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(29002.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var result = await Answer(() => statuses.Remove(author, form.PostId, token), nameof(DeletePost), token);
result.Data = default;
return result;
@@ -79,7 +79,7 @@ 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(29003.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
return await Answer(() => statuses.Edit(author, form.PostId, new StatusDraft
{
Text = form.Text,
@@ -96,7 +96,7 @@ 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(29004.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
var query = dbEntities.Posts;
if (string.IsNullOrEmpty(groupId))
@@ -107,7 +107,7 @@ namespace PrivaPub.Services
.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(29005.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound);
query.Match(p => p.GroupId == groupId);
}
@@ -118,7 +118,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetPosts)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(29006.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -126,9 +126,9 @@ 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(29007.ToCodedError(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 new WebResult().Invalidate(29008.ToCodedError(localizer["At least one recipient is required."]));
return await Answer(() => statuses.Publish(author, new StatusDraft
{
Text = form.Text,
@@ -146,7 +146,7 @@ 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(29009.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
if (string.IsNullOrEmpty(dmGroupId))
{
@@ -167,7 +167,7 @@ namespace PrivaPub.Services
.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(29010.ToCodedError(localizer["Conversation not found."]), StatusCodes.Status404NotFound);
var dms = await dbEntities.Posts
.Match(p => p.ConversationId == dmGroupId && p.Visibility == PostVisibility.Direct)
@@ -181,7 +181,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(PostsService)}.{nameof(GetDms)}");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(29011.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -192,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(29012.ToCodedError(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(29013.ToCodedError(localizer["Invalid model."]));
var query = new MongoDB.Bson.BsonDocument
{
@@ -223,14 +223,14 @@ namespace PrivaPub.Services
{
var outcome = await action();
if (!outcome.Ok)
return result.Invalidate(localizer[outcome.Error], outcome.Status);
return result.Invalidate(29014.ToCodedError(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);
return result.Invalidate(29015.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
+44 -44
View File
@@ -43,7 +43,7 @@ namespace PrivaPub.Services
{
signUpForm.UserName = signUpForm.UserName.ToLower();
if (await dbEntities.RootUsers.Match(u => u.UserName == signUpForm.UserName).ExecuteAnyAsync(token))
return result.Invalidate(localizer["Username '{0}' already taken.", signUpForm.UserName]);
return result.Invalidate(30001.ToCodedError(localizer["Username '{0}' already taken.", signUpForm.UserName]));
var signUpPasswordHashed = passwordHasher.Hash(signUpForm.Password);
var newUser = new RootUser
@@ -74,7 +74,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(SignUpAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30002.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -89,16 +89,16 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.Match(u => u.UserName == loginForm.UserName && u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, needsUpgrade) = passwordHasher.Check(user?.HashedPassword ?? Decoy, loginForm.Password);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer[NoMatch]);
return result.Invalidate(30003.ToCodedError(localizer[NoMatch]));
if (user.IsBanned)
return result.Invalidate(localizer["User '{0}' banned.", user.UserName]);
return result.Invalidate(30004.ToCodedError(localizer["User '{0}' banned.", user.UserName]));
// only a correct password learns that two-factor is on: it is asked for with a 428
if (user.Totp?.EnabledAt != default)
{
if (string.IsNullOrWhiteSpace(loginForm.OneTimeCode))
return result.Invalidate(localizer[TwoFactor.CodeNeeded], StatusCodes.Status428PreconditionRequired);
return result.Invalidate(30005.ToCodedError(localizer[TwoFactor.CodeNeeded]), StatusCodes.Status428PreconditionRequired);
if (!await TwoFactor.Consume(user, loginForm.OneTimeCode, CancellationToken.None))
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
return result.Invalidate(30006.ToCodedError(localizer[TwoFactor.CodeInvalid]));
}
// hashed again at today's iterations; matched on the old hash, so two sign-ins at once never undo each other
@@ -122,7 +122,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(LoginAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30007.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -133,12 +133,12 @@ namespace PrivaPub.Services
{
var isSupportedLanguage = appConfigurationService.AppConfiguration.SupportedLanguages.Contains(userSettings.LanguageCode);
if (!isSupportedLanguage)
return result.Invalidate(localizer["Language code '{0}' unsupported.", userSettings.LanguageCode]);
return result.Invalidate(30008.ToCodedError(localizer["Language code '{0}' unsupported.", userSettings.LanguageCode]));
var languageCodeExists = await dbEntities.Languages
.Match(l => l.International2Code == userSettings.LanguageCode).ExecuteAnyAsync(token);
if (!languageCodeExists)
return result.Invalidate(localizer["Language code '{0}' doesn't exist.", userSettings.LanguageCode]);
return result.Invalidate(30009.ToCodedError(localizer["Language code '{0}' doesn't exist.", userSettings.LanguageCode]));
var language = await dbEntities.Languages.Match(l => l.International2Code == userSettings.LanguageCode)
.ExecuteFirstAsync(token);
@@ -161,7 +161,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UpdateUserSettingsAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30010.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -173,12 +173,12 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.Match(u => u.ID == userId && u.DeletedAt == null).ExecuteFirstAsync(token);
if (user == null)
return result.Invalidate(localizer["Username '{0}' not found.", userId]);
return result.Invalidate(30011.ToCodedError(localizer["Username '{0}' not found.", userId]));
var (verified, needsUpgrade) = passwordHasher.Check(user.HashedPassword, userPasswordForm.OldPassword);
if (!verified)
return result.Invalidate(localizer["Wrong password."]);
return result.Invalidate(30012.ToCodedError(localizer["Wrong password."]));
await DB.Default.Update<RootUser>().MatchID(user.ID)
.Modify(u => u.HashedPassword, passwordHasher.Hash(userPasswordForm.NewPassword))
@@ -193,7 +193,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UpdateUserPasswordAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30013.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -207,13 +207,13 @@ namespace PrivaPub.Services
if (await removal.Remove(id, CancellationToken.None))
removed++;
if (removed == 0)
return result.Invalidate(localizer["User already deleted."]);
return result.Invalidate(30014.ToCodedError(localizer["User already deleted."]));
return result;
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveUserAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30015.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -226,14 +226,14 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer[NoMatch], StatusCodes.Status403Forbidden);
return result.Invalidate(30016.ToCodedError(localizer[NoMatch]), StatusCodes.Status403Forbidden);
await removal.Remove(user.ID, CancellationToken.None);
return result;
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveSelfAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30017.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -260,7 +260,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(BanUserAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30018.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -279,7 +279,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UnbanUserAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30019.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -290,7 +290,7 @@ namespace PrivaPub.Services
{
var user = await dbEntities.RootUsers.MatchID(userId).ExecuteFirstAsync(token);
if (user == default)
return result.Invalidate(localizer["User not found."], StatusCodes.Status404NotFound);
return result.Invalidate(30020.ToCodedError(localizer["User not found."]), StatusCodes.Status404NotFound);
if (loginForm != default && loginForm.ThemeIsDarkMode != user.Settings.ThemeIsDarkMode)
{
@@ -307,7 +307,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(GetUserSettingsAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30021.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -332,7 +332,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(SetupAndSendRecoveryEmail)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30022.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -353,7 +353,7 @@ namespace PrivaPub.Services
}
catch (Exception ex)
{
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30023.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -367,7 +367,7 @@ namespace PrivaPub.Services
var user = recovery == default ? default
: await dbEntities.RootUsers.MatchID(recovery.RootUserId).Match(u => u.DeletedAt == null && !u.IsBanned).ExecuteFirstAsync(token);
if (user == default)
return result.Invalidate(localizer["Invalid recovery code."], StatusCodes.Status404NotFound);
return result.Invalidate(30024.ToCodedError(localizer["Invalid recovery code."]), StatusCodes.Status404NotFound);
await DB.Default.Update<RootUser>().MatchID(user.ID)
.Modify(u => u.HashedPassword, passwordHasher.Hash(newPasswordForm.NewPassword))
@@ -380,7 +380,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(ChangePassword)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30025.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -407,7 +407,7 @@ namespace PrivaPub.Services
{
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
if (user == default)
return result.Invalidate(localizer["User not found."], StatusCodes.Status404NotFound);
return result.Invalidate(30026.ToCodedError(localizer["User not found."]), StatusCodes.Status404NotFound);
var now = DateTime.UtcNow;
var pending = await DB.Default.Find<EmailVerification>().Match(v => v.RootUserId == rootId && v.ExpiresAt > now).ExecuteFirstAsync(token);
result.Data = new ViewAccount
@@ -425,7 +425,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(AccountAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30027.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -439,7 +439,7 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, form.Password ?? string.Empty);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
return result.Invalidate(30028.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
await DB.Default.DeleteAsync<EmailVerification>(v => v.RootUserId == user.ID);
var pending = new EmailVerification
@@ -457,7 +457,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RequestEmailChangeAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30029.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -470,18 +470,18 @@ namespace PrivaPub.Services
var now = DateTime.UtcNow;
var pending = await DB.Default.Find<EmailVerification>().Match(v => v.RootUserId == rootId && v.ExpiresAt > now).ExecuteFirstAsync(token);
if (pending == default || string.IsNullOrEmpty(pending.CodeHash))
return result.Invalidate(localizer[EmailCodeInvalid]);
return result.Invalidate(30030.ToCodedError(localizer[EmailCodeInvalid]));
if (pending.CodeHash != RecoveryJob.Hash(code?.Trim()))
{
if (pending.Attempts + 1 >= EmailVerification.MaxAttempts)
await DB.Default.DeleteAsync<EmailVerification>(pending.ID);
else
await DB.Default.Update<EmailVerification>().MatchID(pending.ID).Modify(b => b.Inc(v => v.Attempts, 1)).ExecuteAsync(token);
return result.Invalidate(localizer[EmailCodeInvalid]);
return result.Invalidate(30031.ToCodedError(localizer[EmailCodeInvalid]));
}
await DB.Default.DeleteAsync<EmailVerification>(pending.ID);
if (await dbEntities.RootUsers.Match(u => u.Email == pending.Email && u.IsEmailValidated && u.ID != rootId && u.DeletedAt == null).ExecuteAnyAsync(token))
return result.Invalidate(localizer[EmailCodeInvalid]);
return result.Invalidate(30032.ToCodedError(localizer[EmailCodeInvalid]));
await DB.Default.Update<RootUser>().MatchID(rootId)
.Modify(u => u.Email, pending.Email)
@@ -493,7 +493,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(VerifyEmailAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30033.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -507,9 +507,9 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
return result.Invalidate(30034.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
if (user.Totp?.EnabledAt != default)
return result.Invalidate(localizer["Two-factor sign-in is on already: turn it off first."], StatusCodes.Status409Conflict);
return result.Invalidate(30035.ToCodedError(localizer["Two-factor sign-in is on already: turn it off first."]), StatusCodes.Status409Conflict);
var secret = TwoFactor.NewSecret();
await DB.Default.Update<RootUser>().MatchID(rootId)
@@ -522,7 +522,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpSetupAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30036.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -533,9 +533,9 @@ namespace PrivaPub.Services
{
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
if (user?.Totp is not { EnabledAt: null, PendingSecret: not null } pending || pending.PendingSince < DateTime.UtcNow.AddHours(-1))
return result.Invalidate(localizer["Set up two-factor sign-in again: that setup expired."], StatusCodes.Status409Conflict);
return result.Invalidate(30037.ToCodedError(localizer["Set up two-factor sign-in again: that setup expired."]), StatusCodes.Status409Conflict);
if (TwoFactor.Step(pending.PendingSecret, code) is not { } step)
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
return result.Invalidate(30038.ToCodedError(localizer[TwoFactor.CodeInvalid]));
var codes = TwoFactor.NewRecoveryCodes();
await DB.Default.Update<RootUser>().MatchID(rootId)
@@ -555,7 +555,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpEnableAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30039.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -567,11 +567,11 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
return result.Invalidate(30040.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
if (user.Totp?.EnabledAt == default)
return await AccountAsync(rootId, token);
if (!await TwoFactor.Consume(user, code, CancellationToken.None))
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
return result.Invalidate(30041.ToCodedError(localizer[TwoFactor.CodeInvalid]));
await DB.Default.Update<RootUser>().MatchID(rootId).Modify(u => u.Totp, null).ExecuteAsync(token);
return await AccountAsync(rootId, token);
@@ -579,7 +579,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpDisableAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30042.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
@@ -591,7 +591,7 @@ namespace PrivaPub.Services
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
if (user?.HashedPassword == default || !verified)
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
return result.Invalidate(30043.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
await DB.Default.Update<RootUser>().MatchID(rootId)
.Modify(u => u.Email, null)
@@ -605,7 +605,7 @@ namespace PrivaPub.Services
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveEmailAsync)}()");
return result.Invalidate(localizer["Something went wrong."], exception: ex);
return result.Invalidate(30044.ToCodedError(localizer["Something went wrong."]), exception: ex);
}
}
}