feat: add avatar/bio/links/names/pronouns to user page

This commit is contained in:
sam 2024-09-24 20:56:10 +02:00
parent 412d720abc
commit 862a64840e
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
16 changed files with 650 additions and 90 deletions

View file

@ -50,7 +50,8 @@ public class MembersController(
("bio", ValidationUtils.ValidateBio(req.Bio)),
("avatar", ValidationUtils.ValidateAvatar(req.Avatar)),
..ValidationUtils.ValidateFields(req.Fields, CurrentUser!.CustomPreferences),
..ValidationUtils.ValidateFieldEntries(req.Names?.ToArray(), CurrentUser!.CustomPreferences, "names")
..ValidationUtils.ValidateFieldEntries(req.Names?.ToArray(), CurrentUser!.CustomPreferences, "names"),
..ValidationUtils.ValidatePronouns(req.Pronouns?.ToArray(), CurrentUser!.CustomPreferences)
]);
var member = new Member

View file

@ -62,9 +62,28 @@ public class UsersController(
if (req.HasProperty(nameof(req.Links)))
{
// TODO: validate link length
user.Links = req.Links ?? [];
}
if (req.Names != null)
{
errors.AddRange(ValidationUtils.ValidateFieldEntries(req.Names, CurrentUser!.CustomPreferences, "names"));
user.Names = req.Names.ToList();
}
if (req.Pronouns != null)
{
errors.AddRange(ValidationUtils.ValidatePronouns(req.Pronouns, CurrentUser!.CustomPreferences));
user.Pronouns = req.Pronouns.ToList();
}
if (req.Fields != null)
{
errors.AddRange(ValidationUtils.ValidateFields(req.Fields.ToList(), CurrentUser!.CustomPreferences));
user.Fields = req.Fields.ToList();
}
if (req.HasProperty(nameof(req.Avatar)))
errors.Add(("avatar", ValidationUtils.ValidateAvatar(req.Avatar)));
@ -157,6 +176,9 @@ public class UsersController(
public string? Bio { get; init; }
public string? Avatar { get; init; }
public string[]? Links { get; init; }
public FieldEntry[]? Names { get; init; }
public Pronoun[]? Pronouns { get; init; }
public Field[]? Fields { get; init; }
}

View file

@ -156,7 +156,7 @@ public static class ValidationUtils
break;
}
errors = errors.Concat(ValidateFieldEntries(field.Entries, customPreferences, $"fields.{index}")).ToList();
errors = errors.Concat(ValidateFieldEntries(field.Entries, customPreferences, $"fields.{index}.entries")).ToList();
}
return errors;
@ -169,7 +169,7 @@ public static class ValidationUtils
var errors = new List<(string, ValidationError?)>();
if (entries.Length > Limits.FieldEntriesLimit)
errors.Add(($"{errorPrefix}.entries",
errors.Add((errorPrefix,
ValidationError.LengthError("Field has too many entries", 0, Limits.FieldEntriesLimit,
entries.Length)));
@ -181,12 +181,12 @@ public static class ValidationUtils
switch (entry.Value.Length)
{
case > Limits.FieldEntryTextLimit:
errors.Add(($"{errorPrefix}.entries.{entryIdx}.value",
errors.Add(($"{errorPrefix}.{entryIdx}.value",
ValidationError.LengthError("Field value is too long", 1, Limits.FieldEntryTextLimit,
entry.Value.Length)));
break;
case < 1:
errors.Add(($"{errorPrefix}.entries.{entryIdx}.value",
errors.Add(($"{errorPrefix}.{entryIdx}.value",
ValidationError.LengthError("Field value is too short", 1, Limits.FieldEntryTextLimit,
entry.Value.Length)));
break;
@ -195,7 +195,64 @@ public static class ValidationUtils
var customPreferenceIds = customPreferences?.Keys.Select(id => id.ToString()) ?? [];
if (!DefaultStatusOptions.Contains(entry.Status) && !customPreferenceIds.Contains(entry.Status))
errors.Add(($"{errorPrefix}.entries.{entryIdx}.status",
errors.Add(($"{errorPrefix}.{entryIdx}.status",
ValidationError.GenericValidationError("Invalid status", entry.Status)));
}
return errors;
}
public static IEnumerable<(string, ValidationError?)> ValidatePronouns(Pronoun[]? entries,
IReadOnlyDictionary<Snowflake, User.CustomPreference> customPreferences, string errorPrefix = "pronouns")
{
if (entries == null || entries.Length == 0) return [];
var errors = new List<(string, ValidationError?)>();
if (entries.Length > Limits.FieldEntriesLimit)
errors.Add((errorPrefix,
ValidationError.LengthError("Too many pronouns", 0, Limits.FieldEntriesLimit,
entries.Length)));
// Same as above, no overwhelming this function with a ridiculous amount of entries
if (entries.Length > Limits.FieldEntriesLimit + 50) return errors;
foreach (var (entry, entryIdx) in entries.Select((entry, entryIdx) => (entry, entryIdx)))
{
switch (entry.Value.Length)
{
case > Limits.FieldEntryTextLimit:
errors.Add(($"{errorPrefix}.{entryIdx}.value",
ValidationError.LengthError("Pronoun value is too long", 1, Limits.FieldEntryTextLimit,
entry.Value.Length)));
break;
case < 1:
errors.Add(($"{errorPrefix}.{entryIdx}.value",
ValidationError.LengthError("Pronoun value is too short", 1, Limits.FieldEntryTextLimit,
entry.Value.Length)));
break;
}
if (entry.DisplayText != null)
{
switch (entry.DisplayText.Length)
{
case > Limits.FieldEntryTextLimit:
errors.Add(($"{errorPrefix}.{entryIdx}.value",
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,
entry.Value.Length)));
break;
}
}
var customPreferenceIds = customPreferences?.Keys.Select(id => id.ToString()) ?? [];
if (!DefaultStatusOptions.Contains(entry.Status) && !customPreferenceIds.Contains(entry.Status))
errors.Add(($"{errorPrefix}.{entryIdx}.status",
ValidationError.GenericValidationError("Invalid status", entry.Status)));
}