feat(apiv2): guild endpoints

This commit is contained in:
spiral
2021-10-13 01:02:34 -04:00
parent eb05cbf76c
commit f602f22a3d
7 changed files with 242 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
using Newtonsoft.Json.Linq;
#nullable enable
namespace PluralKit.Core
{
@@ -8,4 +10,17 @@ namespace PluralKit.Core
public string? DisplayName { get; }
public string? AvatarUrl { get; }
}
public static class MemberGuildExt
{
public static JObject ToJson(this MemberGuildSettings settings)
{
var o = new JObject();
o.Add("display_name", settings.DisplayName);
o.Add("avatar_url", settings.AvatarUrl);
return o;
}
}
}

View File

@@ -1,5 +1,7 @@
#nullable enable
using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
@@ -13,5 +15,28 @@ namespace PluralKit.Core
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
);
public new void AssertIsValid()
{
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
}
#nullable disable
public static MemberGuildPatch FromJson(JObject o)
{
var patch = new MemberGuildPatch();
if (o.ContainsKey("display_name"))
patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("avatar_url"))
patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
return patch;
}
}
}

View File

@@ -1,5 +1,7 @@
#nullable enable
using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
@@ -19,5 +21,33 @@ namespace PluralKit.Core
.With("tag", Tag)
.With("tag_enabled", TagEnabled)
);
public new void AssertIsValid()
{
if (Tag.Value != null)
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
}
#nullable disable
public static SystemGuildPatch FromJson(JObject o, MemberId? memberId)
{
var patch = new SystemGuildPatch();
if (o.ContainsKey("proxying_enabled") && o["proxying_enabled"].Type != JTokenType.Null)
patch.ProxyEnabled = o.Value<bool>("proxying_enabled");
if (o.ContainsKey("autoproxy_mode") && o["autoproxy_mode"].ParseAutoproxyMode() is { } autoproxyMode)
patch.AutoproxyMode = autoproxyMode;
patch.AutoproxyMember = memberId;
if (o.ContainsKey("tag"))
patch.Tag = o.Value<string>("tag").NullIfEmpty();
if (o.ContainsKey("tag_enabled") && o["tag_enabled"].Type != JTokenType.Null)
patch.TagEnabled = o.Value<bool>("tag_enabled");
return patch;
}
}
}

View File

@@ -1,5 +1,10 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
namespace PluralKit.Core
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AutoproxyMode
{
Off = 1,
@@ -20,4 +25,44 @@ namespace PluralKit.Core
public string? Tag { get; }
public bool TagEnabled { get; }
}
public static class SystemGuildExt
{
public static JObject ToJson(this SystemGuildSettings settings, string? memberHid = null)
{
var o = new JObject();
o.Add("proxying_enabled", settings.ProxyEnabled);
o.Add("autoproxy_mode", settings.AutoproxyMode.ToString().ToLower());
o.Add("autoproxy_member", memberHid);
o.Add("tag", settings.Tag);
o.Add("tag_enabled", settings.TagEnabled);
return o;
}
public static AutoproxyMode? ParseAutoproxyMode(this JToken o)
{
if (o.Type == JTokenType.Null)
return AutoproxyMode.Off;
else if (o.Type != JTokenType.String)
return null;
var value = o.Value<string>();
switch (value)
{
case "off":
return AutoproxyMode.Off;
case "front":
return AutoproxyMode.Front;
case "latch":
return AutoproxyMode.Latch;
case "member":
return AutoproxyMode.Member;
default:
throw new ValidationError($"Value '{value}' is not a valid autoproxy mode.");
}
}
}
}