PluralKit/PluralKit.Core/Models/Patch/GroupPatch.cs

89 lines
3.8 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
#nullable enable
using System.Text.RegularExpressions;
2021-09-22 01:42:41 +00:00
using Newtonsoft.Json.Linq;
2020-06-29 21:51:12 +00:00
namespace PluralKit.Core
{
public class GroupPatch: PatchObject
{
public Partial<string> Name { get; set; }
2021-07-08 14:04:05 +00:00
public Partial<string> Hid { get; set; }
public Partial<string?> DisplayName { get; set; }
2020-06-29 21:51:12 +00:00
public Partial<string?> Description { get; set; }
public Partial<string?> Icon { get; set; }
2021-08-02 17:46:12 +00:00
public Partial<string?> BannerImage { get; set; }
2021-03-28 10:02:41 +00:00
public Partial<string?> Color { get; set; }
2021-08-27 15:03:47 +00:00
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> IconPrivacy { get; set; }
public Partial<PrivacyLevel> ListPrivacy { get; set; }
public Partial<PrivacyLevel> Visibility { get; set; }
2020-06-29 21:51:12 +00:00
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("name", Name)
2021-07-08 14:04:05 +00:00
.With("hid", Hid)
.With("display_name", DisplayName)
.With("description", Description)
.With("icon", Icon)
2021-08-02 17:46:12 +00:00
.With("banner_image", BannerImage)
2021-03-28 10:02:41 +00:00
.With("color", Color)
.With("description_privacy", DescriptionPrivacy)
.With("icon_privacy", IconPrivacy)
.With("list_privacy", ListPrivacy)
.With("visibility", Visibility);
public new void AssertIsValid()
{
2021-09-22 17:48:34 +00:00
if (Name.IsPresent)
AssertValid(Name.Value, "name", Limits.MaxGroupNameLength);
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxGroupNameLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (Icon.Value != null)
AssertValid(Icon.Value, "icon", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
}
2021-09-22 01:42:41 +00:00
#nullable disable
public static GroupPatch FromJson(JObject o)
{
var patch = new GroupPatch();
if (o.ContainsKey("name") && o["name"].Type == JTokenType.Null)
throw new ValidationError("Group name can not be set to null.");
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name");
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("icon")) patch.Icon = o.Value<string>("icon").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var privacy = o.Value<JObject>("privacy");
2021-09-22 01:42:41 +00:00
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = privacy.ParsePrivacy("description_privacy");
if (privacy.ContainsKey("icon_privacy"))
patch.IconPrivacy = privacy.ParsePrivacy("icon_privacy");
if (privacy.ContainsKey("list_privacy"))
patch.ListPrivacy = privacy.ParsePrivacy("list_privacy");
if (privacy.ContainsKey("visibility"))
patch.Visibility = privacy.ParsePrivacy("visibility");
}
return patch;
}
2020-06-29 21:51:12 +00:00
}
}