Skip to content

Commit d219d0e

Browse files
authored
Support Video Preview in RSS Feeds #1324
1 parent b905efb commit d219d0e

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

application/config.json.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
}
188188
],
189189
"fallbackChannelPattern": "java-news-and-changes",
190+
"videoLinkPattern": "http(s)?://www\\.youtube.com.*",
190191
"pollIntervalInMinutes": 10
191192
},
192193
"memberCountCategoryPattern": "Info",

application/src/main/java/org/togetherjava/tjbot/config/RSSFeedsConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616
public record RSSFeedsConfig(@JsonProperty(value = "feeds", required = true) List<RSSFeed> feeds,
1717
@JsonProperty(value = "fallbackChannelPattern",
1818
required = true) String fallbackChannelPattern,
19+
@JsonProperty(value = "videoLinkPattern", required = true) String videoLinkPattern,
1920
@JsonProperty(value = "pollIntervalInMinutes", required = true) int pollIntervalInMinutes) {
2021

2122
/**
2223
* Constructs a new {@link RSSFeedsConfig}.
2324
*
2425
* @param feeds The list of RSS feeds to subscribe to.
2526
* @param fallbackChannelPattern The pattern used to identify the fallback text channel to use.
27+
* @param videoLinkPattern pattern determining if a link is a video. It is then posted in a way
28+
* to support Discord video embeds.
2629
* @param pollIntervalInMinutes The interval (in minutes) for polling the RSS feeds for updates.
2730
* @throws NullPointerException if any of the parameters (feeds or fallbackChannelPattern) are
2831
* null
2932
*/
3033
public RSSFeedsConfig {
3134
Objects.requireNonNull(feeds);
3235
Objects.requireNonNull(fallbackChannelPattern);
36+
Objects.requireNonNull(videoLinkPattern);
3337
}
3438
}

application/src/main/java/org/togetherjava/tjbot/features/rss/RSSHandlerRoutine.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.apptasticsoftware.rssreader.RssReader;
55
import net.dv8tion.jda.api.EmbedBuilder;
66
import net.dv8tion.jda.api.JDA;
7-
import net.dv8tion.jda.api.entities.MessageEmbed;
87
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
98
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
9+
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
1010
import org.apache.commons.text.StringEscapeUtils;
1111
import org.jetbrains.annotations.Nullable;
1212
import org.jooq.tools.StringUtils;
@@ -79,6 +79,7 @@ public final class RSSHandlerRoutine implements Routine {
7979
private final RssReader rssReader;
8080
private final RSSFeedsConfig config;
8181
private final Predicate<String> fallbackChannelPattern;
82+
private final Predicate<String> isVideoLink;
8283
private final Map<RSSFeed, Predicate<String>> targetChannelPatterns;
8384
private final int interval;
8485
private final Database database;
@@ -95,6 +96,7 @@ public RSSHandlerRoutine(Config config, Database database) {
9596
this.database = database;
9697
this.fallbackChannelPattern =
9798
Pattern.compile(this.config.fallbackChannelPattern()).asMatchPredicate();
99+
isVideoLink = Pattern.compile(this.config.videoLinkPattern()).asMatchPredicate();
98100
this.targetChannelPatterns = new HashMap<>();
99101
this.config.feeds().forEach(feed -> {
100102
if (feed.targetChannelPattern() != null) {
@@ -155,7 +157,7 @@ private void sendRSS(JDA jda, RSSFeed feedConfig) {
155157
}
156158
rssItems.reversed()
157159
.stream()
158-
.filter(shouldItemBePosted.get())
160+
.filter(shouldItemBePosted.orElseThrow())
159161
.forEachOrdered(item -> postItem(textChannels, item, feedConfig));
160162
}
161163

@@ -241,8 +243,8 @@ private Optional<ZonedDateTime> getLatestPostDateFromItems(List<Item> items,
241243
* @param feedConfig the RSS feed configuration
242244
*/
243245
private void postItem(List<TextChannel> textChannels, Item rssItem, RSSFeed feedConfig) {
244-
MessageEmbed embed = constructEmbedMessage(rssItem, feedConfig).build();
245-
textChannels.forEach(channel -> channel.sendMessageEmbeds(List.of(embed)).queue());
246+
MessageCreateData message = constructMessage(rssItem, feedConfig);
247+
textChannels.forEach(channel -> channel.sendMessage(message).queue());
246248
}
247249

248250
/**
@@ -346,13 +348,18 @@ private List<TextChannel> getTextChannelsFromFeed(JDA jda, RSSFeed feed) {
346348
}
347349

348350
/**
349-
* Provides the {@link EmbedBuilder} from an RSS item used for sending RSS posts.
351+
* Provides the message from an RSS item used for sending RSS posts.
350352
*
351353
* @param item the RSS item to construct the embed message from
352354
* @param feedConfig the configuration of the RSS feed
353-
* @return the constructed {@link EmbedBuilder} containing information from the RSS item
355+
* @return the constructed message containing information from the RSS item
354356
*/
355-
private static EmbedBuilder constructEmbedMessage(Item item, RSSFeed feedConfig) {
357+
private MessageCreateData constructMessage(Item item, RSSFeed feedConfig) {
358+
if (item.getLink().filter(isVideoLink).isPresent()) {
359+
// Automatic video previews are created on normal messages, not on embeds
360+
return MessageCreateData.fromContent(item.getLink().orElseThrow());
361+
}
362+
356363
final EmbedBuilder embedBuilder = new EmbedBuilder();
357364
String title = item.getTitle().orElse("No title");
358365
String titleLink = item.getLink().orElse("");
@@ -381,7 +388,7 @@ private static EmbedBuilder constructEmbedMessage(Item item, RSSFeed feedConfig)
381388
embedBuilder.setDescription("No description");
382389
}
383390

384-
return embedBuilder;
391+
return MessageCreateData.fromEmbeds(embedBuilder.build());
385392
}
386393

387394
/**

0 commit comments

Comments
 (0)