implement parse list options and related commands

This commit is contained in:
dusk 2025-09-30 18:45:35 +00:00
parent 3e7898e5cc
commit 95fc7e9f60
No known key found for this signature in database
18 changed files with 367 additions and 199 deletions

View file

@ -51,11 +51,12 @@ public class GroupMember
groups.Count - toAction.Count));
}
public async Task ListMemberGroups(Context ctx, PKMember target)
public async Task ListMemberGroups(Context ctx, PKMember target, string? query, IHasListOptions flags)
{
var targetSystem = await ctx.Repository.GetSystem(target.System);
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.System), ctx.LookupContextFor(target.System));
var opts = flags.GetListOptions(ctx, target.System);
opts.MemberFilter = target.Id;
opts.Search = query;
var title = new StringBuilder($"Groups containing {target.NameFor(ctx)} (`{target.DisplayHid(ctx.Config)}`) in ");
if (ctx.Guild != null)
@ -137,15 +138,16 @@ public class GroupMember
members.Count - toAction.Count));
}
public async Task ListGroupMembers(Context ctx, PKGroup target)
public async Task ListGroupMembers(Context ctx, PKGroup target, string? query, IHasListOptions flags)
{
// see global system list for explanation of how privacy settings are used here
var targetSystem = await GetGroupSystem(ctx, target);
ctx.CheckSystemPrivacy(targetSystem.Id, target.ListPrivacy);
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.System), ctx.LookupContextFor(target.System));
var opts = flags.GetListOptions(ctx, target.System);
opts.GroupFilter = target.Id;
opts.Search = query;
var title = new StringBuilder($"Members of {target.DisplayName ?? target.Name} (`{target.DisplayHid(ctx.Config)}`) in ");
if (ctx.Guild != null)

View file

