From 98115faada7945941dfc5cc98d105f695b26144d Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 18 Jan 2024 16:02:42 -0500 Subject: [PATCH] FIX: Render votes RSS feed (#180) --- .../list_controller_extension.rb | 13 +++++++-- plugin.rb | 4 +++ spec/requests/lists_controller_spec.rb | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/discourse_topic_voting/list_controller_extension.rb b/lib/discourse_topic_voting/list_controller_extension.rb index e700e56..cd61fda 100644 --- a/lib/discourse_topic_voting/list_controller_extension.rb +++ b/lib/discourse_topic_voting/list_controller_extension.rb @@ -4,8 +4,8 @@ module DiscourseTopicVoting module ListControllerExtension def self.prepended(base) base.class_eval do - before_action :ensure_discourse_topic_voting, only: %i[voted_by] - skip_before_action :ensure_logged_in, only: %i[voted_by] + before_action :ensure_discourse_topic_voting, only: %i[voted_by votes_feed] + skip_before_action :ensure_logged_in, only: %i[voted_by votes_feed] end end @@ -18,6 +18,15 @@ def voted_by respond_with_list(list) end + def votes_feed + category_slug_path_with_id = params.require(:category_slug_path_with_id) + + @category = Category.find_by_slug_path_with_id(category_slug_path_with_id) + @topic_list = TopicQuery.new(current_user, { category: @category.id }).list_votes + + render "list", formats: [:rss] + end + protected def ensure_discourse_topic_voting diff --git a/plugin.rb b/plugin.rb index 7eaa768..c0df817 100755 --- a/plugin.rb +++ b/plugin.rb @@ -223,6 +223,10 @@ class Engine < ::Rails::Engine get "who" => "votes#who" end + Discourse::Application.routes.prepend do + get "c/*category_slug_path_with_id/l/votes.rss" => "list#votes_feed", :format => :rss + end + Discourse::Application.routes.append do mount ::DiscourseTopicVoting::Engine, at: "/voting" diff --git a/spec/requests/lists_controller_spec.rb b/spec/requests/lists_controller_spec.rb index 984863d..a4ae5fc 100644 --- a/spec/requests/lists_controller_spec.rb +++ b/spec/requests/lists_controller_spec.rb @@ -25,4 +25,32 @@ expect(response.status).to eq(404) end + + context "in a category" do + fab!(:category1) { Fabricate(:category) } + fab!(:category2) { Fabricate(:category) } + fab!(:topic1) do + Fabricate(:topic, category: category1, title: "Topic in votes-enabled category 1") + end + fab!(:topic2) do + Fabricate(:topic, category: category2, title: "Topic in votes-enabled category 2") + end + + before do + DiscourseTopicVoting::CategorySetting.create!(category: category1) + DiscourseTopicVoting::CategorySetting.create!(category: category2) + end + + it "allows anons to view votes RSS feed" do + DiscourseTopicVoting::Vote.create!(user: user, topic: topic1) + DiscourseTopicVoting::Vote.create!(user: user, topic: topic2) + + get "/c/#{category2.slug}/#{category2.id}/l/votes.rss" + + expect(response.status).to eq(200) + expect(response.body).to include(topic2.title) + # ensure we don't include votes from other categories + expect(response.body).not_to include(topic1.title) + end + end end