From a2bf70b3953b918640b2e94ba9d21d849ccb51fa Mon Sep 17 00:00:00 2001 From: spiral Date: Mon, 13 Sep 2021 03:14:59 -0400 Subject: [PATCH] feat: don't try to match other systems' members in switch / group add commands --- .../CommandSystem/ContextArgumentsExt.cs | 8 +++----- .../CommandSystem/ContextEntityArgumentsExt.cs | 16 ++++++++-------- .../Database/Repository/ModelRepository.Group.cs | 7 +++++-- .../Repository/ModelRepository.Member.cs | 8 ++++++-- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs index f7061a89..61f7e881 100644 --- a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs @@ -104,14 +104,12 @@ namespace PluralKit.Bot while (ctx.HasNext()) { // and attempt to match a member - var member = await ctx.MatchMember(); + var member = await ctx.MatchMember(restrictToSystem); + if (member == null) // if we can't, big error. Every member name must be valid. throw new PKError(ctx.CreateMemberNotFoundError(ctx.PopArgument())); - if (restrictToSystem != null && member.System != restrictToSystem) - throw Errors.NotOwnMemberError; // TODO: name *which* member? - members.Add(member); // Then add to the final output list } if (members.Count == 0) throw new PKSyntaxError($"You must input at least one member."); @@ -127,7 +125,7 @@ namespace PluralKit.Bot while (ctx.HasNext()) { // and attempt to match a group - var group = await ctx.MatchGroup(); + var group = await ctx.MatchGroup(restrictToSystem); if (group == null) // if we can't, big error. Every group name must be valid. throw new PKError(ctx.CreateGroupNotFoundError(ctx.PopArgument())); diff --git a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs index f3fcd343..f7061150 100644 --- a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs @@ -60,7 +60,7 @@ namespace PluralKit.Bot return system; } - public static async Task PeekMember(this Context ctx) + public static async Task PeekMember(this Context ctx, SystemId? restrictToSystem = null) { var input = ctx.PeekArgument(); @@ -76,7 +76,7 @@ namespace PluralKit.Bot return memberByName; // Then, try member HID parsing: - if (await ctx.Repository.GetMemberByHid(conn, input) is PKMember memberByHid) + if (await ctx.Repository.GetMemberByHid(conn, input, restrictToSystem) is PKMember memberByHid) return memberByHid; // And if that again fails, we try finding a member with a display name matching the argument from the system @@ -91,10 +91,10 @@ namespace PluralKit.Bot /// Attempts to pop a member descriptor from the stack, returning it if present. If a member could not be /// resolved by the next word in the argument stack, does *not* touch the stack, and returns null. /// - public static async Task MatchMember(this Context ctx) + public static async Task MatchMember(this Context ctx, SystemId? restrictToSystem = null) { // First, peek a member - var member = await ctx.PeekMember(); + var member = await ctx.PeekMember(restrictToSystem); // If the peek was successful, we've used up the next argument, so we pop that just to get rid of it. if (member != null) ctx.PopArgument(); @@ -103,14 +103,14 @@ namespace PluralKit.Bot return member; } - public static async Task PeekGroup(this Context ctx) + public static async Task PeekGroup(this Context ctx, SystemId? restrictToSystem = null) { var input = ctx.PeekArgument(); await using var conn = await ctx.Database.Obtain(); if (ctx.System != null && await ctx.Repository.GetGroupByName(conn, ctx.System.Id, input) is { } byName) return byName; - if (await ctx.Repository.GetGroupByHid(conn, input) is { } byHid) + if (await ctx.Repository.GetGroupByHid(conn, input, restrictToSystem) is { } byHid) return byHid; if (await ctx.Repository.GetGroupByDisplayName(conn, ctx.System.Id, input) is { } byDisplayName) return byDisplayName; @@ -118,9 +118,9 @@ namespace PluralKit.Bot return null; } - public static async Task MatchGroup(this Context ctx) + public static async Task MatchGroup(this Context ctx, SystemId? restrictToSystem = null) { - var group = await ctx.PeekGroup(); + var group = await ctx.PeekGroup(restrictToSystem); if (group != null) ctx.PopArgument(); return group; } diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs index 24d1e17c..67bc1147 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs @@ -17,8 +17,11 @@ namespace PluralKit.Core public Task GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) => conn.QueryFirstOrDefaultAsync("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name }); - public Task GetGroupByHid(IPKConnection conn, string hid) => - conn.QueryFirstOrDefaultAsync("select * from groups where hid = @hid", new { hid = hid.ToLowerInvariant() }); + public Task GetGroupByHid(IPKConnection conn, string hid, SystemId? system = null) + => conn.QueryFirstOrDefaultAsync( + "select * from groups where hid = @hid" + (system != null ? " and system = @System" : ""), + new { hid = hid.ToLowerInvariant(), System = system } + ); public Task GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null) { diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs index 1ab4fc60..654c08ff 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.Data; using System.Threading.Tasks; @@ -11,8 +12,11 @@ namespace PluralKit.Core public Task GetMember(IPKConnection conn, MemberId id) => conn.QueryFirstOrDefaultAsync("select * from members where id = @id", new { id }); - public Task GetMemberByHid(IPKConnection conn, string hid) => - conn.QuerySingleOrDefaultAsync("select * from members where hid = @Hid", new { Hid = hid.ToLower() }); + public Task GetMemberByHid(IPKConnection conn, string hid, SystemId? system = null) + => conn.QuerySingleOrDefaultAsync( + "select * from members where hid = @Hid" + (system != null ? " and system = @System" : ""), + new { Hid = hid.ToLower(), System = system } + ); public Task GetMemberByName(IPKConnection conn, SystemId system, string name) => conn.QueryFirstOrDefaultAsync("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });