Skip to content

Commit

Permalink
RSS feed on tagged content for author pages (publiclab#2161)
Browse files Browse the repository at this point in the history
* RSS feed

* Added changes
  • Loading branch information
SidharthBansal authored and Souravirus committed Mar 12, 2018
1 parent d554a61 commit 4b811f6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions app/views/tag/rss_for_tagged_with_author.rss.builder
Original file line number Diff line number Diff line change
@@ -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!("<img src='#{node.main_image.path(:default)}' alt='#{node.main_image.title}'>") }
else
xml.description { xml.cdata!("<img src='https://i.publiclab.org/system/images/photos/000/000/354/medium/Boots-ground-02.png' alt='PublicLab'><p>#{body}</p>") }
end
xml.guid url_for :only_path => false, :controller => 'notes', :action => 'show', :id => node.nid
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4b811f6

Please sign in to comment.