From 848120c0985b5a98a5f89908efefb7a90a915dbc Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 13 Mar 2017 14:24:10 +0530 Subject: [PATCH] FEATURE: RSS feed for top page period filters --- app/controllers/list_controller.rb | 18 ++++++++-- config/locales/server.en.yml | 6 ++++ config/routes.rb | 1 + spec/controllers/list_controller_spec.rb | 44 ++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 431b68f1439..91eeeab5b29 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -46,6 +46,7 @@ class ListController < ApplicationController :parent_category_category_top, # top pages (ie. with a period) TopTopic.periods.map { |p| :"top_#{p}" }, + TopTopic.periods.map { |p| :"top_#{p}_feed" }, TopTopic.periods.map { |p| :"category_top_#{p}" }, TopTopic.periods.map { |p| :"category_none_top_#{p}" }, TopTopic.periods.map { |p| :"parent_category_category_top_#{p}" }, @@ -168,7 +169,7 @@ class ListController < ApplicationController @link = "#{Discourse.base_url}/top" @atom_link = "#{Discourse.base_url}/top.rss" @description = I18n.t("rss_description.top") - @topic_list = TopicQuery.new(nil).list_top_for("monthly") + @topic_list = TopicQuery.new(nil).list_top_for(SiteSetting.top_page_default_timeframe.to_sym) render 'list', formats: [:rss] end @@ -232,7 +233,7 @@ class ListController < ApplicationController list.for_period = period list.more_topics_url = construct_url_with(:next, top_options) list.prev_topics_url = construct_url_with(:prev, top_options) - @rss = "top" + @rss = "top_#{period}" if use_crawler_layout? @title = I18n.t("js.filters.top.#{period}.title") @@ -252,6 +253,19 @@ class ListController < ApplicationController define_method("parent_category_category_top_#{period}") do self.send("top_#{period}", category: @category.id) end + + # rss feed + define_method("top_#{period}_feed") do |options = nil| + discourse_expires_in 1.minute + + @description = I18n.t("rss_description.top_#{period}") + @title = "#{SiteSetting.title} - #{@description}" + @link = "#{Discourse.base_url}/top/#{period}" + @atom_link = "#{Discourse.base_url}/top/#{period}.rss" + @topic_list = TopicQuery.new(nil).list_top_for(period) + + render 'list', formats: [:rss] + end end protected diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index f3042f594b6..aa6423dccec 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -229,6 +229,12 @@ en: latest: "Latest topics" hot: "Hot topics" top: "Top topics" + top_all: "All time top topics" + top_yearly: "Yearly top topics" + top_quarterly: "Quarterly top topics" + top_monthly: "Monthly top topics" + top_weekly: "Weekly top topics" + top_daily: "Daily top topics" posts: "Latest posts" private_posts: "Latest private messages" group_posts: "Latest posts from %{group_name}" diff --git a/config/routes.rb b/config/routes.rb index 7c42b21eb7b..b62487d368b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -503,6 +503,7 @@ Discourse::Application.routes.draw do get "category_hashtags/check" => "category_hashtags#check" TopTopic.periods.each do |period| + get "top/#{period}.rss" => "list#top_#{period}_feed", format: :rss get "top/#{period}" => "list#top_#{period}" get "c/:category/l/top/#{period}" => "list#category_top_#{period}", as: "category_top_#{period}" get "c/:category/none/l/top/#{period}" => "list#category_none_top_#{period}", as: "category_none_top_#{period}" diff --git a/spec/controllers/list_controller_spec.rb b/spec/controllers/list_controller_spec.rb index db81150915e..5a4a94d800e 100644 --- a/spec/controllers/list_controller_spec.rb +++ b/spec/controllers/list_controller_spec.rb @@ -49,13 +49,53 @@ describe ListController do end describe 'RSS feeds' do - - it 'renders RSS' do + it 'renders latest RSS' do get "latest_feed", format: :rss expect(response).to be_success expect(response.content_type).to eq('application/rss+xml') end + it 'renders top RSS' do + get "top_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders all time top RSS' do + get "top_all_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders yearly top RSS' do + get "top_yearly_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders quarterly top RSS' do + get "top_quarterly_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders monthly top RSS' do + get "top_monthly_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders weekly top RSS' do + get "top_weekly_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end + + it 'renders daily top RSS' do + get "top_daily_feed", format: :rss + expect(response).to be_success + expect(response.content_type).to eq('application/rss+xml') + end end context 'category' do