2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-08-07 22:13:46 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public class SystemPatch: PatchObject
|
2020-06-29 12:39:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public Partial<string?> Name { get; set; }
|
|
|
|
public Partial<string?> Hid { get; set; }
|
|
|
|
public Partial<string?> Description { get; set; }
|
|
|
|
public Partial<string?> Tag { get; set; }
|
2022-03-23 18:20:16 +00:00
|
|
|
public Partial<string?> Pronouns { get; set; }
|
2021-11-27 02:10:56 +00:00
|
|
|
public Partial<string?> AvatarUrl { get; set; }
|
|
|
|
public Partial<string?> BannerImage { get; set; }
|
|
|
|
public Partial<string?> Color { get; set; }
|
|
|
|
public Partial<string?> Token { get; set; }
|
|
|
|
public Partial<string?> WebhookUrl { get; set; }
|
|
|
|
public Partial<string?> WebhookToken { get; set; }
|
|
|
|
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> MemberListPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> GroupListPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
|
|
|
|
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
|
2022-03-23 18:20:16 +00:00
|
|
|
public Partial<PrivacyLevel> PronounPrivacy { get; set; }
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
|
|
|
.With("name", Name)
|
|
|
|
.With("hid", Hid)
|
|
|
|
.With("description", Description)
|
|
|
|
.With("tag", Tag)
|
2022-03-23 18:20:16 +00:00
|
|
|
.With("pronouns", Pronouns)
|
2021-11-27 02:10:56 +00:00
|
|
|
.With("avatar_url", AvatarUrl)
|
|
|
|
.With("banner_image", BannerImage)
|
|
|
|
.With("color", Color)
|
|
|
|
.With("token", Token)
|
|
|
|
.With("webhook_url", WebhookUrl)
|
|
|
|
.With("webhook_token", WebhookToken)
|
|
|
|
.With("description_privacy", DescriptionPrivacy)
|
|
|
|
.With("member_list_privacy", MemberListPrivacy)
|
|
|
|
.With("group_list_privacy", GroupListPrivacy)
|
|
|
|
.With("front_privacy", FrontPrivacy)
|
|
|
|
.With("front_history_privacy", FrontHistoryPrivacy)
|
2022-03-23 18:20:16 +00:00
|
|
|
.With("pronoun_privacy", PronounPrivacy)
|
2021-11-27 02:10:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
public new void AssertIsValid()
|
2020-06-29 12:39:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (Name.Value != null)
|
|
|
|
AssertValid(Name.Value, "name", Limits.MaxSystemNameLength);
|
|
|
|
if (Description.Value != null)
|
|
|
|
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
|
|
|
|
if (Tag.Value != null)
|
|
|
|
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
|
2022-03-23 18:20:16 +00:00
|
|
|
if (Pronouns.Value != null)
|
|
|
|
AssertValid(Pronouns.Value, "pronouns", Limits.MaxPronounsLength);
|
2021-11-27 02:10:56 +00:00
|
|
|
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}$");
|
|
|
|
}
|
2021-04-21 21:57:19 +00:00
|
|
|
|
2021-10-12 06:19:42 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2022-04-20 16:20:03 +00:00
|
|
|
public static SystemPatch FromJSON(JObject o, bool isImport = false)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
var patch = new SystemPatch();
|
|
|
|
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("tag")) patch.Tag = o.Value<string>("tag").NullIfEmpty();
|
2022-03-23 18:20:16 +00:00
|
|
|
if (o.ContainsKey("pronouns")) patch.Pronouns = o.Value<string>("pronouns").NullIfEmpty();
|
2021-11-27 02:10:56 +00:00
|
|
|
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty();
|
|
|
|
|
2022-04-20 16:20:03 +00:00
|
|
|
if (isImport)
|
2021-08-07 22:13:46 +00:00
|
|
|
{
|
2022-04-20 16:20:03 +00:00
|
|
|
if (o.ContainsKey("description_privacy"))
|
|
|
|
patch.DescriptionPrivacy = patch.ParsePrivacy(o, "description_privacy");
|
|
|
|
if (o.ContainsKey("member_list_privacy"))
|
|
|
|
patch.MemberListPrivacy = patch.ParsePrivacy(o, "member_list_privacy");
|
|
|
|
if (o.ContainsKey("front_privacy")) patch.FrontPrivacy = patch.ParsePrivacy(o, "front_privacy");
|
|
|
|
if (o.ContainsKey("front_history_privacy"))
|
|
|
|
patch.FrontHistoryPrivacy = patch.ParsePrivacy(o, "front_history_privacy");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
|
|
|
|
{
|
|
|
|
var privacy = o.Value<JObject>("privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("description_privacy"))
|
|
|
|
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("pronoun_privacy"))
|
|
|
|
patch.PronounPrivacy = patch.ParsePrivacy(privacy, "pronoun_privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("member_list_privacy"))
|
|
|
|
patch.MemberListPrivacy = patch.ParsePrivacy(privacy, "member_list_privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("group_list_privacy"))
|
|
|
|
patch.GroupListPrivacy = patch.ParsePrivacy(privacy, "group_list_privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("front_privacy"))
|
|
|
|
patch.FrontPrivacy = patch.ParsePrivacy(privacy, "front_privacy");
|
|
|
|
|
|
|
|
if (privacy.ContainsKey("front_history_privacy"))
|
|
|
|
patch.FrontHistoryPrivacy = patch.ParsePrivacy(privacy, "front_history_privacy");
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-10-12 06:19:42 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return patch;
|
|
|
|
}
|
2021-10-12 06:19:42 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public JObject ToJson()
|
|
|
|
{
|
|
|
|
var o = new JObject();
|
|
|
|
|
|
|
|
if (Name.IsPresent)
|
|
|
|
o.Add("name", Name.Value);
|
|
|
|
if (Hid.IsPresent)
|
|
|
|
o.Add("id", Hid.Value);
|
|
|
|
if (Description.IsPresent)
|
|
|
|
o.Add("description", Description.Value);
|
|
|
|
if (Tag.IsPresent)
|
|
|
|
o.Add("tag", Tag.Value);
|
2022-03-23 18:20:16 +00:00
|
|
|
if (Pronouns.IsPresent)
|
|
|
|
o.Add("pronouns", Pronouns.Value);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (AvatarUrl.IsPresent)
|
|
|
|
o.Add("avatar_url", AvatarUrl.Value);
|
|
|
|
if (BannerImage.IsPresent)
|
|
|
|
o.Add("banner", BannerImage.Value);
|
|
|
|
if (Color.IsPresent)
|
|
|
|
o.Add("color", Color.Value);
|
|
|
|
|
|
|
|
if (
|
|
|
|
DescriptionPrivacy.IsPresent
|
2022-03-23 18:20:16 +00:00
|
|
|
|| PronounPrivacy.IsPresent
|
2021-11-27 02:10:56 +00:00
|
|
|
|| MemberListPrivacy.IsPresent
|
|
|
|
|| GroupListPrivacy.IsPresent
|
|
|
|
|| FrontPrivacy.IsPresent
|
|
|
|
|| FrontHistoryPrivacy.IsPresent
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var p = new JObject();
|
2021-10-22 15:20:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (DescriptionPrivacy.IsPresent)
|
|
|
|
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
|
2021-10-12 06:19:42 +00:00
|
|
|
|
2022-03-23 18:20:16 +00:00
|
|
|
if (PronounPrivacy.IsPresent)
|
|
|
|
p.Add("pronoun_privacy", PronounPrivacy.Value.ToJsonString());
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (MemberListPrivacy.IsPresent)
|
|
|
|
p.Add("member_list_privacy", MemberListPrivacy.Value.ToJsonString());
|
2021-08-07 22:13:46 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (GroupListPrivacy.IsPresent)
|
|
|
|
p.Add("group_list_privacy", GroupListPrivacy.Value.ToJsonString());
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (FrontPrivacy.IsPresent)
|
|
|
|
p.Add("front_privacy", FrontPrivacy.Value.ToJsonString());
|
2021-11-02 10:08:17 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (FrontHistoryPrivacy.IsPresent)
|
|
|
|
p.Add("front_history_privacy", FrontHistoryPrivacy.Value.ToJsonString());
|
|
|
|
|
|
|
|
o.Add("privacy", p);
|
2021-11-02 10:08:17 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return o;
|
2020-06-29 12:39:19 +00:00
|
|
|
}
|
|
|
|
}
|