Skip to content

Commit c7abeef

Browse files
nateweiszchristolis
authored andcommitted
fix: now correctly finds the latest date
1 parent b3d4287 commit c7abeef

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

application/src/main/java/org/togetherjava/tjbot/features/javamail/JavaMailRSSRoutine.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.dv8tion.jda.api.entities.MessageEmbed;
88
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
99
import org.apache.commons.text.StringEscapeUtils;
10+
import org.jetbrains.annotations.Nullable;
1011
import org.jooq.tools.StringUtils;
1112
import org.jsoup.Jsoup;
1213
import org.jsoup.nodes.Document;
@@ -88,7 +89,12 @@ private void sendRSS(JDA jda, RSSFeed feed) {
8889
textChannel.get().sendMessageEmbeds(List.of(embed)).queue();
8990
});
9091

91-
String lastDate = items.getFirst().getPubDate().orElseThrow();
92+
String lastDate = getLatestDate(items);
93+
94+
if (lastDate == null) {
95+
return;
96+
}
97+
9298
if (entry == null) {
9399
// Insert
94100
database.write(context -> context.newRecord(RSS_FEED)
@@ -104,6 +110,26 @@ private void sendRSS(JDA jda, RSSFeed feed) {
104110
.executeAsync());
105111
}
106112

113+
@Nullable
114+
private static String getLatestDate(List<Item> items) {
115+
String lastDate = null;
116+
117+
for (Item item : items) {
118+
if (lastDate == null) {
119+
lastDate = item.getPubDate().orElseThrow();
120+
continue;
121+
}
122+
123+
LocalDateTime formattedLastDate = getLocalDateTime(lastDate);
124+
LocalDateTime itemDate = getLocalDateTime(item.getPubDate().orElseThrow());
125+
126+
if (itemDate.isAfter(formattedLastDate)) {
127+
lastDate = item.getPubDate().orElseThrow();
128+
}
129+
}
130+
return lastDate;
131+
}
132+
107133
private Optional<TextChannel> getTextChannelFromFeed(JDA jda, RSSFeed feed) {
108134
return jda.getTextChannelCache()
109135
.stream()

0 commit comments

Comments
 (0)