From f81ae97821980d3273ca603d40773c0553c3dd62 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 25 Sep 2024 19:48:05 +0200 Subject: [PATCH] feat(backend): return unlisted status in partial member for authenticated users --- .../Controllers/InternalController.cs | 3 +- .../Controllers/MembersController.cs | 3 +- .../Controllers/UsersController.cs | 10 ++-- .../Extensions/AvatarObjectExtensions.cs | 6 +- .../Extensions/WebApplicationExtensions.cs | 4 +- Foxnouns.Backend/Foxnouns.Backend.csproj | 56 +++++++++---------- .../Mailables/AccountCreationMailable.cs | 3 +- .../Services/MemberRendererService.cs | 14 +++-- .../Services/UserRendererService.cs | 7 ++- Foxnouns.Backend/Utils/AuthUtils.cs | 2 +- Foxnouns.Backend/Utils/ValidationUtils.cs | 9 ++- .../Views/Mail/_ViewImports.cshtml | 2 +- Foxnouns.Backend/Views/Mail/_ViewStart.cshtml | 2 +- Foxnouns.Frontend/app/lib/utils.ts | 0 .../app/routes/$username/MemberCard.tsx | 0 15 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 Foxnouns.Frontend/app/lib/utils.ts create mode 100644 Foxnouns.Frontend/app/routes/$username/MemberCard.tsx diff --git a/Foxnouns.Backend/Controllers/InternalController.cs b/Foxnouns.Backend/Controllers/InternalController.cs index e63b579..b79de1c 100644 --- a/Foxnouns.Backend/Controllers/InternalController.cs +++ b/Foxnouns.Backend/Controllers/InternalController.cs @@ -60,7 +60,8 @@ public partial class InternalController(DatabaseContext db) : ControllerBase { if (endpoint.RoutePattern.RawText == null) continue; - var templateMatcher = new TemplateMatcher(TemplateParser.Parse(endpoint.RoutePattern.RawText), new RouteValueDictionary()); + var templateMatcher = new TemplateMatcher(TemplateParser.Parse(endpoint.RoutePattern.RawText), + new RouteValueDictionary()); if (!templateMatcher.TryMatch(url, new())) continue; var httpMethodAttribute = endpoint.Metadata.GetMetadata(); if (httpMethodAttribute != null && diff --git a/Foxnouns.Backend/Controllers/MembersController.cs b/Foxnouns.Backend/Controllers/MembersController.cs index 113d95b..1ffc928 100644 --- a/Foxnouns.Backend/Controllers/MembersController.cs +++ b/Foxnouns.Backend/Controllers/MembersController.cs @@ -42,7 +42,8 @@ public class MembersController( [HttpPost("/api/v2/users/@me/members")] [ProducesResponseType(StatusCodes.Status200OK)] [Authorize("member.create")] - public async Task CreateMemberAsync([FromBody] CreateMemberRequest req, CancellationToken ct = default) + public async Task CreateMemberAsync([FromBody] CreateMemberRequest req, + CancellationToken ct = default) { ValidationUtils.Validate([ ("name", ValidationUtils.ValidateMemberName(req.Name)), diff --git a/Foxnouns.Backend/Controllers/UsersController.cs b/Foxnouns.Backend/Controllers/UsersController.cs index 98e4f9c..bb3417c 100644 --- a/Foxnouns.Backend/Controllers/UsersController.cs +++ b/Foxnouns.Backend/Controllers/UsersController.cs @@ -104,7 +104,8 @@ public class UsersController( [HttpPatch("@me/custom-preferences")] [Authorize("user.update")] [ProducesResponseType>(StatusCodes.Status200OK)] - public async Task UpdateCustomPreferencesAsync([FromBody] List req, CancellationToken ct = default) + public async Task UpdateCustomPreferencesAsync([FromBody] List req, + CancellationToken ct = default) { ValidationUtils.Validate(ValidateCustomPreferences(req)); @@ -180,8 +181,8 @@ public class UsersController( public Pronoun[]? Pronouns { get; init; } public Field[]? Fields { get; init; } } - - + + [HttpGet("@me/settings")] [Authorize("user.read_hidden")] [ProducesResponseType(statusCode: StatusCodes.Status200OK)] @@ -194,7 +195,8 @@ public class UsersController( [HttpPatch("@me/settings")] [Authorize("user.read_hidden", "user.update")] [ProducesResponseType(statusCode: StatusCodes.Status200OK)] - public async Task UpdateUserSettingsAsync([FromBody] UpdateUserSettingsRequest req, CancellationToken ct = default) + public async Task UpdateUserSettingsAsync([FromBody] UpdateUserSettingsRequest req, + CancellationToken ct = default) { var user = await db.Users.FirstAsync(u => u.Id == CurrentUser!.Id, ct); diff --git a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs index cb70adf..7c39aa4 100644 --- a/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs +++ b/Foxnouns.Backend/Extensions/AvatarObjectExtensions.cs @@ -14,11 +14,13 @@ public static class AvatarObjectExtensions private static readonly string[] ValidContentTypes = ["image/png", "image/webp", "image/jpeg"]; public static async Task - DeleteMemberAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash, CancellationToken ct = default) => + DeleteMemberAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash, + CancellationToken ct = default) => await objectStorageService.RemoveObjectAsync(MemberAvatarUpdateInvocable.Path(id, hash), ct); public static async Task - DeleteUserAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash, CancellationToken ct = default) => + DeleteUserAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash, + CancellationToken ct = default) => await objectStorageService.RemoveObjectAsync(UserAvatarUpdateInvocable.Path(id, hash), ct); public static async Task ConvertBase64UriToAvatar(this string uri) diff --git a/Foxnouns.Backend/Extensions/WebApplicationExtensions.cs b/Foxnouns.Backend/Extensions/WebApplicationExtensions.cs index 132b4d0..a5b2af6 100644 --- a/Foxnouns.Backend/Extensions/WebApplicationExtensions.cs +++ b/Foxnouns.Backend/Extensions/WebApplicationExtensions.cs @@ -100,11 +100,11 @@ public static class WebApplicationExtensions // Transient jobs .AddTransient() .AddTransient(); - + if (!config.Logging.EnableMetrics) services.AddHostedService(); }); - + return builder.Services; } diff --git a/Foxnouns.Backend/Foxnouns.Backend.csproj b/Foxnouns.Backend/Foxnouns.Backend.csproj index 987ebbd..a9e7b74 100644 --- a/Foxnouns.Backend/Foxnouns.Backend.csproj +++ b/Foxnouns.Backend/Foxnouns.Backend.csproj @@ -8,34 +8,34 @@ - - - - - - - - + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -44,12 +44,12 @@ - + - - .dockerignore - + + .dockerignore + diff --git a/Foxnouns.Backend/Mailables/AccountCreationMailable.cs b/Foxnouns.Backend/Mailables/AccountCreationMailable.cs index 3fc1ff4..cec17cf 100644 --- a/Foxnouns.Backend/Mailables/AccountCreationMailable.cs +++ b/Foxnouns.Backend/Mailables/AccountCreationMailable.cs @@ -2,7 +2,8 @@ using Coravel.Mailer.Mail; namespace Foxnouns.Backend.Mailables; -public class AccountCreationMailable(Config config, AccountCreationMailableView view) : Mailable +public class AccountCreationMailable(Config config, AccountCreationMailableView view) + : Mailable { public override void Build() { diff --git a/Foxnouns.Backend/Services/MemberRendererService.cs b/Foxnouns.Backend/Services/MemberRendererService.cs index 962712f..ef7b923 100644 --- a/Foxnouns.Backend/Services/MemberRendererService.cs +++ b/Foxnouns.Backend/Services/MemberRendererService.cs @@ -11,6 +11,7 @@ public class MemberRendererService(DatabaseContext db, Config config) public async Task> RenderUserMembersAsync(User user, Token? token) { var canReadHiddenMembers = token != null && token.UserId == user.Id && token.HasScope("member.read"); + var renderUnlisted = token != null && token.UserId == user.Id && token.HasScope("user.read_hidden"); var canReadMemberList = !user.ListHidden || canReadHiddenMembers; IEnumerable members = canReadMemberList @@ -20,7 +21,7 @@ public class MemberRendererService(DatabaseContext db, Config config) .ToListAsync() : []; if (!canReadHiddenMembers) members = members.Where(m => !m.Unlisted); - return members.Select(RenderPartialMember); + return members.Select(m => RenderPartialMember(m, renderUnlisted)); } public MemberResponse RenderMember(Member member, Token? token) @@ -34,10 +35,11 @@ public class MemberRendererService(DatabaseContext db, Config config) } private UserRendererService.PartialUser RenderPartialUser(User user) => - new(user.Id, user.Username, user.DisplayName, AvatarUrlFor(user)); + new(user.Id, user.Username, user.DisplayName, AvatarUrlFor(user), user.CustomPreferences); - public PartialMember RenderPartialMember(Member member) => new(member.Id, member.Name, - member.DisplayName, member.Bio, AvatarUrlFor(member), member.Names, member.Pronouns); + public PartialMember RenderPartialMember(Member member, bool renderUnlisted = false) => new(member.Id, member.Name, + member.DisplayName, member.Bio, AvatarUrlFor(member), member.Names, member.Pronouns, + renderUnlisted ? member.Unlisted : null); private string? AvatarUrlFor(Member member) => member.Avatar != null ? $"{config.MediaBaseUrl}/members/{member.Id}/avatars/{member.Avatar}.webp" : null; @@ -52,7 +54,9 @@ public class MemberRendererService(DatabaseContext db, Config config) string? Bio, string? AvatarUrl, IEnumerable Names, - IEnumerable Pronouns); + IEnumerable Pronouns, + [property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + bool? Unlisted); public record MemberResponse( Snowflake Id, diff --git a/Foxnouns.Backend/Services/UserRendererService.cs b/Foxnouns.Backend/Services/UserRendererService.cs index 07bdb8b..8251611 100644 --- a/Foxnouns.Backend/Services/UserRendererService.cs +++ b/Foxnouns.Backend/Services/UserRendererService.cs @@ -39,7 +39,7 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe return new UserResponse( user.Id, user.Username, user.DisplayName, user.Bio, user.MemberTitle, AvatarUrlFor(user), user.Links, user.Names, user.Pronouns, user.Fields, user.CustomPreferences, - renderMembers ? members.Select(memberRenderer.RenderPartialMember) : null, + renderMembers ? members.Select(m => memberRenderer.RenderPartialMember(m, tokenHidden)) : null, renderAuthMethods ? authMethods.Select(a => new AuthenticationMethodResponse( a.Id, a.AuthType, a.RemoteId, @@ -52,7 +52,7 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe } public PartialUser RenderPartialUser(User user) => - new(user.Id, user.Username, user.DisplayName, AvatarUrlFor(user)); + new(user.Id, user.Username, user.DisplayName, AvatarUrlFor(user), user.CustomPreferences); private string? AvatarUrlFor(User user) => user.Avatar != null ? $"{config.MediaBaseUrl}/users/{user.Id}/avatars/{user.Avatar}.webp" : null; @@ -94,6 +94,7 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe Snowflake Id, string Username, string? DisplayName, - string? AvatarUrl + string? AvatarUrl, + Dictionary CustomPreferences ); } \ No newline at end of file diff --git a/Foxnouns.Backend/Utils/AuthUtils.cs b/Foxnouns.Backend/Utils/AuthUtils.cs index 26965e2..c767198 100644 --- a/Foxnouns.Backend/Utils/AuthUtils.cs +++ b/Foxnouns.Backend/Utils/AuthUtils.cs @@ -79,7 +79,7 @@ public static class AuthUtils return false; } } - + public static bool TryParseToken(string? input, out byte[] rawToken) { rawToken = []; diff --git a/Foxnouns.Backend/Utils/ValidationUtils.cs b/Foxnouns.Backend/Utils/ValidationUtils.cs index 9020c0f..f29dd24 100644 --- a/Foxnouns.Backend/Utils/ValidationUtils.cs +++ b/Foxnouns.Backend/Utils/ValidationUtils.cs @@ -156,7 +156,8 @@ public static class ValidationUtils break; } - errors = errors.Concat(ValidateFieldEntries(field.Entries, customPreferences, $"fields.{index}.entries")).ToList(); + errors = errors.Concat(ValidateFieldEntries(field.Entries, customPreferences, $"fields.{index}.entries")) + .ToList(); } return errors; @@ -238,12 +239,14 @@ public static class ValidationUtils { case > Limits.FieldEntryTextLimit: errors.Add(($"{errorPrefix}.{entryIdx}.value", - ValidationError.LengthError("Pronoun display text is too long", 1, Limits.FieldEntryTextLimit, + ValidationError.LengthError("Pronoun display text is too long", 1, + Limits.FieldEntryTextLimit, entry.Value.Length))); break; case < 1: errors.Add(($"{errorPrefix}.{entryIdx}.value", - ValidationError.LengthError("Pronoun display text is too short", 1, Limits.FieldEntryTextLimit, + ValidationError.LengthError("Pronoun display text is too short", 1, + Limits.FieldEntryTextLimit, entry.Value.Length))); break; } diff --git a/Foxnouns.Backend/Views/Mail/_ViewImports.cshtml b/Foxnouns.Backend/Views/Mail/_ViewImports.cshtml index 6ececef..f13b1c3 100644 --- a/Foxnouns.Backend/Views/Mail/_ViewImports.cshtml +++ b/Foxnouns.Backend/Views/Mail/_ViewImports.cshtml @@ -1,2 +1,2 @@ @using Foxnouns.Backend -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers \ No newline at end of file diff --git a/Foxnouns.Backend/Views/Mail/_ViewStart.cshtml b/Foxnouns.Backend/Views/Mail/_ViewStart.cshtml index b74bab7..4080127 100644 --- a/Foxnouns.Backend/Views/Mail/_ViewStart.cshtml +++ b/Foxnouns.Backend/Views/Mail/_ViewStart.cshtml @@ -1,3 +1,3 @@ @{ Layout = "~/Views/Mail/Layout.cshtml"; -} +} \ No newline at end of file diff --git a/Foxnouns.Frontend/app/lib/utils.ts b/Foxnouns.Frontend/app/lib/utils.ts new file mode 100644 index 0000000..e69de29 diff --git a/Foxnouns.Frontend/app/routes/$username/MemberCard.tsx b/Foxnouns.Frontend/app/routes/$username/MemberCard.tsx new file mode 100644 index 0000000..e69de29