API patch improvements

- add PatchObject.CheckIsValid
- use transaction when creating member, as to not create a member if the
patch is invalid
- return edited system in `PATCH /s` endpoint
This commit is contained in:
spiral
2021-04-21 22:57:19 +01:00
parent a2d2036851
commit b34ed5c4c0
10 changed files with 103 additions and 22 deletions

View File

@@ -1,4 +1,6 @@
#nullable enable
using System.Text.RegularExpressions;
namespace PluralKit.Core
{
public class GroupPatch: PatchObject
@@ -24,5 +26,14 @@ namespace PluralKit.Core
.With("icon_privacy", IconPrivacy)
.With("list_privacy", ListPrivacy)
.With("visibility", Visibility);
public new void CheckIsValid()
{
if (Icon.Value != null && !MiscUtils.TryMatchUri(Icon.Value, out var avatarUri))
throw new InvalidPatchException("avatar_url");
if (Color.Value != null && (!Regex.IsMatch(Color.Value, "^[0-9a-fA-F]{6}$")))
throw new InvalidPatchException("color");
}
}
}

View File

@@ -1,4 +1,5 @@
#nullable enable
using System.Text.RegularExpressions;
using NodaTime;
@@ -44,5 +45,14 @@ namespace PluralKit.Core
.With("birthday_privacy", BirthdayPrivacy)
.With("avatar_privacy", AvatarPrivacy)
.With("metadata_privacy", MetadataPrivacy);
public new void CheckIsValid()
{
if (AvatarUrl.Value != null && !MiscUtils.TryMatchUri(AvatarUrl.Value, out var avatarUri))
throw new InvalidPatchException("avatar_url");
if (Color.Value != null && (!Regex.IsMatch(Color.Value, "^[0-9a-fA-F]{6}$")))
throw new InvalidPatchException("color");
}
}
}

View File

@@ -1,7 +1,17 @@
namespace PluralKit.Core
using System;
namespace PluralKit.Core
{
public class InvalidPatchException : Exception
{
public InvalidPatchException(string message) : base(message) {}
}
public abstract class PatchObject
{
public abstract UpdateQueryBuilder Apply(UpdateQueryBuilder b);
public void CheckIsValid() {}
}
}

View File

@@ -1,4 +1,6 @@
#nullable enable
using System.Text.RegularExpressions;
namespace PluralKit.Core
{
public class SystemPatch: PatchObject
@@ -33,5 +35,14 @@ namespace PluralKit.Core
.With("front_history_privacy", FrontHistoryPrivacy)
.With("pings_enabled", PingsEnabled)
.With("latch_timeout", LatchTimeout);
public new void CheckIsValid()
{
if (AvatarUrl.Value != null && !MiscUtils.TryMatchUri(AvatarUrl.Value, out var avatarUri))
throw new InvalidPatchException("avatar_url");
if (Color.Value != null && (!Regex.IsMatch(Color.Value, "^[0-9a-fA-F]{6}$")))
throw new InvalidPatchException("color");
}
}
}