Skip to content

Commit

Permalink
feat(rss): add parameter to include only important items
Browse files Browse the repository at this point in the history
Fixes #28, at least somewhat
  • Loading branch information
RafDevX committed Sep 12, 2024
1 parent c2d6e72 commit f9f39f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:

web:
build: .
ports:
- 3000:3000
environment:
JDBC_DATABASE_URL: "jdbc:postgresql://db:5432/postgres"
JDBC_DATABASE_USERNAME: postgres
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package se.datasektionen.calypso.controllers.api;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import se.datasektionen.calypso.feeds.IcalFeed;
import se.datasektionen.calypso.feeds.rss.RssFeeds;
Expand All @@ -30,13 +32,13 @@ public String eventFeed() {
}

@RequestMapping(produces = "application/rss+xml", method = RequestMethod.GET, value = "/rss")
public RssView rssFeedSwedish() {
return rssFeeds.swedishFeed();
public RssView rssFeedSwedish(@RequestParam Optional<Boolean> important) {
return rssFeeds.swedishFeed(important.orElse(false));
}

@RequestMapping(produces = "application/rss+xml", method = RequestMethod.GET, value = "/rss_en")
public RssView rssFeedEnglish() {
return rssFeeds.englishFeed();
public RssView rssFeedEnglish(@RequestParam Optional<Boolean> important) {
return rssFeeds.englishFeed(important.orElse(false));
}

}
39 changes: 22 additions & 17 deletions src/main/java/se/datasektionen/calypso/feeds/rss/RssFeeds.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,54 @@ public class RssFeeds {
private final Darkmode darkmode;
private final Config config;

public RssView swedishFeed() {
return new RssView(fetchAndConvertSwedishItems(),
public RssView swedishFeed(boolean important) {
return new RssView(fetchAndConvertSwedishItems(important),
config.getBaseUrl() + RssConstants.Swedish.FEED_URL,
RssConstants.Swedish.TITLE,
RssConstants.Swedish.DESCRIPTION);
}

public RssView englishFeed() {
return new RssView(fetchAndConvertEnglishItems(),
public RssView englishFeed(boolean important) {
return new RssView(fetchAndConvertEnglishItems(important),
config.getBaseUrl() + RssConstants.English.FEED_URL,
RssConstants.English.TITLE,
RssConstants.English.DESCRIPTION);
}

private List<com.rometools.rome.feed.rss.Item> fetchAndConvertSwedishItems() {
return fetchAndConvertItems(Item::getTitleSwedish,
private List<com.rometools.rome.feed.rss.Item> fetchAndConvertSwedishItems(boolean important) {
return fetchAndConvertItems(important,
Item::getTitleSwedish,
Item::getAuthorDisplay,
Item::getContentSwedishProcessed,
i -> RssConstants.Swedish.LINKER.apply(i.getId()));
}

private List<com.rometools.rome.feed.rss.Item> fetchAndConvertEnglishItems() {
return fetchAndConvertItems(Item::getTitleEnglish,
private List<com.rometools.rome.feed.rss.Item> fetchAndConvertEnglishItems(boolean important) {
return fetchAndConvertItems(important,
Item::getTitleEnglish,
Item::getAuthorDisplay,
Item::getContentEnglishProcessed,
i -> RssConstants.English.LINKER.apply(i.getId()));
}

private List<com.rometools.rome.feed.rss.Item> fetchAndConvertItems(Function<Item, String> titleMapper,
private List<com.rometools.rome.feed.rss.Item> fetchAndConvertItems(boolean important,
Function<Item, String> titleMapper,
Function<Item, String> authorMapper,
Function<Item, String> contentMapper,
Function<Item, String> linkMapper) {

Stream<Item> sensitive = apiRepository
.findAllPublished(PageRequest.of(0, 100, Sort.by(Sort.Direction.DESC, "publishDate")))
.getContent()
.stream()
.filter(i -> !i.isSensitive());
Stream<Item> all = apiRepository
Stream<Item> items = apiRepository
.findAllPublished(PageRequest.of(0, 100, Sort.by(Sort.Direction.DESC, "publishDate")))
.getContent()
.stream();
return (darkmode.getCurrent() ? sensitive : all)

if (darkmode.getCurrent()) {
items = items.filter(i -> !i.isSensitive());
}
if (important) {
items = items.filter(Item::isSticky);
}

return items
.map(i -> RssConverter.toRssItem(i, titleMapper, authorMapper, contentMapper, linkMapper))
.collect(Collectors.toList());
}
Expand Down

0 comments on commit f9f39f8

Please sign in to comment.