From 5ae0055fdc31610199d06631c651fcd9c2fab002 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 13 Dec 2021 11:48:26 +0100 Subject: [PATCH] Added type-to-count table to audit summary - and added some missing NotNulls in AuditCommand --- .../commands/moderation/AuditCommand.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 1b756927d2..e33f3feded 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -16,12 +16,10 @@ import org.togetherjava.tjbot.config.Config; import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.function.Predicate; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * This command lists all moderation actions that have been taken against a given user, for example @@ -54,20 +52,39 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore) { this.actionsStore = Objects.requireNonNull(actionsStore); } - private static MessageEmbed createSummaryMessage(@NotNull User user, + private static @NotNull MessageEmbed createSummaryMessage(@NotNull User user, @NotNull Collection actions) { - int actionAmount = actions.size(); - String description = actionAmount == 0 ? "There are **no actions** against the user." - : "There are **%d actions** against the user.".formatted(actionAmount); - return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag())) .setAuthor(user.getName(), null, user.getAvatarUrl()) - .setDescription(description) + .setDescription(createSummaryMessageDescription(actions)) .setColor(ModerationUtils.AMBIENT_COLOR) .build(); } - private static RestAction actionToMessage(@NotNull ActionRecord action, + private static @NotNull String createSummaryMessageDescription( + @NotNull Collection actions) { + int actionAmount = actions.size(); + if (actionAmount == 0) { + return "There are **no actions** against the user."; + } + + String shortSummary = "There are **%d actions** against the user.".formatted(actionAmount); + + // Summary of all actions with their count, like "- Warn: 5", descending + Map actionTypeToCount = actions.stream() + .collect(Collectors.groupingBy(ActionRecord::actionType, Collectors.counting())); + String typeCountSummary = actionTypeToCount.entrySet() + .stream() + .filter(typeAndCount -> typeAndCount.getValue() > 0) + .sorted(Map.Entry.comparingByValue().reversed()) + .map(typeAndCount -> "- **%s**: %d".formatted(typeAndCount.getKey(), + typeAndCount.getValue())) + .collect(Collectors.joining("\n")); + + return shortSummary + "\n" + typeCountSummary; + } + + private static @NotNull RestAction actionToMessage(@NotNull ActionRecord action, @NotNull JDA jda) { String footer = action.actionExpiresAt() == null ? null : "Temporary action, expires at %s".formatted(TimeUtil @@ -84,7 +101,8 @@ private static RestAction actionToMessage(@NotNull ActionRecord ac .build()); } - private static List prependElement(E element, Collection elements) { + private static @NotNull List prependElement(@NotNull E element, + @NotNull Collection elements) { List allElements = new ArrayList<>(elements.size() + 1); allElements.add(element); allElements.addAll(elements);