Skip to content

Commit

Permalink
rss feeds!
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs committed Aug 24, 2012
1 parent 13a5848 commit 7b3cbdd
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 47 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem "unicorn"
gem "uglifier"

gem "nokogiri"
gem "htmlentities"

group :test, :development do
gem "rspec-rails", "~> 2.6"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -121,6 +122,7 @@ DEPENDENCIES
bcrypt-ruby (= 3.0.0)
dynamic_form
exception_notification
htmlentities
jquery-rails
machinist
mysql2
Expand Down
45 changes: 45 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,49 @@ def require_logged_in_user_or_400
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
78 changes: 34 additions & 44 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -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
end
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
"title=\"RSS 2.0\" href=\"/rss\" />"

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)
respond_to do |format|
format.html { render :action => "index" }
format.rss { render :action => "rss", :layout => false }
end
end

if @user
votes = Vote.votes_by_user_for_stories_hash(@user.id,
@stories.map{|s| s.id })
def newest
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, true)

@stories.each do |s|
if votes[s.id]
s.vote = votes[s.id]
end
end
end
@page_title = "Newest Stories"

if @newest
# TODO: better algorithm here
@stories.sort_by!{|s| s.created_at }.reverse!
else
@stories.sort_by!{|s| s.hotness }
end
@rss_link = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss\" />"

render :action => "index"
end
@title = "Newest Stories"
@title_url = "/newest"

def newest
@newest = true
index
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)

@page_title = @tag.description

index
@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
2 changes: 1 addition & 1 deletion app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def deal_with_tags
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
Expand Down
24 changes: 24 additions & 0 deletions app/views/home/rss.erb
Original file line number Diff line number Diff line change
@@ -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>
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>';
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -39,4 +39,6 @@

get "/p/:id/(:title)" => "stories#show"
get "/u/:id" => "users#show"

get "/rss" => "home#index", :format => "rss"
end
2 changes: 1 addition & 1 deletion extras/markdowner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def self.markdown(string)
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)
Expand Down

0 comments on commit 7b3cbdd

Please sign in to comment.