From ab66e152de7a5fb000c5d917aa9138a49d35f319 Mon Sep 17 00:00:00 2001 From: christolis Date: Tue, 16 Apr 2024 18:43:41 +0300 Subject: [PATCH] fix(rss): logic for acquiring target channels This commit attempts to change the way a text channel list for RSS feeds gets generated, as it would originally not consider any channels that matched the fallback channel pattern and it would constantly log a warning that would clutter the log channels and skip sending any RSS posts as a result. The method which is responsible for finding the text channels from a given RSS feed configuration now focuses on collecting a list of text channels that match the target channel pattern from the configuration, and if no channels are found, the same collection attempt happens with the fallback channel pattern. In case an empty list is still yielded, a now-improved and more accurate warning message gets logged. --- .../features/javamail/RSSHandlerRoutine.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/javamail/RSSHandlerRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/javamail/RSSHandlerRoutine.java index 7edc42f7ef..e114f53a61 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/javamail/RSSHandlerRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/javamail/RSSHandlerRoutine.java @@ -6,6 +6,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView; import org.apache.commons.text.StringEscapeUtils; import org.jetbrains.annotations.Nullable; import org.jooq.tools.StringUtils; @@ -125,8 +126,8 @@ public void runRoutine(@Nonnull JDA jda) { private void sendRSS(JDA jda, RSSFeed feedConfig) { List textChannels = getTextChannelsFromFeed(jda, feedConfig); if (textChannels.isEmpty()) { - logger.warn("Tried to send an RSS post, got empty response (channel {} not found)", - feedConfig.targetChannelPattern()); + logger.warn( + "Tried to send an RSS post, but neither a target channel nor a fallback channel was found."); return; } @@ -326,18 +327,18 @@ private static boolean isValidDateFormat(Item rssItem, RSSFeed feedConfig) { * @return an {@link List} of the text channels found, or empty if none are found */ private List getTextChannelsFromFeed(JDA jda, RSSFeed feed) { - // Attempt to find the target channel, use the fallback otherwise - if (targetChannelPatterns.containsKey(feed)) { - return jda.getTextChannelCache() - .stream() - .filter(channel -> targetChannelPatterns.get(feed).test(channel.getName())) - .toList(); - } else { - return jda.getTextChannelCache() - .stream() - .filter(channel -> fallbackChannelPattern.test(channel.getName())) - .toList(); + final SnowflakeCacheView textChannelCache = jda.getTextChannelCache(); + List textChannels = textChannelCache.stream() + .filter(channel -> targetChannelPatterns.get(feed).test(channel.getName())) + .toList(); + + if (!textChannels.isEmpty()) { + return textChannels; } + + return textChannelCache.stream() + .filter(channel -> fallbackChannelPattern.test(channel.getName())) + .toList(); } /**