From 42b70cde9a94a07d79bc08206d05e33b512f5f3b Mon Sep 17 00:00:00 2001 From: kittens Date: Sat, 12 Sep 2020 18:10:37 -0400 Subject: [PATCH] Add -all flag on system and group cards (#216) * Show group count on stats card * Add -all flag on system and group cards Shows full count, including private members. * fix stuff broken by merging conflicts --- PluralKit.Bot/Commands/Groups.cs | 2 +- PluralKit.Bot/Commands/Misc.cs | 3 ++- PluralKit.Bot/Commands/Privacy/ContextPrivacyExt.cs | 9 +++++++++ PluralKit.Bot/Commands/System.cs | 2 +- PluralKit.Bot/Services/EmbedService.cs | 9 +++++---- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 24f6b928..732e85f2 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -284,7 +284,7 @@ namespace PluralKit.Bot var system = await GetGroupSystem(ctx, target, conn); var pctx = ctx.LookupContextFor(system); - var memberCount = await _repo.GetGroupMemberCount(conn, target.Id, PrivacyLevel.Public); + var memberCount = ctx.MatchPrivateFlag(pctx) ? await _repo.GetGroupMemberCount(conn, target.Id, PrivacyLevel.Public) : await _repo.GetGroupMemberCount(conn, target.Id); var nameField = target.Name; if (system.Name != null) diff --git a/PluralKit.Bot/Commands/Misc.cs b/PluralKit.Bot/Commands/Misc.cs index 9b0337f0..da65284c 100644 --- a/PluralKit.Bot/Commands/Misc.cs +++ b/PluralKit.Bot/Commands/Misc.cs @@ -65,6 +65,7 @@ namespace PluralKit.Bot { var totalSystems = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SystemCount.Name)?.Value ?? 0; var totalMembers = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MemberCount.Name)?.Value ?? 0; + var totalGroups = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.GroupCount.Name)?.Value ?? 0; var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0; var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0; @@ -89,7 +90,7 @@ namespace PluralKit.Bot { .AddField("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true) .AddField("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true) .AddField("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true) - .AddField("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalSwitches:N0} switches, {totalMessages:N0} messages"); + .AddField("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages"); await msg.ModifyAsync("", embed.Build()); } diff --git a/PluralKit.Bot/Commands/Privacy/ContextPrivacyExt.cs b/PluralKit.Bot/Commands/Privacy/ContextPrivacyExt.cs index 2e9eb675..ba422e5e 100644 --- a/PluralKit.Bot/Commands/Privacy/ContextPrivacyExt.cs +++ b/PluralKit.Bot/Commands/Privacy/ContextPrivacyExt.cs @@ -44,5 +44,14 @@ namespace PluralKit.Bot ctx.PopArgument(); return subject; } + + public static bool MatchPrivateFlag(this Context ctx, LookupContext pctx) + { + var privacy = true; + if (ctx.MatchFlag("a", "all")) privacy = false; + if (pctx == LookupContext.ByNonOwner && !privacy) throw Errors.LookupNotAllowed; + + return privacy; + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/System.cs b/PluralKit.Bot/Commands/System.cs index 509f2b59..d531196d 100644 --- a/PluralKit.Bot/Commands/System.cs +++ b/PluralKit.Bot/Commands/System.cs @@ -20,7 +20,7 @@ namespace PluralKit.Bot public async Task Query(Context ctx, PKSystem system) { if (system == null) throw Errors.NoSystemError; - await ctx.Reply(embed: await _embeds.CreateSystemEmbed(ctx.Shard, system, ctx.LookupContextFor(system))); + await ctx.Reply(embed: await _embeds.CreateSystemEmbed(ctx, system, ctx.LookupContextFor(system))); } public async Task New(Context ctx) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index e0896e75..9ddb19e3 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -26,15 +26,16 @@ namespace PluralKit.Bot { _repo = repo; } - public async Task CreateSystemEmbed(DiscordClient client, PKSystem system, LookupContext ctx) + public async Task CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx) { await using var conn = await _db.Obtain(); // Fetch/render info for all accounts simultaneously var accounts = await _repo.GetSystemAccounts(conn, system.Id); - var users = await Task.WhenAll(accounts.Select(async uid => (await client.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})")); + var users = await Task.WhenAll(accounts.Select(async uid => (await cctx.Shard.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})")); + + var memberCount = cctx.MatchPrivateFlag(ctx) ? await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public) : await _repo.GetSystemMemberCount(conn, system.Id); - var memberCount = await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public); var eb = new DiscordEmbedBuilder() .WithColor(DiscordUtils.Gray) .WithTitle(system.Name ?? null) @@ -244,4 +245,4 @@ namespace PluralKit.Bot { return Task.FromResult(eb.Build()); } } -} \ No newline at end of file +}