@ -478,7 +478,7 @@ public class Groups
}
}
public async Task ListSystemGroups(Context ctx, PKSystem system)
public async Task ListSystemGroups(Context ctx, PKSystem system, string? query, IHasListOptions flags)
{
if (system == null)
{
@ -492,7 +492,9 @@ public class Groups
// - ParseListOptions checks list access privacy and sets the privacy filter (which members show up in list)
// - RenderGroupList checks the indivual privacy for each member (NameFor, etc)
// the own system is always allowed to look up their list
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(system.Id), ctx.LookupContextFor(system.Id));
var opts = flags.GetListOptions(ctx, system.Id);
opts.Search = query;
await ctx.RenderGroupList(
ctx.LookupContextFor(system.Id),
system.Id,

View file

@ -9,95 +9,13 @@ using PluralKit.Core;
namespace PluralKit.Bot;
public interface IHasListOptions
{
ListOptions GetListOptions(Context ctx, SystemId system);
}
public static class ContextListExt
{
public static ListOptions ParseListOptions(this Context ctx, LookupContext directLookupCtx, LookupContext lookupContext)
{
var p = new ListOptions();
// Short or long list? (parse this first, as it can potentially take a positional argument)
var isFull = ctx.Match("f", "full", "big", "details", "long") || ctx.MatchFlag("f", "full");
p.Type = isFull ? ListType.Long : ListType.Short;
// Search query
if (ctx.HasNext())
p.Search = ctx.RemainderOrNull();
// Include description in search?
if (ctx.MatchFlag(
"search-description",
"filter-description",
"in-description",
"sd",
"description",
"desc"
))
p.SearchDescription = true;
// Sort property (default is by name, but adding a flag anyway, 'cause why not)
if (ctx.MatchFlag("by-name", "bn")) p.SortProperty = SortProperty.Name;
if (ctx.MatchFlag("by-display-name", "bdn")) p.SortProperty = SortProperty.DisplayName;
if (ctx.MatchFlag("by-id", "bid")) p.SortProperty = SortProperty.Hid;
if (ctx.MatchFlag("by-message-count", "bmc")) p.SortProperty = SortProperty.MessageCount;
if (ctx.MatchFlag("by-created", "bc", "bcd")) p.SortProperty = SortProperty.CreationDate;
if (ctx.MatchFlag("by-last-fronted", "by-last-front", "by-last-switch", "blf", "bls"))
p.SortProperty = SortProperty.LastSwitch;
if (ctx.MatchFlag("by-last-message", "blm", "blp")) p.SortProperty = SortProperty.LastMessage;
if (ctx.MatchFlag("by-birthday", "by-birthdate", "bbd")) p.SortProperty = SortProperty.Birthdate;
if (ctx.MatchFlag("random", "rand")) p.SortProperty = SortProperty.Random;
// Sort reverse?
if (ctx.MatchFlag("r", "rev", "reverse"))
p.Reverse = true;
// Privacy filter (default is public only)
if (ctx.MatchFlag("a", "all")) p.PrivacyFilter = null;
if (ctx.MatchFlag("private-only", "po")) p.PrivacyFilter = PrivacyLevel.Private;
// PERM CHECK: If we're trying to access non-public members of another system, error
if (p.PrivacyFilter != PrivacyLevel.Public && directLookupCtx != LookupContext.ByOwner)
// TODO: should this just return null instead of throwing or something? >.>
throw Errors.NotOwnInfo;
//this is for searching
p.Context = lookupContext;
// Additional fields to include in the search results
if (ctx.MatchFlag("with-last-switch", "with-last-fronted", "with-last-front", "wls", "wlf"))
p.IncludeLastSwitch = true;
if (ctx.MatchFlag("with-last-message", "with-last-proxy", "wlm", "wlp"))
p.IncludeLastMessage = true;
if (ctx.MatchFlag("with-message-count", "wmc"))
p.IncludeMessageCount = true;
if (ctx.MatchFlag("with-created", "wc"))
p.IncludeCreated = true;
if (ctx.MatchFlag("with-avatar", "with-image", "with-icon", "wa", "wi", "ia", "ii", "img"))
p.IncludeAvatar = true;
if (ctx.MatchFlag("with-pronouns", "wp", "wprns"))
p.IncludePronouns = true;
if (ctx.MatchFlag("with-displayname", "wdn"))
p.IncludeDisplayName = true;
if (ctx.MatchFlag("with-birthday", "wbd", "wb"))
p.IncludeBirthday = true;
// Always show the sort property, too (unless this is the short list and we are already showing something else)
if (p.Type != ListType.Short || p.includedCount == 0)
{
if (p.SortProperty == SortProperty.DisplayName) p.IncludeDisplayName = true;
if (p.SortProperty == SortProperty.MessageCount) p.IncludeMessageCount = true;
if (p.SortProperty == SortProperty.CreationDate) p.IncludeCreated = true;
if (p.SortProperty == SortProperty.LastSwitch) p.IncludeLastSwitch = true;
if (p.SortProperty == SortProperty.LastMessage) p.IncludeLastMessage = true;
if (p.SortProperty == SortProperty.Birthdate) p.IncludeBirthday = true;
}
// Make sure the options are valid
p.AssertIsValid();
// Done!
return p;
}
public static async Task RenderMemberList(this Context ctx, LookupContext lookupCtx,
SystemId system, string embedTitle, string color, ListOptions opts)
{

View file

@ -184,6 +184,7 @@ public static class ListOptionsExt
// the check for multiple *sorting* property flags is done in SortProperty setter
}
}
public enum SortProperty

View file

@ -82,11 +82,11 @@ public class Random
components: await _embeds.CreateGroupMessageComponents(ctx, target, groups.ToArray()[randInt]));
}
public async Task GroupMember(Context ctx, PKGroup group, bool all, bool showEmbed = false)
public async Task GroupMember(Context ctx, PKGroup group, GroupRandomMemberFlags flags)
{
ctx.CheckSystemPrivacy(group.System, group.ListPrivacy);
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(group.System), ctx.LookupContextFor(group.System));
var opts = flags.GetListOptions(ctx, group.System);
opts.GroupFilter = group.Id;
var members = await ctx.Database.Execute(conn => conn.QueryMemberList(group.System, opts.ToQueryOptions()));
@ -96,7 +96,7 @@ public class Random
"This group has no members!"
+ (ctx.System?.Id == group.System ? " Please add at least one member to this group before using this command." : ""));
if (!all)
if (!flags.all)
members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public);
else
ctx.CheckOwnGroup(group);
@ -112,7 +112,7 @@ public class Random
var randInt = randGen.Next(ms.Count);
if (showEmbed)
if (flags.show_embed)
{
await ctx.Reply(
text: EmbedService.LEGACY_EMBED_WARNING,

View file

@ -8,16 +8,20 @@ namespace PluralKit.Bot;
public class SystemList
{
public async Task MemberList(Context ctx, PKSystem target)
public async Task MemberList(Context ctx, PKSystem target, string? query, IHasListOptions flags)
{
ctx.CheckSystem(target);
if (target == null) throw Errors.NoSystemError(ctx.DefaultPrefix);
ctx.CheckSystemPrivacy(target.Id, target.MemberListPrivacy);
var opts = flags.GetListOptions(ctx, target.Id);
opts.Search = query;
// explanation of privacy lookup here:
// - ParseListOptions checks list access privacy and sets the privacy filter (which members show up in list)
// - RenderMemberList checks the indivual privacy for each member (NameFor, etc)
// the own system is always allowed to look up their list
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.Id), ctx.LookupContextFor(target.Id));
await ctx.RenderMemberList(
ctx.LookupContextFor(target.Id),
target.Id,