feat: rework group list into member list

This commit is contained in:
rladenson
2022-01-14 22:30:02 -05:00
committed by spiral
parent 0afe031284
commit f3869dbcbe
21 changed files with 374 additions and 126 deletions

View File

@@ -64,7 +64,7 @@ public class GroupMember
var groups = await _repo.GetMemberGroups(target.Id)
.Where(g => g.Visibility.CanAccess(pctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
.OrderBy(g => (g.DisplayName ?? g.Name), StringComparer.InvariantCultureIgnoreCase)
.ToListAsync();
var description = "";
@@ -97,7 +97,7 @@ public class GroupMember
.ToList();
var existingMembersInGroup = (await _db.Execute(conn => conn.QueryMemberList(target.System,
new DatabaseViewsExt.MemberListQueryOptions { GroupFilter = target.Id })))
new DatabaseViewsExt.ListQueryOptions { GroupFilter = target.Id })))
.Select(m => m.Id.Value)
.Distinct()
.ToHashSet();
@@ -134,7 +134,7 @@ public class GroupMember
var targetSystem = await GetGroupSystem(ctx, target);
ctx.CheckSystemPrivacy(targetSystem.Id, target.ListPrivacy);
var opts = ctx.ParseMemberListOptions(ctx.DirectLookupContextFor(target.System));
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.System));
opts.GroupFilter = target.Id;
var title = new StringBuilder($"Members of {target.DisplayName ?? target.Name} (`{target.Hid}`) in ");

View File

@@ -175,6 +175,8 @@ public class Groups
await _repo.UpdateGroup(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Group display name cleared.");
if (target.NamePrivacy == PrivacyLevel.Private)
await ctx.Reply($"{Emojis.Warn} Since this group no longer has a display name set, their name privacy **can no longer take effect**.");
}
else
{
@@ -431,49 +433,33 @@ public class Groups
ctx.CheckSystemPrivacy(system.Id, system.GroupListPrivacy);
// TODO: integrate with the normal "search" system
// explanation of privacy lookup here:
// - 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));
await ctx.RenderGroupList(
ctx.LookupContextFor(system.Id),
system.Id,
GetEmbedTitle(system, opts),
system.Color,
opts
);
}
// TODO: integrate with privacy config settings
private string GetEmbedTitle(PKSystem target, ListOptions opts)
{
var title = new StringBuilder("Groups of ");
var pctx = LookupContext.ByNonOwner;
if (ctx.MatchFlag("a", "all"))
{
if (system.Id == ctx.System.Id)
pctx = LookupContext.ByOwner;
else
throw Errors.LookupNotAllowed;
}
if (target.Name != null)
title.Append($"{target.Name} (`{target.Hid}`)");
else
title.Append($"`{target.Hid}`");
var groups = (await _db.Execute(conn => conn.QueryGroupList(system.Id)))
.Where(g => g.Visibility.CanAccess(pctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
.ToList();
if (opts.Search != null)
title.Append($" matching **{opts.Search}**");
if (groups.Count == 0)
{
if (system.Id == ctx.System?.Id)
await ctx.Reply("This system has no groups. To create one, use the command `pk;group new <name>`.");
else
await ctx.Reply("This system has no groups.");
return;
}
var title = system.Name != null ? $"Groups of {system.Name} (`{system.Hid}`)" : $"Groups of `{system.Hid}`";
await ctx.Paginate(groups.ToAsyncEnumerable(), groups.Count, 25, title, system.Color, Renderer);
Task Renderer(EmbedBuilder eb, IEnumerable<ListedGroup> page)
{
eb.WithSimpleLineContent(page.Select(g =>
{
if (g.DisplayName != null)
return
$"[`{g.Hid}`] **{g.Name.EscapeMarkdown()}** ({g.DisplayName.EscapeMarkdown()}) ({"member".ToQuantity(g.MemberCount)})";
return $"[`{g.Hid}`] **{g.Name.EscapeMarkdown()}** ({"member".ToQuantity(g.MemberCount)})";
}));
eb.Footer(new Embed.EmbedFooter($"{groups.Count} total."));
return Task.CompletedTask;
}
return title.ToString();
}
public async Task ShowGroupCard(Context ctx, PKGroup target)
@@ -490,12 +476,14 @@ public class Groups
{
await ctx.Reply(embed: new EmbedBuilder()
.Title($"Current privacy settings for {target.Name}")
.Field(new Embed.Field("Name", target.NamePrivacy.Explanation()))
.Field(new Embed.Field("Description", target.DescriptionPrivacy.Explanation()))
.Field(new Embed.Field("Icon", target.IconPrivacy.Explanation()))
.Field(new Embed.Field("Member list", target.ListPrivacy.Explanation()))
.Field(new Embed.Field("Metadata (creation date)", target.MetadataPrivacy.Explanation()))
.Field(new Embed.Field("Visibility", target.Visibility.Explanation()))
.Description(
$"To edit privacy settings, use the command:\n> pk;group **{target.Reference()}** privacy **<subject>** **<level>**\n\n- `subject` is one of `description`, `icon`, `members`, `visibility`, or `all`\n- `level` is either `public` or `private`.")
$"To edit privacy settings, use the command:\n> pk;group **{target.Reference()}** privacy **<subject>** **<level>**\n\n- `subject` is one of `name`, `description`, `icon`, `members`, `metadata`, `visibility`, or `all`\n- `level` is either `public` or `private`.")
.Build());
return;
}
@@ -518,30 +506,40 @@ public class Groups
var subjectName = subject switch
{
GroupPrivacySubject.Name => "name privacy",
GroupPrivacySubject.Description => "description privacy",
GroupPrivacySubject.Icon => "icon privacy",
GroupPrivacySubject.List => "member list",
GroupPrivacySubject.Metadata => "metadata",
GroupPrivacySubject.Visibility => "visibility",
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
var explanation = (subject, level) switch
{
(GroupPrivacySubject.Name, PrivacyLevel.Private) =>
"This group's name is now hidden from other systems, and will be replaced by the group's display name.",
(GroupPrivacySubject.Description, PrivacyLevel.Private) =>
"This group's description is now hidden from other systems.",
(GroupPrivacySubject.Icon, PrivacyLevel.Private) =>
"This group's icon is now hidden from other systems.",
(GroupPrivacySubject.Visibility, PrivacyLevel.Private) =>
"This group is now hidden from group lists and member cards.",
(GroupPrivacySubject.Metadata, PrivacyLevel.Private) =>
"This group's metadata (eg. creation date) is now hidden from other systems.",
(GroupPrivacySubject.List, PrivacyLevel.Private) =>
"This group's member list is now hidden from other systems.",
(GroupPrivacySubject.Name, PrivacyLevel.Public) =>
"This group's name is no longer hidden from other systems.",
(GroupPrivacySubject.Description, PrivacyLevel.Public) =>
"This group's description is no longer hidden from other systems.",
(GroupPrivacySubject.Icon, PrivacyLevel.Public) =>
"This group's icon is no longer hidden from other systems.",
(GroupPrivacySubject.Visibility, PrivacyLevel.Public) =>
"This group is no longer hidden from group lists and member cards.",
(GroupPrivacySubject.Metadata, PrivacyLevel.Public) =>
"This group's metadata (eg. creation date) is no longer hidden from other systems.",
(GroupPrivacySubject.List, PrivacyLevel.Public) =>
"This group's member list is no longer hidden from other systems.",
@@ -550,6 +548,10 @@ public class Groups
await ctx.Reply(
$"{Emojis.Success} {target.Name}'s **{subjectName}** has been set to **{level.LevelName()}**. {explanation}");
if (subject == GroupPrivacySubject.Name && level == PrivacyLevel.Private && target.DisplayName == null)
await ctx.Reply(
$"{Emojis.Warn} This group does not have a display name set, and name privacy **will not take effect**.");
}
if (ctx.Match("all") || newValueFromCommand != null)

View File

@@ -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)));
}
}
}
}

