feat: rework group list into member list
This commit is contained in:
@@ -13,9 +13,9 @@ namespace PluralKit.Bot;
|
||||
|
||||
public static class ContextListExt
|
||||
{
|
||||
public static MemberListOptions ParseMemberListOptions(this Context ctx, LookupContext lookupCtx)
|
||||
public static ListOptions ParseListOptions(this Context ctx, LookupContext lookupCtx)
|
||||
{
|
||||
var p = new MemberListOptions();
|
||||
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");
|
||||
@@ -71,7 +71,7 @@ public static class ContextListExt
|
||||
p.IncludeMessageCount = true;
|
||||
if (ctx.MatchFlag("with-created", "wc"))
|
||||
p.IncludeCreated = true;
|
||||
if (ctx.MatchFlag("with-avatar", "with-image", "wa", "wi", "ia", "ii", "img"))
|
||||
if (ctx.MatchFlag("with-avatar", "with-image", "with-icon", "wa", "wi", "ia", "ii", "img"))
|
||||
p.IncludeAvatar = true;
|
||||
if (ctx.MatchFlag("with-pronouns", "wp"))
|
||||
p.IncludePronouns = true;
|
||||
@@ -87,7 +87,7 @@ public static class ContextListExt
|
||||
}
|
||||
|
||||
public static async Task RenderMemberList(this Context ctx, LookupContext lookupCtx,
|
||||
SystemId system, string embedTitle, string color, MemberListOptions opts)
|
||||
SystemId system, string embedTitle, string color, ListOptions opts)
|
||||
{
|
||||
// We take an IDatabase instead of a IPKConnection so we don't keep the handle open for the entire runtime
|
||||
// We wanna release it as soon as the member list is actually *fetched*, instead of potentially minutes later (paginate timeout)
|
||||
@@ -248,4 +248,134 @@ public static class ContextListExt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task RenderGroupList(this Context ctx, LookupContext lookupCtx,
|
||||
SystemId system, string embedTitle, string color, ListOptions opts)
|
||||
{
|
||||
// We take an IDatabase instead of a IPKConnection so we don't keep the handle open for the entire runtime
|
||||
// We wanna release it as soon as the member list is actually *fetched*, instead of potentially minutes later (paginate timeout)
|
||||
var groups = (await ctx.Database.Execute(conn => conn.QueryGroupList(system, opts.ToQueryOptions())))
|
||||
.SortByGroupListOptions(opts, lookupCtx)
|
||||
.ToList();
|
||||
|
||||
var itemsPerPage = opts.Type == ListType.Short ? 25 : 5;
|
||||
await ctx.Paginate(groups.ToAsyncEnumerable(), groups.Count, itemsPerPage, embedTitle, color, Renderer);
|
||||
|
||||
// Base renderer, dispatches based on type
|
||||
Task Renderer(EmbedBuilder eb, IEnumerable<ListedGroup> page)
|
||||
{
|
||||
// Add a global footer with the filter/sort string + result count
|
||||
eb.Footer(new Embed.EmbedFooter($"{opts.CreateFilterString()}. {"result".ToQuantity(groups.Count)}."));
|
||||
|
||||
// Then call the specific renderers
|
||||
if (opts.Type == ListType.Short)
|
||||
ShortRenderer(eb, page);
|
||||
else
|
||||
LongRenderer(eb, page);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
void ShortRenderer(EmbedBuilder eb, IEnumerable<ListedGroup> page)
|
||||
{
|
||||
// We may end up over the description character limit
|
||||
// so run it through a helper that "makes it work" :)
|
||||
eb.WithSimpleLineContent(page.Select(g =>
|
||||
{
|
||||
var ret = $"[`{g.Hid}`] **{g.NameFor(ctx)}** ";
|
||||
|
||||
switch (opts.SortProperty)
|
||||
{
|
||||
case SortProperty.DisplayName:
|
||||
{
|
||||
if (g.NamePrivacy.CanAccess(lookupCtx) && g.DisplayName != null)
|
||||
ret += $"({g.DisplayName})";
|
||||
break;
|
||||
}
|
||||
case SortProperty.CreationDate:
|
||||
{
|
||||
if (g.MetadataPrivacy.TryGet(lookupCtx, g.Created, out var created))
|
||||
ret += $"(created at <t:{created.ToUnixTimeSeconds()}>)";
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
if (opts.IncludeCreated &&
|
||||
g.MetadataPrivacy.TryGet(lookupCtx, g.Created, out var created))
|
||||
{
|
||||
ret += $"(created at <t:{created.ToUnixTimeSeconds()}>)";
|
||||
}
|
||||
else if (opts.IncludeAvatar && g.IconFor(lookupCtx) is { } avatarUrl)
|
||||
{
|
||||
ret += $"([avatar URL]({avatarUrl}))";
|
||||
}
|
||||
else
|
||||
{
|
||||
// -priv/-pub and listprivacy affects whether count is shown
|
||||
// -all and visibility affects what the count is
|
||||
if (ctx.DirectLookupContextFor(system) == LookupContext.ByOwner)
|
||||
{
|
||||
if (g.ListPrivacy == PrivacyLevel.Public || lookupCtx == LookupContext.ByOwner)
|
||||
{
|
||||
if (ctx.MatchFlag("all", "a"))
|
||||
{
|
||||
ret += $"({"member".ToQuantity(g.TotalMemberCount)})";
|
||||
}
|
||||
else
|
||||
{
|
||||
ret += $"({"member".ToQuantity(g.PublicMemberCount)})";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (g.ListPrivacy == PrivacyLevel.Public)
|
||||
{
|
||||
ret += $"({"member".ToQuantity(g.PublicMemberCount)})";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}));
|
||||
}
|
||||
|
||||
void LongRenderer(EmbedBuilder eb, IEnumerable<ListedGroup> page)
|
||||
{
|
||||
foreach (var g in page)
|
||||
{
|
||||
var profile = new StringBuilder($"**ID**: {g.Hid}");
|
||||
|
||||
if (g.DisplayName != null && g.NamePrivacy.CanAccess(lookupCtx))
|
||||
profile.Append($"\n**Display name**: {g.DisplayName}");
|
||||
|
||||
if (g.ListPrivacy == PrivacyLevel.Public || lookupCtx == LookupContext.ByOwner)
|
||||
{
|
||||
if (ctx.MatchFlag("all", "a") && ctx.DirectLookupContextFor(system) == LookupContext.ByOwner)
|
||||
profile.Append($"\n**Member Count:** {g.TotalMemberCount}");
|
||||
else
|
||||
profile.Append($"\n**Member Count:** {g.PublicMemberCount}");
|
||||
}
|
||||
|
||||
if ((opts.IncludeCreated || opts.SortProperty == SortProperty.CreationDate) &&
|
||||
g.MetadataPrivacy.TryGet(lookupCtx, g.Created, out var created))
|
||||
profile.Append($"\n**Created on:** {created.FormatZoned(ctx.Zone)}");
|
||||
|
||||
if (opts.IncludeAvatar && g.IconFor(lookupCtx) is { } avatar)
|
||||
profile.Append($"\n**Avatar URL:** {avatar.TryGetCleanCdnUrl()}");
|
||||
|
||||
if (g.DescriptionFor(lookupCtx) is { } desc)
|
||||
profile.Append($"\n\n{desc}");
|
||||
|
||||
if (g.Visibility == PrivacyLevel.Private)
|
||||
profile.Append("\n*(this member is hidden)*");
|
||||
|
||||
eb.Field(new Embed.Field(g.NameFor(ctx), profile.ToString().Truncate(1024)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ using PluralKit.Core;
|
||||
#nullable enable
|
||||
namespace PluralKit.Bot;
|
||||
|
||||
public class MemberListOptions
|
||||
public class ListOptions
|
||||
{
|
||||
public SortProperty SortProperty { get; set; } = SortProperty.Name;
|
||||
public bool Reverse { get; set; }
|
||||
@@ -32,8 +32,8 @@ public class MemberListOptions
|
||||
if (SortProperty != SortProperty.Random) str.Append("by ");
|
||||
str.Append(SortProperty switch
|
||||
{
|
||||
SortProperty.Name => "member name",
|
||||
SortProperty.Hid => "member ID",
|
||||
SortProperty.Name => "name",
|
||||
SortProperty.Hid => "ID",
|
||||
SortProperty.DisplayName => "display name",
|
||||
SortProperty.CreationDate => "creation date",
|
||||
SortProperty.LastMessage => "last message",
|
||||
@@ -52,8 +52,8 @@ public class MemberListOptions
|
||||
|
||||
str.Append(PrivacyFilter switch
|
||||
{
|
||||
null => ", showing all members",
|
||||
PrivacyLevel.Private => ", showing only private members",
|
||||
null => ", showing all items",
|
||||
PrivacyLevel.Private => ", showing only private items",
|
||||
PrivacyLevel.Public => "", // (default, no extra line needed)
|
||||
_ => new ArgumentOutOfRangeException(
|
||||
$"Couldn't find readable string for privacy filter {PrivacyFilter}")
|
||||
@@ -62,7 +62,7 @@ public class MemberListOptions
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
public DatabaseViewsExt.MemberListQueryOptions ToQueryOptions() =>
|
||||
public DatabaseViewsExt.ListQueryOptions ToQueryOptions() =>
|
||||
new()
|
||||
{
|
||||
PrivacyFilter = PrivacyFilter,
|
||||
@@ -72,10 +72,10 @@ public class MemberListOptions
|
||||
};
|
||||
}
|
||||
|
||||
public static class MemberListOptionsExt
|
||||
public static class ListOptionsExt
|
||||
{
|
||||
public static IEnumerable<ListedMember> SortByMemberListOptions(this IEnumerable<ListedMember> input,
|
||||
MemberListOptions opts, LookupContext ctx)
|
||||
ListOptions opts, LookupContext ctx)
|
||||
{
|
||||
IComparer<T> ReverseMaybe<T>(IComparer<T> c) =>
|
||||
opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c;
|
||||
@@ -89,26 +89,23 @@ public static class MemberListOptionsExt
|
||||
// We want nulls last no matter what, even if orders are reversed
|
||||
SortProperty.Hid => input.OrderBy(m => m.Hid, ReverseMaybe(culture)),
|
||||
SortProperty.Name => input.OrderBy(m => m.NameFor(ctx), ReverseMaybe(culture)),
|
||||
SortProperty.CreationDate => input
|
||||
.OrderByDescending(m => m.MetadataPrivacy.CanAccess(ctx))
|
||||
.ThenBy(m => m.MetadataPrivacy.Get(ctx, m.Created, default), ReverseMaybe(Comparer<Instant>.Default)),
|
||||
SortProperty.MessageCount => input
|
||||
.OrderByDescending(m => m.MessageCount != 0 && m.MetadataPrivacy.CanAccess(ctx))
|
||||
.ThenByDescending(m => m.MetadataPrivacy.Get(ctx, m.MessageCount, 0), ReverseMaybe(Comparer<int>.Default)),
|
||||
SortProperty.CreationDate => input.OrderBy(m => m.Created, ReverseMaybe(Comparer<Instant>.Default)),
|
||||
SortProperty.MessageCount => input.OrderByDescending(m => m.MessageCount,
|
||||
ReverseMaybe(Comparer<int>.Default)),
|
||||
SortProperty.DisplayName => input
|
||||
.OrderByDescending(m => m.DisplayName != null && m.NamePrivacy.CanAccess(ctx))
|
||||
.ThenBy(m => m.NamePrivacy.Get(ctx, m.DisplayName), ReverseMaybe(culture)),
|
||||
.OrderByDescending(m => m.DisplayName != null)
|
||||
.ThenBy(m => m.DisplayName, ReverseMaybe(culture)),
|
||||
SortProperty.Birthdate => input
|
||||
.OrderByDescending(m => m.AnnualBirthday.HasValue && m.BirthdayPrivacy.CanAccess(ctx))
|
||||
.ThenBy(m => m.BirthdayPrivacy.Get(ctx, m.AnnualBirthday), ReverseMaybe(Comparer<AnnualDate?>.Default)),
|
||||
.OrderByDescending(m => m.AnnualBirthday.HasValue)
|
||||
.ThenBy(m => m.AnnualBirthday, ReverseMaybe(Comparer<AnnualDate?>.Default)),
|
||||
SortProperty.LastMessage => throw new PKError(
|
||||
"Sorting by last message is temporarily disabled due to database issues, sorry."),
|
||||
// SortProperty.LastMessage => input
|
||||
// .OrderByDescending(m => m.LastMessage.HasValue)
|
||||
// .ThenByDescending(m => m.LastMessage, ReverseMaybe(Comparer<ulong?>.Default)),
|
||||
SortProperty.LastSwitch => input
|
||||
.OrderByDescending(m => m.LastSwitchTime.HasValue && m.MetadataPrivacy.CanAccess(ctx))
|
||||
.ThenByDescending(m => m.MetadataPrivacy.Get(ctx, m.LastSwitchTime), ReverseMaybe(Comparer<Instant?>.Default)),
|
||||
.OrderByDescending(m => m.LastSwitchTime.HasValue)
|
||||
.ThenByDescending(m => m.LastSwitchTime, ReverseMaybe(Comparer<Instant?>.Default)),
|
||||
SortProperty.Random => input
|
||||
.OrderBy(m => randGen.Next()),
|
||||
_ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}")
|
||||
@@ -116,6 +113,33 @@ public static class MemberListOptionsExt
|
||||
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
|
||||
.ThenBy(m => m.NameFor(ctx), culture);
|
||||
}
|
||||
|
||||
public static IEnumerable<ListedGroup> SortByGroupListOptions(this IEnumerable<ListedGroup> input,
|
||||
ListOptions opts, LookupContext ctx)
|
||||
{
|
||||
IComparer<T> ReverseMaybe<T>(IComparer<T> c) =>
|
||||
opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c;
|
||||
|
||||
var randGen = new global::System.Random();
|
||||
|
||||
var culture = StringComparer.InvariantCultureIgnoreCase;
|
||||
return (opts.SortProperty switch
|
||||
{
|
||||
// As for the OrderByDescending HasValue calls: https://www.jerriepelser.com/blog/orderby-with-null-values/
|
||||
// We want nulls last no matter what, even if orders are reversed
|
||||
SortProperty.Hid => input.OrderBy(g => g.Hid, ReverseMaybe(culture)),
|
||||
SortProperty.Name => input.OrderBy(g => g.NameFor(ctx), ReverseMaybe(culture)),
|
||||
SortProperty.CreationDate => input.OrderBy(g => g.Created, ReverseMaybe(Comparer<Instant>.Default)),
|
||||
SortProperty.DisplayName => input
|
||||
.OrderByDescending(g => g.DisplayName != null)
|
||||
.ThenBy(g => g.DisplayName, ReverseMaybe(culture)),
|
||||
SortProperty.Random => input
|
||||
.OrderBy(g => randGen.Next()),
|
||||
_ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}")
|
||||
})
|
||||
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
|
||||
.ThenBy(m => m.NameFor(ctx), culture);
|
||||
}
|
||||
}
|
||||
|
||||
public enum SortProperty
|
Reference in New Issue
Block a user