From 3a99f65b493abfe31fd0c170bf7c259c2ef827b4 Mon Sep 17 00:00:00 2001 From: spiral Date: Mon, 9 May 2022 13:54:33 -0400 Subject: [PATCH] feat(api): add description_templates to /systems/@me/settings --- PluralKit.Core/Database/Migrations/30.sql | 5 +++++ .../Models/Patch/SystemConfigPatch.cs | 19 +++++++++++++++++++ PluralKit.Core/Models/SystemConfig.cs | 3 +++ 3 files changed, 27 insertions(+) create mode 100644 PluralKit.Core/Database/Migrations/30.sql diff --git a/PluralKit.Core/Database/Migrations/30.sql b/PluralKit.Core/Database/Migrations/30.sql new file mode 100644 index 00000000..26a2e514 --- /dev/null +++ b/PluralKit.Core/Database/Migrations/30.sql @@ -0,0 +1,5 @@ +-- schema version 30 + +alter table system_config add column description_templates text[] not null default array[]::text[]; + +update info set schema_version = 30; \ No newline at end of file diff --git a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs index a65ee1a8..f56f8225 100644 --- a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs +++ b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs @@ -16,6 +16,8 @@ public class SystemConfigPatch: PatchObject public Partial ShowPrivateInfo { get; set; } public Partial MemberLimitOverride { get; set; } public Partial GroupLimitOverride { get; set; } + public Partial DescriptionTemplates { get; set; } + public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper .With("ui_tz", UiTz) @@ -26,12 +28,23 @@ public class SystemConfigPatch: PatchObject .With("show_private_info", ShowPrivateInfo) .With("member_limit_override", MemberLimitOverride) .With("group_limit_override", GroupLimitOverride) + .With("description_templates", DescriptionTemplates) ); public new void AssertIsValid() { if (UiTz.IsPresent && DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz.Value) == null) Errors.Add(new ValidationError("timezone")); + + 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)); + } } public JObject ToJson() @@ -62,6 +75,9 @@ public class SystemConfigPatch: PatchObject if (GroupLimitOverride.IsPresent) o.Add("group_limit", GroupLimitOverride.Value); + if (DescriptionTemplates.IsPresent) + o.Add("description_templates", JArray.FromObject(DescriptionTemplates.Value)); + return o; } @@ -84,6 +100,9 @@ public class SystemConfigPatch: PatchObject if (o.ContainsKey("group_default_private")) patch.GroupDefaultPrivate = o.Value("group_default_private"); + if (o.ContainsKey("description_templates")) + patch.DescriptionTemplates = o.Value("description_templates").Select(x => x.Value()).ToArray(); + return patch; } } \ No newline at end of file diff --git a/PluralKit.Core/Models/SystemConfig.cs b/PluralKit.Core/Models/SystemConfig.cs index 905a3162..a3e0b960 100644 --- a/PluralKit.Core/Models/SystemConfig.cs +++ b/PluralKit.Core/Models/SystemConfig.cs @@ -15,6 +15,7 @@ public class SystemConfig public bool ShowPrivateInfo { get; } public int? MemberLimitOverride { get; } public int? GroupLimitOverride { get; } + public ICollection DescriptionTemplates { get; } public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz); } @@ -34,6 +35,8 @@ public static class SystemConfigExt o.Add("member_limit", cfg.MemberLimitOverride ?? Limits.MaxMemberCount); o.Add("group_limit", cfg.GroupLimitOverride ?? Limits.MaxGroupCount); + o.Add("description_templates", JArray.FromObject(cfg.DescriptionTemplates)); + return o; } } \ No newline at end of file