feat(api): add description_templates to /systems/@me/settings

This commit is contained in:
spiral 2022-05-09 13:54:33 -04:00
parent 4c8e5d9aec
commit 3a99f65b49
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
3 changed files with 27 additions and 0 deletions

View File

@ -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;

View File

@ -16,6 +16,8 @@ public class SystemConfigPatch: PatchObject
public Partial<bool> ShowPrivateInfo { get; set; }
public Partial<int?> MemberLimitOverride { get; set; }
public Partial<int?> GroupLimitOverride { get; set; }
public Partial<string[]> 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<bool>("group_default_private");
if (o.ContainsKey("description_templates"))
patch.DescriptionTemplates = o.Value<JArray>("description_templates").Select(x => x.Value<string>()).ToArray();
return patch;
}
}

View File

@ -15,6 +15,7 @@ public class SystemConfig
public bool ShowPrivateInfo { get; }
public int? MemberLimitOverride { get; }
public int? GroupLimitOverride { get; }
public ICollection<string> 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;
}
}