diff --git a/core/src/main/groovy/com/muwire/core/filefeeds/FeedManager.groovy b/core/src/main/groovy/com/muwire/core/filefeeds/FeedManager.groovy index 72f3eab1..22cb0f4d 100644 --- a/core/src/main/groovy/com/muwire/core/filefeeds/FeedManager.groovy +++ b/core/src/main/groovy/com/muwire/core/filefeeds/FeedManager.groovy @@ -129,11 +129,26 @@ class FeedManager { Set set = feedItems.get(publisher) if (set == null) return // can happen if nothing was published - - File itemsFile = new File(itemsFolder, publisher.destination.toBase32() + ".json") + Feed feed = feeds[publisher] + if (feed == null) { + log.severe("Persisting items for non-existing feed " + publisher.getHumanReadableName()) + return + } + + List list = new ArrayList<>(set) + if (list.size() > feed.getItemsToKeep()) { + log.info("will persist ${feed.getItemsToKeep()}/${list.size()} items") + list.sort({l, r -> + Long.compare(r.getTimestamp(), l.getTimestamp()) + } as Comparator) + list = list[0..feed.getItemsToKeep()] + } + + + File itemsFile = new File(itemsFolder, publisher.destination.toBase32() + ".json") itemsFile.withPrintWriter { writer -> - set.each { item -> + list.each { item -> def obj = FeedItems.feedItemToObj(item) def json = JsonOutput.toJson(obj) writer.println(json)