View File

@@ -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

View File

@@ -415,7 +415,10 @@ public class MemberEdit
await _repo.UpdateMember(target.Id, patch);
await PrintSuccess(
$"{Emojis.Success} Member display name cleared. This member will now be proxied using their member name \"{target.NameFor(ctx)}\".");
$"{Emojis.Success} Member display name cleared. This member will now be proxied using their member name \"{target.Name}\".");
if (target.NamePrivacy == PrivacyLevel.Private)
await ctx.Reply($"{Emojis.Warn} Since this member no longer has a display name set, their name privacy **can no longer take effect**.");
}
else
{

View File

@@ -41,9 +41,9 @@ public class Random
{
ctx.CheckSystem();
var groups = await _db.Execute(c => c.QueryGroupList(ctx.System.Id));
var groups = await _repo.GetSystemGroups(ctx.System.Id).ToListAsync();
if (!ctx.MatchFlag("all", "a"))
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public);
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public).ToList();
if (groups == null || !groups.Any())
throw new PKError(
@@ -57,7 +57,7 @@ public class Random
{
ctx.CheckOwnGroup(group);
var opts = ctx.ParseMemberListOptions(ctx.DirectLookupContextFor(group.System));
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(group.System));
opts.GroupFilter = group.Id;
await using var conn = await _db.Obtain();

View File

@@ -12,10 +12,10 @@ public class SystemList
ctx.CheckSystemPrivacy(target.Id, target.MemberListPrivacy);
// explanation of privacy lookup here:
// - ParseMemberListOptions checks list access privacy and sets the privacy filter (which members show up in list)
// - 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.ParseMemberListOptions(ctx.DirectLookupContextFor(target.Id));
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.Id));
await ctx.RenderMemberList(
ctx.LookupContextFor(target.Id),
target.Id,
@@ -25,7 +25,7 @@ public class SystemList
);
}
private string GetEmbedTitle(PKSystem target, MemberListOptions opts)
private string GetEmbedTitle(PKSystem target, ListOptions opts)
{
var title = new StringBuilder("Members of ");