From c4a1a3974068be62a1ece331e24cdf3b1f4e283c Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Fri, 14 Apr 2023 20:19:49 +1000 Subject: [PATCH] Add a feed for statuses --- app/actions/feeds/statuses_rss.rb | 14 ++++++++++++ app/repos/post_repo.rb | 9 ++++++++ app/templates/feeds/statuses_rss.xml.builder | 20 +++++++++++++++++ app/templates/layouts/app.html.slim | 1 + app/views/feeds/statuses_rss.rb | 23 ++++++++++++++++++++ config/routes.rb | 1 + 6 files changed, 68 insertions(+) create mode 100644 app/actions/feeds/statuses_rss.rb create mode 100644 app/templates/feeds/statuses_rss.xml.builder create mode 100644 app/views/feeds/statuses_rss.rb diff --git a/app/actions/feeds/statuses_rss.rb b/app/actions/feeds/statuses_rss.rb new file mode 100644 index 0000000..b4d6b2e --- /dev/null +++ b/app/actions/feeds/statuses_rss.rb @@ -0,0 +1,14 @@ +module Adamantium + module Actions + module Feeds + class StatusesRss < Action + include Deps["views.feeds.statuses_rss"] + + def handle(req, res) + res.content_type = "application/rss+xml" + res.render statuses_rss, format: :xml + end + end + end + end +end diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index 4f7c5c8..d82808f 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -153,6 +153,15 @@ module Adamantium .to_a end + def statuses_for_rss + posts + .where(post_type: "post", name: nil, location: nil) + .published + .combine(:tags) + .order(Sequel.desc(:published_at)) + .to_a + end + def fetch!(slug) posts .published diff --git a/app/templates/feeds/statuses_rss.xml.builder b/app/templates/feeds/statuses_rss.xml.builder new file mode 100644 index 0000000..eda69bf --- /dev/null +++ b/app/templates/feeds/statuses_rss.xml.builder @@ -0,0 +1,20 @@ +xml.instruct!(:xml, version: "2.0", encoding: "utf-8") + +xml.channel do |channel| + channel.title "Daniel Nitsikopoulos" + channel.description "The RSS feed for https://dnitza.com" + channel.lastBuildDate Time.now.rfc2822 + channel.pubDate Time.now.rfc2822 + channel.ttl 1800 + + posts.each do |post| + channel.item do |item| + item.title post.display_title + item.description do |desc| + desc.cdata! post.feed_content + end + item.guid(post.slug, isPermaLink: true) + item.pubDate post.machine_published_at + end + end +end diff --git a/app/templates/layouts/app.html.slim b/app/templates/layouts/app.html.slim index 60f28d3..3fcb1bb 100644 --- a/app/templates/layouts/app.html.slim +++ b/app/templates/layouts/app.html.slim @@ -16,6 +16,7 @@ html link rel="pingback" href=Hanami.app.settings.pingback_url link rel="feed" type="text/html" href="/posts" link rel="feed alternate" type="application/rss+xml" href="/feeds/rss" + link rel="feed alternate" type="application/rss+xml" href="/feeds/statuses_rss" link rel="me" href=Hanami.app.settings.mastodon_url link rel="me" href=Hanami.app.settings.github_url diff --git a/app/views/feeds/statuses_rss.rb b/app/views/feeds/statuses_rss.rb new file mode 100644 index 0000000..6bdfca6 --- /dev/null +++ b/app/views/feeds/statuses_rss.rb @@ -0,0 +1,23 @@ +require "builder" + +module Adamantium + module Views + module Feeds + class StatusesRss < Adamantium::View + include Deps["repos.post_repo"] + + expose :posts do + post_repo.statuses_for_rss.map do |post| + Decorators::Posts::Decorator.new post + end + end + + expose :xml, decorate: false, layout: true + + def xml + Builder::XmlMarkup.new(indent: 2) + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index c1f3539..28dfc26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ module Adamantium get "/key", to: "key.show" if Hanami.app.settings.micropub_pub_key get "/feeds/rss", to: "feeds.rss" + get "/feeds/statuses_rss", to: "feeds.statuses_rss" get "/:slug", to: "pages.show"