diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadActivityUpdater.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadActivityUpdater.java index 4b21fb37e8..5cf11b83b8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadActivityUpdater.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadActivityUpdater.java @@ -1,8 +1,11 @@ package org.togetherjava.tjbot.commands.help; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageHistory; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; @@ -13,6 +16,7 @@ import org.togetherjava.tjbot.commands.Routine; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Map; import java.util.Optional; @@ -30,8 +34,14 @@ public final class HelpThreadActivityUpdater implements Routine { private static final Logger logger = LoggerFactory.getLogger(HelpThreadActivityUpdater.class); private static final int SCHEDULE_MINUTES = 30; private static final int ACTIVITY_DETERMINE_MESSAGE_LIMIT = 11; - + private static final int CHANNEL_ACTIVITY_CACHE_LIFETIME = 12; + private static final ChronoUnit CHANNEL_ACTIVITY_CACHE_LIFETIME_UNIT = ChronoUnit.HOURS; private final HelpSystemHelper helper; + public static final Cache manuallyResetChannelActivityCache = Caffeine.newBuilder() + .maximumSize(1_000) + .expireAfterWrite(CHANNEL_ACTIVITY_CACHE_LIFETIME, + TimeUnit.of(CHANNEL_ACTIVITY_CACHE_LIFETIME_UNIT)) + .build(); /** * Creates a new instance. @@ -78,7 +88,8 @@ private void updateActivityForThread(ThreadChannel threadChannel) { private static RestAction determineActivity( MessageChannel channel) { - return channel.getHistory().retrievePast(ACTIVITY_DETERMINE_MESSAGE_LIMIT).map(messages -> { + + return getRelevantHistory(channel).map(messages -> { if (messages.size() >= ACTIVITY_DETERMINE_MESSAGE_LIMIT) { // There are likely even more messages, but we hit the limit return HelpSystemHelper.ThreadActivity.HIGH; @@ -97,6 +108,16 @@ private static RestAction determineActivity( }); } + private static RestAction> getRelevantHistory(MessageChannel channel) { + Long mostRecentMessageId = + manuallyResetChannelActivityCache.getIfPresent(channel.getIdLong()); + + return mostRecentMessageId != null + ? channel.getHistoryAfter(mostRecentMessageId, ACTIVITY_DETERMINE_MESSAGE_LIMIT) + .map(MessageHistory::getRetrievedHistory) + : channel.getHistory().retrievePast(ACTIVITY_DETERMINE_MESSAGE_LIMIT); + } + private static boolean isBotMessage(Message message) { return message.getAuthor().equals(message.getJDA().getSelfUser()); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java index 5ca8c3eab6..675c03c092 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java @@ -3,10 +3,7 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.InteractionHook; @@ -31,6 +28,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.togetherjava.tjbot.commands.help.HelpThreadActivityUpdater.manuallyResetChannelActivityCache; + /** * Implements the {@code /help-thread} command, used to maintain certain aspects of help threads, * such as renaming or closing them. @@ -42,6 +41,8 @@ public final class HelpThreadCommand extends SlashCommandAdapter { private static final String CHANGE_CATEGORY_OPTION = "category"; public static final String CHANGE_TITLE_SUBCOMMAND = "title"; private static final String CHANGE_TITLE_OPTION = "title"; + private static final String CLOSE_SUBCOMMAND = "close"; + private static final String RESET_ACTIVITY_SUBCOMMAND = "reset-activity"; public static final String CHANGE_SUBCOMMAND_GROUP = "change"; public static final String COMMAND_NAME = "help-thread"; @@ -80,6 +81,7 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) { getData().addSubcommandGroups(changeCommands); getData().addSubcommands(Subcommand.CLOSE.toSubcommandData()); + getData().addSubcommands(Subcommand.RESET_ACTIVITY.toSubcommandData()); this.helper = helper; @@ -92,9 +94,9 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) { subcommandToCooldownCache = new EnumMap<>(streamSubcommands() .filter(Subcommand::hasCooldown) .collect(Collectors.toMap(Function.identity(), any -> createCooldownCache.get()))); - subcommandToEventHandler = new EnumMap<>( - Map.of(Subcommand.CHANGE_CATEGORY, this::changeCategory, Subcommand.CHANGE_TITLE, - this::changeTitle, Subcommand.CLOSE, this::closeThread)); + subcommandToEventHandler = new EnumMap<>(Map.of(Subcommand.CHANGE_CATEGORY, + this::changeCategory, Subcommand.CHANGE_TITLE, this::changeTitle, Subcommand.CLOSE, + this::closeThread, Subcommand.RESET_ACTIVITY, this::resetActivity)); } @Override @@ -198,6 +200,19 @@ private void closeThread(SlashCommandInteractionEvent event, ThreadChannel helpT event.replyEmbeds(embed).flatMap(any -> helpThread.getManager().setArchived(true)).queue(); } + private void resetActivity(SlashCommandInteractionEvent event, ThreadChannel helpThread) { + refreshCooldownFor(Subcommand.RESET_ACTIVITY, helpThread); + + helpThread.getHistory() + .retrievePast(1) + .map(messages -> messages.get(0)) + .queue(lastMessage -> manuallyResetChannelActivityCache.put(helpThread.getIdLong(), + lastMessage.getIdLong())); + + helper.changeChannelActivity(helpThread, HelpSystemHelper.ThreadActivity.LOW); + event.reply("Activities have been reset.").queue(); + } + private static Stream streamSubcommands() { return Arrays.stream(Subcommand.values()); } @@ -206,7 +221,10 @@ enum Subcommand { CHANGE_CATEGORY(CHANGE_CATEGORY_SUBCOMMAND, "Change the category of this help thread", Cooldown.YES), CHANGE_TITLE(CHANGE_TITLE_SUBCOMMAND, "Change the title of this help thread", Cooldown.YES), - CLOSE("close", "Close this help thread", Cooldown.YES); + CLOSE(CLOSE_SUBCOMMAND, "Close this help thread", Cooldown.YES), + RESET_ACTIVITY(RESET_ACTIVITY_SUBCOMMAND, + "Resets the activity indicator, use if help is still needed, but the indicator shows otherwise", + Cooldown.YES); private final String commandName; private final String description;