From 26ceaafc66b3b9d288fd505974a5e3cd7bd6b608 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Thu, 11 Jan 2024 08:28:37 -0500 Subject: [PATCH] Add recent comments RSS feed --- .../kotlin/info/mechyrdia/Factbooks.kt | 4 ++ .../info/mechyrdia/data/views_comment.kt | 2 +- .../kotlin/info/mechyrdia/lore/view_nav.kt | 1 + .../kotlin/info/mechyrdia/lore/views_rss.kt | 64 ++++++++++++++++++- 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/jvmMain/kotlin/info/mechyrdia/Factbooks.kt b/src/jvmMain/kotlin/info/mechyrdia/Factbooks.kt index 9892827..f2f6f8e 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/Factbooks.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/Factbooks.kt @@ -170,6 +170,10 @@ fun Application.factbooks() { call.respondText(buildString { generateRecentPageEdits() }, ContentType.Application.Rss) } + get("/comments.rss") { + call.respondText(buildString(call.recentCommentsRssFeedGenerator()), ContentType.Application.Rss) + } + // Client settings get("/change-theme") { diff --git a/src/jvmMain/kotlin/info/mechyrdia/data/views_comment.kt b/src/jvmMain/kotlin/info/mechyrdia/data/views_comment.kt index 96f64bd..3f8ad93 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/data/views_comment.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/data/views_comment.kt @@ -35,7 +35,7 @@ suspend fun ApplicationCall.recentCommentsPage(): HTML.() -> Unit { Comment.Table .sorted(Sorts.descending(Comment::submittedAt.serialName)) .filterNot { comment -> - NationData.get(comment.submittedBy).isBanned + comment.submittedBy != currNation?.id && NationData.get(comment.submittedBy).isBanned } .take(limit) .toList(), diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/view_nav.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/view_nav.kt index 05a40e6..3a35966 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/view_nav.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/view_nav.kt @@ -53,6 +53,7 @@ suspend fun ApplicationCall.standardNavBar(path: List? = null) = listOf( NavHead("Useful Links"), NavLink("/comment/help", "Commenting Help"), NavLink("/comment/recent", "Recent Comments"), + NavLink("/comments.rss", "RSS Feed: New Comments"), NavLink("/edits.rss", "RSS Feed: Page Updates"), ) + loadExternalLinks() diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/views_rss.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/views_rss.kt index 021f5c3..b4abc54 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/views_rss.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/views_rss.kt @@ -1,7 +1,13 @@ package info.mechyrdia.lore +import com.mongodb.client.model.Sorts import info.mechyrdia.Configuration +import info.mechyrdia.data.* +import io.ktor.server.application.* import io.ktor.util.* +import kotlinx.coroutines.flow.filterNot +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.flow.toList import java.io.File import java.time.Instant import java.time.ZoneOffset @@ -34,7 +40,7 @@ fun Appendable.generateRecentPageEdits() { RssChannel( title = "Recently Edited Factbooks | The Hour of Decision", link = "https://mechyrdia.info", - description = "A RSS news feed containing all factbooks in The Hour of Decision, in order of most recently edited.", + description = "An RSS feed containing all factbooks in The Hour of Decision, in order of most recently edited.", pubDate = mostRecentChange, lastBuildDate = mostRecentChange, ttl = 30, @@ -80,6 +86,62 @@ fun Appendable.generateRecentPageEdits() { ).toXml(this) } +suspend fun ApplicationCall.recentCommentsRssFeedGenerator(): Appendable.() -> Unit { + val currNation = currentNation() + + val limit = request.queryParameters["limit"]?.toIntOrNull() ?: 10 + + val validLimits = 1..100 + + if (limit !in validLimits) + return RssChannel( + title = "Recent Comments - Error | The Hour of Decision", + link = "https://mechyrdia.info/comment/recent", + description = "Comment limit must be between ${validLimits.first} and ${validLimits.last}, got $limit", + pubDate = null, + lastBuildDate = Instant.now(), + ttl = 120, + )::toXml + + val comments = CommentRenderData( + Comment.Table + .sorted(Sorts.descending(Comment::submittedAt.serialName)) + .filterNot { comment -> + comment.submittedBy != currNation?.id && NationData.get(comment.submittedBy).isBanned + } + .take(limit) + .toList(), + nationCache + ) + + val mostRecentComment = comments.firstOrNull()?.submittedAt + + return RssChannel( + title = "Recent Comments | The Hour of Decision", + link = "https://mechyrdia.info/comment/recent", + description = "An RSS feed containing the $limit most recently-submitted comments", + pubDate = mostRecentComment, + lastBuildDate = mostRecentComment, + ttl = 60, + categories = listOf( + RssCategory(domain = "https://nationstates.net", category = "Mechyrdia") + ), + items = comments.map { comment -> + RssItem( + title = "Comment by ${comment.submittedBy.name} on https://mechyrdia.info/lore/${comment.submittedIn}", + description = comment.contentsRaw.replace(INSIDE_TAG_TEXT, "").escapeHTML(), + link = "https://mechyrdia.info/comment/view/${comment.id}", + author = null, + comments = "https://mechyrdia.info/lore/${comment.submittedIn}#comments", + pubDate = comment.lastEdit ?: comment.submittedAt, + categories = listOf( + RssCategory(domain = "https://nationstates.net", category = comment.submittedBy.name) + ) + ) + } + )::toXml +} + data class RssCategory( val category: String, val domain: String? = null -- 2.25.1