2021-11-30 02:35:21 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using SqlKata;
|
|
|
|
|
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public class SystemConfigPatch: PatchObject
|
|
|
|
{
|
|
|
|
public Partial<string> UiTz { get; set; }
|
|
|
|
public Partial<bool> PingsEnabled { get; set; }
|
|
|
|
public Partial<int?> LatchTimeout { get; set; }
|
2021-12-01 16:48:49 +00:00
|
|
|
public Partial<bool> MemberDefaultPrivate { get; set; }
|
|
|
|
public Partial<bool> GroupDefaultPrivate { get; set; }
|
2021-12-06 09:01:42 +00:00
|
|
|
public Partial<bool> ShowPrivateInfo { get; set; }
|
2021-11-30 02:35:21 +00:00
|
|
|
public Partial<int?> MemberLimitOverride { get; set; }
|
|
|
|
public Partial<int?> GroupLimitOverride { get; set; }
|
2022-05-09 17:54:33 +00:00
|
|
|
public Partial<string[]> DescriptionTemplates { get; set; }
|
2022-11-23 08:48:24 +00:00
|
|
|
public Partial<bool> CaseSensitiveProxyTags { get; set; }
|
2023-03-25 22:42:47 +00:00
|
|
|
public Partial<bool> ProxyErrorMessageEnabled { get; set; }
|
2022-05-09 17:54:33 +00:00
|
|
|
|
2021-11-30 02:35:21 +00:00
|
|
|
|
|
|
|
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
|
|
|
.With("ui_tz", UiTz)
|
|
|
|
.With("pings_enabled", PingsEnabled)
|
|
|
|
.With("latch_timeout", LatchTimeout)
|
2021-12-01 16:48:49 +00:00
|
|
|
.With("member_default_private", MemberDefaultPrivate)
|
|
|
|
.With("group_default_private", GroupDefaultPrivate)
|
2021-12-06 09:01:42 +00:00
|
|
|
.With("show_private_info", ShowPrivateInfo)
|
2021-11-30 02:35:21 +00:00
|
|
|
.With("member_limit_override", MemberLimitOverride)
|
|
|
|
.With("group_limit_override", GroupLimitOverride)
|
2022-05-09 17:54:33 +00:00
|
|
|
.With("description_templates", DescriptionTemplates)
|
2022-11-23 08:48:24 +00:00
|
|
|
.With("case_sensitive_proxy_tags", CaseSensitiveProxyTags)
|
2023-03-25 22:42:47 +00:00
|
|
|
.With("proxy_error_message_enabled", ProxyErrorMessageEnabled)
|
2021-11-30 02:35:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
public new void AssertIsValid()
|
|
|
|
{
|
|
|
|
if (UiTz.IsPresent && DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz.Value) == null)
|
|
|
|
Errors.Add(new ValidationError("timezone"));
|
2022-05-09 17:54:33 +00:00
|
|
|
|
|
|
|
if (DescriptionTemplates.IsPresent)
|
|
|
|
{
|
|
|
|
if (DescriptionTemplates.Value.Length > 3)
|
|
|
|
Errors.Add(new FieldTooLongError("description_templates", 3, DescriptionTemplates.Value.Length));
|
|
|
|
|
|
|
|
foreach (var template in DescriptionTemplates.Value)
|
|
|
|
if (template.Length > Limits.MaxDescriptionLength)
|
|
|
|
Errors.Add(new FieldTooLongError($"description_templates[{Array.IndexOf(DescriptionTemplates.Value, template)}]", template.Length, Limits.MaxDescriptionLength));
|
|
|
|
}
|
2021-11-30 02:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public JObject ToJson()
|
|
|
|
{
|
|
|
|
var o = new JObject();
|
|
|
|
|
|
|
|
if (UiTz.IsPresent)
|
|
|
|
o.Add("timezone", UiTz.Value);
|
|
|
|
|
|
|
|
if (PingsEnabled.IsPresent)
|
|
|
|
o.Add("pings_enabled", PingsEnabled.Value);
|
|
|
|
|
|
|
|
if (LatchTimeout.IsPresent)
|
|
|
|
o.Add("latch_timeout", LatchTimeout.Value);
|
|
|
|
|
2021-12-01 16:48:49 +00:00
|
|
|
if (MemberDefaultPrivate.IsPresent)
|
|
|
|
o.Add("member_default_private", MemberDefaultPrivate.Value);
|
|
|
|
|
|
|
|
if (GroupDefaultPrivate.IsPresent)
|
|
|
|
o.Add("group_default_private", GroupDefaultPrivate.Value);
|
|
|
|
|
2021-12-06 09:01:42 +00:00
|
|
|
if (ShowPrivateInfo.IsPresent)
|
|
|
|
o.Add("show_private_info", ShowPrivateInfo.Value);
|
|
|
|
|
2021-11-30 02:35:21 +00:00
|
|
|
if (MemberLimitOverride.IsPresent)
|
|
|
|
o.Add("member_limit", MemberLimitOverride.Value);
|
|
|
|
|
|
|
|
if (GroupLimitOverride.IsPresent)
|
|
|
|
o.Add("group_limit", GroupLimitOverride.Value);
|
|
|
|
|
2022-05-09 17:54:33 +00:00
|
|
|
if (DescriptionTemplates.IsPresent)
|
|
|
|
o.Add("description_templates", JArray.FromObject(DescriptionTemplates.Value));
|
|
|
|
|
2022-11-23 08:48:24 +00:00
|
|
|
if (CaseSensitiveProxyTags.IsPresent)
|
|
|
|
o.Add("case_sensitive_proxy_tags", CaseSensitiveProxyTags.Value);
|
|
|
|
|
2023-03-25 22:42:47 +00:00
|
|
|
if (ProxyErrorMessageEnabled.IsPresent)
|
|
|
|
o.Add("proxy_error_message_enabled", ProxyErrorMessageEnabled.Value);
|
|
|
|
|
2021-11-30 02:35:21 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SystemConfigPatch FromJson(JObject o)
|
|
|
|
{
|
|
|
|
var patch = new SystemConfigPatch();
|
|
|
|
|
|
|
|
if (o.ContainsKey("timezone"))
|
|
|
|
patch.UiTz = o.Value<string>("timezone");
|
|
|
|
|
|
|
|
if (o.ContainsKey("pings_enabled"))
|
|
|
|
patch.PingsEnabled = o.Value<bool>("pings_enabled");
|
|
|
|
|
|
|
|
if (o.ContainsKey("latch_timeout"))
|
2021-12-04 23:32:01 +00:00
|
|
|
patch.LatchTimeout = o.Value<int?>("latch_timeout");
|
2021-11-30 02:35:21 +00:00
|
|
|
|
2021-12-01 16:48:49 +00:00
|
|
|
if (o.ContainsKey("member_default_private"))
|
|
|
|
patch.MemberDefaultPrivate = o.Value<bool>("member_default_private");
|
|
|
|
|
|
|
|
if (o.ContainsKey("group_default_private"))
|
|
|
|
patch.GroupDefaultPrivate = o.Value<bool>("group_default_private");
|
|
|
|
|
2022-05-09 17:54:33 +00:00
|
|
|
if (o.ContainsKey("description_templates"))
|
|
|
|
patch.DescriptionTemplates = o.Value<JArray>("description_templates").Select(x => x.Value<string>()).ToArray();
|
|
|
|
|
2022-11-23 08:48:24 +00:00
|
|
|
if (o.ContainsKey("case_sensitive_proxy_tags"))
|
|
|
|
patch.CaseSensitiveProxyTags = o.Value<bool>("case_sensitive_proxy_tags");
|
|
|
|
|
2023-03-25 22:42:47 +00:00
|
|
|
if (o.ContainsKey("proxy_error_message_enabled"))
|
|
|
|
patch.ProxyErrorMessageEnabled = o.Value<bool>("proxy_error_message_enabled");
|
|
|
|
|
2021-11-30 02:35:21 +00:00
|
|
|
return patch;
|
|
|
|
}
|
|
|
|
}
|