From c472a7f6df7be258ac7a94f2eb66e1084a89c847 Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 22 Sep 2021 13:48:34 -0400 Subject: [PATCH] lint/fixes, add group patch validation --- PluralKit.Bot/Commands/Message.cs | 2 +- PluralKit.Bot/Proxy/ProxyService.cs | 4 ++-- .../Tracing/DiscordRequestObserver.cs | 2 +- PluralKit.Core/Models/Patch/GroupPatch.cs | 20 +++++++++++++------ PluralKit.Core/Models/Patch/MemberPatch.cs | 2 +- PluralKit.Core/Services/DataFileService.cs | 2 +- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 93be6c14..3e4f22da 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -137,7 +137,7 @@ namespace PluralKit.Bot { if (message.System.Id != ctx.System.Id) throw new PKError("You can only delete your own messages."); - + await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid); if (ctx.Guild != null) diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index 176dca1b..13f952d4 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -55,13 +55,13 @@ namespace PluralKit.Bot { if (!ShouldProxy(channel, message, ctx)) return false; - + var rootChannel = _cache.GetRootChannel(message.ChannelId); List members; // Fetch members and try to match to a specific member using (_metrics.Measure.Timer.Time(BotMetrics.ProxyMembersQueryTime)) - { + { await using var conn = await _db.Obtain(); members = (await _repo.GetProxyMembers(conn, message.Author.Id, message.GuildId!.Value)).ToList(); } diff --git a/PluralKit.Bot/Tracing/DiscordRequestObserver.cs b/PluralKit.Bot/Tracing/DiscordRequestObserver.cs index 801b63e0..d3710445 100644 --- a/PluralKit.Bot/Tracing/DiscordRequestObserver.cs +++ b/PluralKit.Bot/Tracing/DiscordRequestObserver.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; diff --git a/PluralKit.Core/Models/Patch/GroupPatch.cs b/PluralKit.Core/Models/Patch/GroupPatch.cs index 4ff0a401..ea5b0a35 100644 --- a/PluralKit.Core/Models/Patch/GroupPatch.cs +++ b/PluralKit.Core/Models/Patch/GroupPatch.cs @@ -35,12 +35,20 @@ namespace PluralKit.Core public new void AssertIsValid() { - if (Icon.Value != null && !MiscUtils.TryMatchUri(Icon.Value, out var avatarUri)) - throw new ValidationError("icon"); - if (BannerImage.Value != null && !MiscUtils.TryMatchUri(BannerImage.Value, out var bannerImage)) - throw new ValidationError("banner"); - if (Color.Value != null && (!Regex.IsMatch(Color.Value, "^[0-9a-fA-F]{6}$"))) - throw new ValidationError("color"); + if (Name.IsPresent) + AssertValid(Name.Value, "name", Limits.MaxGroupNameLength); + if (DisplayName.Value != null) + AssertValid(DisplayName.Value, "display_name", Limits.MaxGroupNameLength); + if (Description.Value != null) + AssertValid(Description.Value, "description", Limits.MaxDescriptionLength); + if (Icon.Value != null) + AssertValid(Icon.Value, "icon", Limits.MaxUriLength, + s => MiscUtils.TryMatchUri(s, out var avatarUri)); + if (BannerImage.Value != null) + AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength, + s => MiscUtils.TryMatchUri(s, out var bannerUri)); + if (Color.Value != null) + AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$"); } #nullable disable diff --git a/PluralKit.Core/Models/Patch/MemberPatch.cs b/PluralKit.Core/Models/Patch/MemberPatch.cs index 0d06c9f0..ac2207cd 100644 --- a/PluralKit.Core/Models/Patch/MemberPatch.cs +++ b/PluralKit.Core/Models/Patch/MemberPatch.cs @@ -56,7 +56,7 @@ namespace PluralKit.Core public new void AssertIsValid() { if (Name.IsPresent) - AssertValid(Name.Value, "display_name", Limits.MaxMemberNameLength); + AssertValid(Name.Value, "name", Limits.MaxMemberNameLength); if (DisplayName.Value != null) AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength); if (AvatarUrl.Value != null) diff --git a/PluralKit.Core/Services/DataFileService.cs b/PluralKit.Core/Services/DataFileService.cs index a7986787..9126341b 100644 --- a/PluralKit.Core/Services/DataFileService.cs +++ b/PluralKit.Core/Services/DataFileService.cs @@ -84,7 +84,7 @@ namespace PluralKit.Core } } - + public class GroupMember { public string Group { get; set; }