2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-08-07 22:13:46 +00:00
|
|
|
using System.Linq;
|
2021-04-21 21:57:19 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2020-06-29 11:57:48 +00:00
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2020-06-29 11:57:48 +00:00
|
|
|
namespace PluralKit.Core
|
|
|
|
{
|
2020-06-29 12:39:19 +00:00
|
|
|
public class MemberPatch: PatchObject
|
2020-06-29 11:57:48 +00:00
|
|
|
{
|
|
|
|
public Partial<string> Name { get; set; }
|
2021-06-08 17:37:44 +00:00
|
|
|
public Partial<string> Hid { get; set; }
|
2020-06-29 11:57:48 +00:00
|
|
|
public Partial<string?> DisplayName { get; set; }
|
2020-06-29 12:15:30 +00:00
|
|
|
public Partial<string?> AvatarUrl { get; set; }
|
2021-08-02 17:46:12 +00:00
|
|
|
public Partial<string?> BannerImage { get; set; }
|
2020-06-29 11:57:48 +00:00
|
|
|
public Partial<string?> Color { get; set; }
|
|
|
|
public Partial<LocalDate?> Birthday { get; set; }
|
|
|
|
public Partial<string?> Pronouns { get; set; }
|
|
|
|
public Partial<string?> Description { get; set; }
|
|
|
|
public Partial<ProxyTag[]> ProxyTags { get; set; }
|
|
|
|
public Partial<bool> KeepProxy { get; set; }
|
|
|
|
public Partial<int> MessageCount { get; set; }
|
2020-11-20 21:40:36 +00:00
|
|
|
public Partial<bool> AllowAutoproxy { get; set; }
|
2020-06-29 11:57:48 +00:00
|
|
|
public Partial<PrivacyLevel> Visibility { get; set; }
|
|
|
|
public Partial<PrivacyLevel> NamePrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> PronounPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> BirthdayPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> AvatarPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> MetadataPrivacy { get; set; }
|
|
|
|
|
|
|
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
|
|
|
.With("name", Name)
|
2021-06-08 17:37:44 +00:00
|
|
|
.With("hid", Hid)
|
2020-06-29 11:57:48 +00:00
|
|
|
.With("display_name", DisplayName)
|
2020-06-29 12:15:30 +00:00
|
|
|
.With("avatar_url", AvatarUrl)
|
2021-08-02 17:46:12 +00:00
|
|
|
.With("banner_image", BannerImage)
|
2020-06-29 11:57:48 +00:00
|
|
|
.With("color", Color)
|
|
|
|
.With("birthday", Birthday)
|
|
|
|
.With("pronouns", Pronouns)
|
|
|
|
.With("description", Description)
|
|
|
|
.With("proxy_tags", ProxyTags)
|
|
|
|
.With("keep_proxy", KeepProxy)
|
|
|
|
.With("message_count", MessageCount)
|
2020-11-20 21:40:36 +00:00
|
|
|
.With("allow_autoproxy", AllowAutoproxy)
|
2020-06-29 11:57:48 +00:00
|
|
|
.With("member_visibility", Visibility)
|
|
|
|
.With("name_privacy", NamePrivacy)
|
|
|
|
.With("description_privacy", DescriptionPrivacy)
|
|
|
|
.With("pronoun_privacy", PronounPrivacy)
|
|
|
|
.With("birthday_privacy", BirthdayPrivacy)
|
|
|
|
.With("avatar_privacy", AvatarPrivacy)
|
|
|
|
.With("metadata_privacy", MetadataPrivacy);
|
2021-04-21 21:57:19 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
public new void AssertIsValid()
|
2021-04-21 21:57:19 +00:00
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
if (Name.IsPresent)
|
|
|
|
AssertValid(Name.Value, "display_name", Limits.MaxMemberNameLength);
|
|
|
|
if (DisplayName.Value != null)
|
|
|
|
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
|
|
|
|
if (AvatarUrl.Value != null)
|
|
|
|
AssertValid(AvatarUrl.Value, "avatar_url", 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}$");
|
|
|
|
if (Pronouns.Value != null)
|
|
|
|
AssertValid(Pronouns.Value, "pronouns", Limits.MaxPronounsLength);
|
|
|
|
if (Description.Value != null)
|
|
|
|
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
|
|
|
|
if (ProxyTags.IsPresent && (ProxyTags.Value.Length > 100 ||
|
|
|
|
ProxyTags.Value.Any(tag => tag.ProxyString.IsLongerThan(100))))
|
|
|
|
// todo: have a better error for this
|
|
|
|
throw new ValidationError("proxy_tags");
|
2021-04-21 21:57:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
#nullable disable
|
|
|
|
|
|
|
|
public static MemberPatch FromJSON(JObject o)
|
|
|
|
{
|
|
|
|
var patch = new MemberPatch();
|
|
|
|
|
2021-08-27 15:03:47 +00:00
|
|
|
if (o.ContainsKey("name") && o["name"].Type == JTokenType.Null)
|
2021-08-26 01:43:31 +00:00
|
|
|
throw new ValidationError("Member name can not be set to null.");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name");
|
2021-08-07 22:13:46 +00:00
|
|
|
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
|
2021-08-26 01:43:31 +00:00
|
|
|
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
|
2021-08-07 22:13:46 +00:00
|
|
|
|
|
|
|
if (o.ContainsKey("birthday"))
|
|
|
|
{
|
|
|
|
var str = o.Value<string>("birthday").NullIfEmpty();
|
|
|
|
var res = DateTimeFormats.DateExportFormat.Parse(str);
|
|
|
|
if (res.Success) patch.Birthday = res.Value;
|
|
|
|
else if (str == null) patch.Birthday = null;
|
2021-08-26 01:43:31 +00:00
|
|
|
else throw new ValidationError("birthday");
|
2021-08-07 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
if (o.ContainsKey("pronouns")) patch.Pronouns = o.Value<string>("pronouns").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
|
2021-08-07 22:13:46 +00:00
|
|
|
if (o.ContainsKey("keep_proxy")) patch.KeepProxy = o.Value<bool>("keep_proxy");
|
|
|
|
|
|
|
|
// legacy: used in old export files and APIv1
|
|
|
|
if (o.ContainsKey("prefix") || o.ContainsKey("suffix") && !o.ContainsKey("proxy_tags"))
|
2021-08-27 15:03:47 +00:00
|
|
|
patch.ProxyTags = new[] { new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")) };
|
2021-08-07 22:13:46 +00:00
|
|
|
else if (o.ContainsKey("proxy_tags"))
|
|
|
|
patch.ProxyTags = o.Value<JArray>("proxy_tags")
|
|
|
|
.OfType<JObject>().Select(o => new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
|
2021-08-26 01:43:31 +00:00
|
|
|
.Where(p => p.Valid)
|
2021-08-07 22:13:46 +00:00
|
|
|
.ToArray();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
|
|
|
if (o.ContainsKey("privacy")) //TODO: Deprecate this completely in api v2
|
2021-08-07 22:13:46 +00:00
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
var plevel = o.ParsePrivacy("privacy");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
patch.Visibility = plevel;
|
|
|
|
patch.NamePrivacy = plevel;
|
|
|
|
patch.AvatarPrivacy = plevel;
|
|
|
|
patch.DescriptionPrivacy = plevel;
|
|
|
|
patch.BirthdayPrivacy = plevel;
|
|
|
|
patch.PronounPrivacy = plevel;
|
|
|
|
// member.ColorPrivacy = plevel;
|
|
|
|
patch.MetadataPrivacy = plevel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
if (o.ContainsKey("visibility")) patch.Visibility = o.ParsePrivacy("visibility");
|
|
|
|
if (o.ContainsKey("name_privacy")) patch.NamePrivacy = o.ParsePrivacy("name_privacy");
|
|
|
|
if (o.ContainsKey("description_privacy")) patch.DescriptionPrivacy = o.ParsePrivacy("description_privacy");
|
|
|
|
if (o.ContainsKey("avatar_privacy")) patch.AvatarPrivacy = o.ParsePrivacy("avatar_privacy");
|
|
|
|
if (o.ContainsKey("birthday_privacy")) patch.BirthdayPrivacy = o.ParsePrivacy("birthday_privacy");
|
|
|
|
if (o.ContainsKey("pronoun_privacy")) patch.PronounPrivacy = o.ParsePrivacy("pronoun_privacy");
|
|
|
|
// if (o.ContainsKey("color_privacy")) member.ColorPrivacy = o.ParsePrivacy("member");
|
|
|
|
if (o.ContainsKey("metadata_privacy")) patch.MetadataPrivacy = o.ParsePrivacy("metadata_privacy");
|
2021-08-07 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return patch;
|
|
|
|
}
|
2020-06-29 11:57:48 +00:00
|
|
|
}
|
|
|
|
}
|