rss feeds!

This commit is contained in:
joshua stein 2012-06-30 18:42:53 -05:00
parent 13a584854e
commit 7b3cbdddee
9 changed files with 117 additions and 50 deletions

View file

@ -23,6 +23,7 @@ gem "unicorn"
gem "uglifier"
gem "nokogiri"
gem "htmlentities"
group :test, :development do
gem "rspec-rails", "~> 2.6"

View file

@ -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

View file

@ -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

View file

@ -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 ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
"title=\"RSS 2.0\" href=\"/rss\" />"
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 = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss\" />"
@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 = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
"title=\"RSS 2.0 - Tagged #{CGI.escape(@tag.tag)} " <<
"(#{CGI.escape(@tag.description)})\" href=\"/t/" +
"#{CGI.escape(@tag.tag)}.rss\" />"
@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

View file

@ -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

24
app/views/home/rss.erb Normal file
View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<% coder = HTMLEntities.new %>
<rss version="2.0">
<channel>
<title>lobste.rs<%= @page_title ? ": " + h(@page_title) : "" %></title>
<description><%= @page_title %></description>
<link>http://lobste.rs/<%= @newest ? "newest" : "" %></link>
<% @stories.each do |story| %>
<item>
<title><%= raw coder.encode(story.title, :decimal) %></title>
<link><%= story.url_or_comments_url %></link>
<author><%= story.user.username %></author>
<pubDate><%= story.created_at.rfc2822 %></pubDate>
<comments><%= story.comments_url %></comments>
<description><%= raw coder.encode(story.linkified_text, :decimal)
%></description>
<% story.tags.each do |tag| %>
<category><%= tag.tag %></category>
<% end %>
</item>
<% end %>
</channel>
</rss>

View file

@ -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 %>
<script>
Lobsters.curUser = '<%= @user.id %>';

View file

@ -1,7 +1,7 @@
Lobsters::Application.routes.draw do
root :to => "home#index"
get "/newest" => "home#newest"
get "/newest(.format)" => "home#newest"
get "/threads" => "comments#threads"
@ -39,4 +39,6 @@ Lobsters::Application.routes.draw do
get "/p/:id/(:title)" => "stories#show"
get "/u/:id" => "users#show"
get "/rss" => "home#index", :format => "rss"
end

View file

@ -112,7 +112,7 @@ class Markdowner
out.gsub!(/<p>\n?<p>/, "\n<p>")
out.gsub!(/<\/p><p>/, "</p>\n<p>")
out.strip
out.strip.force_encoding("utf-8")
end
def self._linkify_text(chunk)