From 4b811f662fe58a1ec3fe8b0b20eb882594c28351 Mon Sep 17 00:00:00 2001 From: Sidharth Bansal Date: Fri, 2 Feb 2018 07:26:35 +0530 Subject: [PATCH] RSS feed on tagged content for author pages (#2161) * RSS feed * Added changes --- app/controllers/tag_controller.rb | 18 +++++++++ app/models/tag.rb | 2 +- .../rss_for_tagged_with_author.rss.builder | 38 +++++++++++++++++++ config/routes.rb | 1 + test/functional/tag_controller_test.rb | 6 +++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/views/tag/rss_for_tagged_with_author.rss.builder diff --git a/app/controllers/tag_controller.rb b/app/controllers/tag_controller.rb index 0dc599f972..670c9a929e 100644 --- a/app/controllers/tag_controller.rb +++ b/app/controllers/tag_controller.rb @@ -346,6 +346,24 @@ def rss end end + def rss_for_tagged_with_author + @user = User.find_by(name: params[:author]) + @notes = Tag.tagged_nodes_by_author([params[:tagname]], @user) + .limit(20) + respond_to do |format| + format.rss do + response.headers['Content-Type'] = 'application/xml; charset=utf-8' + response.headers['Access-Control-Allow-Origin'] = '*' + render layout: false + end + format.ics do + response.headers['Content-Disposition'] = "attachment; filename='public-lab-events.ics'" + response.headers['Content-Type'] = 'text/calendar; charset=utf-8' + render layout: false, template: 'tag/icalendar.ics', filename: 'public-lab-events.ics' + end + end + end + def contributors set_sidebar :tags, [params[:id]], note_count: 20 @tagnames = [params[:id]] diff --git a/app/models/tag.rb b/app/models/tag.rb index e222fef812..a32171412d 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -248,8 +248,8 @@ def self.tagged_nodes_by_author(tagname, user_id) @wildcard = true Node.where('term_data.name LIKE(?)', tagname[0..-2]+'%') .includes(:node_tag, :tag) - .order('node.nid DESC') .references(:term_data) + .order('node.nid DESC') .where('node.uid = ?', user_id) else Node.where('term_data.name = ?', tagname) diff --git a/app/views/tag/rss_for_tagged_with_author.rss.builder b/app/views/tag/rss_for_tagged_with_author.rss.builder new file mode 100644 index 0000000000..38c83fe523 --- /dev/null +++ b/app/views/tag/rss_for_tagged_with_author.rss.builder @@ -0,0 +1,38 @@ +xml.rss :version => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom' do + xml.channel do + xml.title "Research tagged '#{params[:tagname]}' by #{params[:authorname]}" + xml.description "Open source environmental science research at Public Lab" + xml.link "https://#{request.host}/feed/tag/"+params[:tagname]+"/author/"+"params[:authorname]"+".rss" + + @notes.each do |node| + + body = node.body + uname = node.author.username + email = node.author.email + if node.author.user.has_power_tag('twitter') + uname = node.author.user.get_value_of_power_tag('twitter') + end + author_format = "@#{uname} (#{email})" + xml.item do + xml.title " #{node.title}" + xml.author author_format + if node.power_tag('date') != '' + begin + xml.pubDate DateTime.strptime(node.power_tag('date'), "%m-%d-%Y").rfc822 + rescue + xml.pubDate node.power_tag('date') + end + else + xml.pubDate node.created_at.to_s(:rfc822) + end + xml.link "https://" + request.host.to_s + node.path + if node.main_image + xml.description { xml.cdata!("#{node.main_image.title}") } + else + xml.description { xml.cdata!("PublicLab

#{body}

") } + end + xml.guid url_for :only_path => false, :controller => 'notes', :action => 'show', :id => node.nid + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 9705149165..81b222f779 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,6 +93,7 @@ # these need precedence for tag listings get 'feed/tag/:tagname' => 'tag#rss' get ':node_type/tag(/:id)' => 'tag#show' + get 'feed/tag/:tagname/author/:authorname' => 'tag#rss_for_tagged_with_author' get 'wiki/raw/:id' => 'wiki#raw' get 'wiki/revisions/:id' => 'wiki#revisions' get 'wiki/revert/:id' => 'wiki#revert' diff --git a/test/functional/tag_controller_test.rb b/test/functional/tag_controller_test.rb index dbfb642181..47bd5d4c7b 100644 --- a/test/functional/tag_controller_test.rb +++ b/test/functional/tag_controller_test.rb @@ -446,4 +446,10 @@ def setup assert_response :success assert_select 'table' # ensure a table is shown end + test 'rss with tagname and authorname' do + get :rss_for_tagged_with_author, tagname: 'test*', authorname: 'jeff', format: 'rss' + assert :success + assert_not_nil :notes + assert_equal 'application/rss+xml', @response.content_type + end end