2020-06-29 12:39:19 +00:00
|
|
|
|
#nullable enable
|
2021-04-21 21:57:19 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
2020-06-29 12:39:19 +00:00
|
|
|
|
namespace PluralKit.Core
|
|
|
|
|
{
|
|
|
|
|
public class SystemPatch: PatchObject
|
|
|
|
|
{
|
|
|
|
|
public Partial<string?> Name { get; set; }
|
2021-06-08 17:37:44 +00:00
|
|
|
|
public Partial<string?> Hid { get; set; }
|
2020-06-29 12:39:19 +00:00
|
|
|
|
public Partial<string?> Description { get; set; }
|
|
|
|
|
public Partial<string?> Tag { get; set; }
|
|
|
|
|
public Partial<string?> AvatarUrl { 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; }
|
2020-06-29 12:39:19 +00:00
|
|
|
|
public Partial<string?> Token { get; set; }
|
|
|
|
|
public Partial<string> UiTz { get; set; }
|
|
|
|
|
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
|
|
|
|
|
public Partial<PrivacyLevel> MemberListPrivacy { get; set; }
|
2020-08-20 19:43:17 +00:00
|
|
|
|
public Partial<PrivacyLevel> GroupListPrivacy { get; set; }
|
2020-06-29 12:39:19 +00:00
|
|
|
|
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
|
|
|
|
|
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
|
|
|
|
|
public Partial<bool> PingsEnabled { get; set; }
|
2020-12-08 11:57:17 +00:00
|
|
|
|
public Partial<int?> LatchTimeout { get; set; }
|
2021-06-08 17:37:44 +00:00
|
|
|
|
public Partial<int?> MemberLimitOverride { get; set; }
|
|
|
|
|
public Partial<int?> GroupLimitOverride { get; set; }
|
2020-06-29 12:39:19 +00:00
|
|
|
|
|
|
|
|
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
|
|
|
|
.With("name", Name)
|
2021-06-08 17:37:44 +00:00
|
|
|
|
.With("hid", Hid)
|
2020-06-29 12:39:19 +00:00
|
|
|
|
.With("description", Description)
|
|
|
|
|
.With("tag", Tag)
|
|
|
|
|
.With("avatar_url", AvatarUrl)
|
2021-08-02 17:46:12 +00:00
|
|
|
|
.With("banner_image", BannerImage)
|
2021-03-28 10:02:41 +00:00
|
|
|
|
.With("color", Color)
|
2020-06-29 12:39:19 +00:00
|
|
|
|
.With("token", Token)
|
|
|
|
|
.With("ui_tz", UiTz)
|
|
|
|
|
.With("description_privacy", DescriptionPrivacy)
|
|
|
|
|
.With("member_list_privacy", MemberListPrivacy)
|
2020-08-20 19:43:17 +00:00
|
|
|
|
.With("group_list_privacy", GroupListPrivacy)
|
2020-06-29 12:39:19 +00:00
|
|
|
|
.With("front_privacy", FrontPrivacy)
|
|
|
|
|
.With("front_history_privacy", FrontHistoryPrivacy)
|
2020-11-21 00:44:15 +00:00
|
|
|
|
.With("pings_enabled", PingsEnabled)
|
2021-06-08 17:37:44 +00:00
|
|
|
|
.With("latch_timeout", LatchTimeout)
|
|
|
|
|
.With("member_limit_override", MemberLimitOverride)
|
|
|
|
|
.With("group_limit_override", GroupLimitOverride);
|
2021-04-21 21:57:19 +00:00
|
|
|
|
|
|
|
|
|
public new void CheckIsValid()
|
|
|
|
|
{
|
|
|
|
|
if (AvatarUrl.Value != null && !MiscUtils.TryMatchUri(AvatarUrl.Value, out var avatarUri))
|
|
|
|
|
throw new InvalidPatchException("avatar_url");
|
2021-08-02 17:46:12 +00:00
|
|
|
|
if (BannerImage.Value != null && !MiscUtils.TryMatchUri(BannerImage.Value, out var bannerImage))
|
|
|
|
|
throw new InvalidPatchException("banner");
|
2021-04-21 21:57:19 +00:00
|
|
|
|
if (Color.Value != null && (!Regex.IsMatch(Color.Value, "^[0-9a-fA-F]{6}$")))
|
|
|
|
|
throw new InvalidPatchException("color");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
|
public static SystemPatch FromJSON(JObject o)
|
|
|
|
|
{
|
|
|
|
|
var patch = new SystemPatch();
|
|
|
|
|
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name").NullIfEmpty().BoundsCheckField(Limits.MaxSystemNameLength, "System name");
|
|
|
|
|
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty().BoundsCheckField(Limits.MaxDescriptionLength, "System description");
|
|
|
|
|
if (o.ContainsKey("tag")) patch.Tag = o.Value<string>("tag").NullIfEmpty().BoundsCheckField(Limits.MaxSystemTagLength, "System tag");
|
|
|
|
|
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty().BoundsCheckField(Limits.MaxUriLength, "System avatar URL");
|
|
|
|
|
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty().BoundsCheckField(Limits.MaxUriLength, "System banner URL");
|
|
|
|
|
if (o.ContainsKey("timezone")) patch.UiTz = o.Value<string>("tz") ?? "UTC";
|
|
|
|
|
|
|
|
|
|
// legacy: APIv1 uses "tz" instead of "timezone"
|
|
|
|
|
// todo: remove in APIv2
|
|
|
|
|
if (o.ContainsKey("tz")) patch.UiTz = o.Value<string>("tz") ?? "UTC";
|
|
|
|
|
|
|
|
|
|
if (o.ContainsKey("description_privacy")) patch.DescriptionPrivacy = o.Value<string>("description_privacy").ParsePrivacy("description");
|
|
|
|
|
if (o.ContainsKey("member_list_privacy")) patch.MemberListPrivacy = o.Value<string>("member_list_privacy").ParsePrivacy("member list");
|
|
|
|
|
if (o.ContainsKey("front_privacy")) patch.FrontPrivacy = o.Value<string>("front_privacy").ParsePrivacy("front");
|
|
|
|
|
if (o.ContainsKey("front_history_privacy")) patch.FrontHistoryPrivacy = o.Value<string>("front_history_privacy").ParsePrivacy("front history");
|
|
|
|
|
return patch;
|
|
|
|
|
}
|
2020-06-29 12:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|