From 7b3cbdddee1970b05c9ca08ffeb5487f21b79125 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Sat, 30 Jun 2012 18:42:53 -0500 Subject: [PATCH] rss feeds! --- Gemfile | 1 + Gemfile.lock | 2 + app/controllers/application_controller.rb | 45 ++++++++++++ app/controllers/home_controller.rb | 84 ++++++++++------------- app/models/story.rb | 2 +- app/views/home/rss.erb | 24 +++++++ app/views/layouts/application.html.erb | 3 + config/routes.rb | 4 +- extras/markdowner.rb | 2 +- 9 files changed, 117 insertions(+), 50 deletions(-) create mode 100644 app/views/home/rss.erb diff --git a/Gemfile b/Gemfile index 3089771..2beacc4 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem "unicorn" gem "uglifier" gem "nokogiri" +gem "htmlentities" group :test, :development do gem "rspec-rails", "~> 2.6" diff --git a/Gemfile.lock b/Gemfile.lock index 12f96fe..1c92a00 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,7 @@ GEM execjs (1.3.2) multi_json (~> 1.0) hike (1.2.1) + htmlentities (4.3.1) i18n (0.6.0) journey (1.0.3) jquery-rails (2.0.1) @@ -121,6 +122,7 @@ DEPENDENCIES bcrypt-ruby (= 3.0.0) dynamic_form exception_notification + htmlentities jquery-rails machinist mysql2 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b1fa4e1..9dcbdeb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,4 +26,49 @@ class ApplicationController < ActionController::Base return false end end + + def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false) + stories = [] + + conds = [ "is_expired = 0 " ] + + if user && !newest + # exclude downvoted items + conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " << + "WHERE user_id = ? AND vote < 0) " + conds.push user.id + end + + if tag + conds[0] << "AND taggings.tag_id = ?" + conds.push tag.id + stories = Story.find(:all, :conditions => conds, + :include => [ :user, :taggings ], :joins => [ :user, :taggings ], + :limit => 30) + else + stories = Story.find(:all, :conditions => conds, + :include => [ :user, :taggings ], :joins => [ :user ], + :limit => 30) + end + + if user + votes = Vote.votes_by_user_for_stories_hash(user.id, + stories.map{|s| s.id }) + + stories.each do |s| + if votes[s.id] + s.vote = votes[s.id] + end + end + end + + if newest + # TODO: better algorithm here + stories.sort_by!{|s| s.created_at }.reverse! + else + stories.sort_by!{|s| s.hotness } + end + + stories + end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index db8bab4..a7c7db5 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,60 +1,50 @@ class HomeController < ApplicationController def index - conds = [ "is_expired = 0 " ] + @stories = find_stories_for_user_and_tag_and_newest(@user, nil, false) - if @user && !@newest - # exclude downvoted items - conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " << - "WHERE user_id = ? AND vote < 0) " - conds.push @user.id + @rss_link ||= "" + + respond_to do |format| + format.html { render :action => "index" } + format.rss { render :action => "rss", :layout => false } end - - if @tag - conds[0] << "AND taggings.tag_id = ?" - conds.push @tag.id - @stories = Story.find(:all, :conditions => conds, - :include => [ :user, :taggings ], :joins => [ :user, :taggings ], - :limit => 30) - - @title = @tag.description.blank?? @tag.tag : @tag.description - @title_url = tag_url(@tag.tag) - else - @stories = Story.find(:all, :conditions => conds, - :include => [ :user, :taggings ], :joins => [ :user ], - :limit => 30) - end - - if @user - votes = Vote.votes_by_user_for_stories_hash(@user.id, - @stories.map{|s| s.id }) - - @stories.each do |s| - if votes[s.id] - s.vote = votes[s.id] - end - end - end - - if @newest - # TODO: better algorithm here - @stories.sort_by!{|s| s.created_at }.reverse! - else - @stories.sort_by!{|s| s.hotness } - end - - render :action => "index" end def newest - @newest = true - index + @stories = find_stories_for_user_and_tag_and_newest(@user, nil, true) + + @page_title = "Newest Stories" + + @rss_link = "" + + @title = "Newest Stories" + @title_url = "/newest" + + respond_to do |format| + format.html { render :action => "index" } + format.rss { render :action => "rss", :layout => false } + end end def tagged - if !(@tag = Tag.find_by_tag(params[:tag])) - raise ActionController::RoutingError.new("tag not found") - end + @tag = Tag.find_by_tag!(params[:tag]) + @stories = find_stories_for_user_and_tag_and_newest(@user, @tag, false) - index + @page_title = @tag.description + + @rss_link = "" + + @title = @tag.description.blank?? @tag.tag : @tag.description + @title_url = tag_url(@tag.tag) + + respond_to do |format| + format.html { render :action => "index" } + format.rss { render :action => "rss", :layout => false } + end end end diff --git a/app/models/story.rb b/app/models/story.rb index 72539c4..d4afe3d 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -80,7 +80,7 @@ class Story < ActiveRecord::Base end def comments_url - "/p/#{self.short_id}/#{self.title_as_url}" + "http://lobste.rs/p/#{self.short_id}/#{self.title_as_url}" end @_comment_count = nil diff --git a/app/views/home/rss.erb b/app/views/home/rss.erb new file mode 100644 index 0000000..efede57 --- /dev/null +++ b/app/views/home/rss.erb @@ -0,0 +1,24 @@ + +<% coder = HTMLEntities.new %> + + + lobste.rs<%= @page_title ? ": " + h(@page_title) : "" %> + <%= @page_title %> + http://lobste.rs/<%= @newest ? "newest" : "" %> + + <% @stories.each do |story| %> + + <%= raw coder.encode(story.title, :decimal) %> + <%= story.url_or_comments_url %> + <%= story.user.username %> + <%= story.created_at.rfc2822 %> + <%= story.comments_url %> + <%= raw coder.encode(story.linkified_text, :decimal) + %> + <% story.tags.each do |tag| %> + <%= tag.tag %> + <% end %> + + <% end %> + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 20a6b14..c461a1c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,6 +7,9 @@ <%= stylesheet_link_tag "application", :media => "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> + <% if @rss_link %> + <%= raw(@rss_link) %> + <% end %> <% if @user %>