rss feeds!
This commit is contained in:
parent
13a584854e
commit
7b3cbdddee
1
Gemfile
1
Gemfile
|
@ -23,6 +23,7 @@ gem "unicorn"
|
||||||
gem "uglifier"
|
gem "uglifier"
|
||||||
|
|
||||||
gem "nokogiri"
|
gem "nokogiri"
|
||||||
|
gem "htmlentities"
|
||||||
|
|
||||||
group :test, :development do
|
group :test, :development do
|
||||||
gem "rspec-rails", "~> 2.6"
|
gem "rspec-rails", "~> 2.6"
|
||||||
|
|
|
@ -39,6 +39,7 @@ GEM
|
||||||
execjs (1.3.2)
|
execjs (1.3.2)
|
||||||
multi_json (~> 1.0)
|
multi_json (~> 1.0)
|
||||||
hike (1.2.1)
|
hike (1.2.1)
|
||||||
|
htmlentities (4.3.1)
|
||||||
i18n (0.6.0)
|
i18n (0.6.0)
|
||||||
journey (1.0.3)
|
journey (1.0.3)
|
||||||
jquery-rails (2.0.1)
|
jquery-rails (2.0.1)
|
||||||
|
@ -121,6 +122,7 @@ DEPENDENCIES
|
||||||
bcrypt-ruby (= 3.0.0)
|
bcrypt-ruby (= 3.0.0)
|
||||||
dynamic_form
|
dynamic_form
|
||||||
exception_notification
|
exception_notification
|
||||||
|
htmlentities
|
||||||
jquery-rails
|
jquery-rails
|
||||||
machinist
|
machinist
|
||||||
mysql2
|
mysql2
|
||||||
|
|
|
@ -26,4 +26,49 @@ class ApplicationController < ActionController::Base
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -1,60 +1,50 @@
|
||||||
class HomeController < ApplicationController
|
class HomeController < ApplicationController
|
||||||
def index
|
def index
|
||||||
conds = [ "is_expired = 0 " ]
|
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, false)
|
||||||
|
|
||||||
if @user && !@newest
|
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
||||||
# exclude downvoted items
|
"title=\"RSS 2.0\" href=\"/rss\" />"
|
||||||
conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " <<
|
|
||||||
"WHERE user_id = ? AND vote < 0) "
|
respond_to do |format|
|
||||||
conds.push @user.id
|
format.html { render :action => "index" }
|
||||||
|
format.rss { render :action => "rss", :layout => false }
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def newest
|
def newest
|
||||||
@newest = true
|
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, true)
|
||||||
index
|
|
||||||
|
@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
|
end
|
||||||
|
|
||||||
def tagged
|
def tagged
|
||||||
if !(@tag = Tag.find_by_tag(params[:tag]))
|
@tag = Tag.find_by_tag!(params[:tag])
|
||||||
raise ActionController::RoutingError.new("tag not found")
|
@stories = find_stories_for_user_and_tag_and_newest(@user, @tag, false)
|
||||||
end
|
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,7 +80,7 @@ class Story < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def comments_url
|
def comments_url
|
||||||
"/p/#{self.short_id}/#{self.title_as_url}"
|
"http://lobste.rs/p/#{self.short_id}/#{self.title_as_url}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@_comment_count = nil
|
@_comment_count = nil
|
||||||
|
|
24
app/views/home/rss.erb
Normal file
24
app/views/home/rss.erb
Normal 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>
|
|
@ -7,6 +7,9 @@
|
||||||
<%= stylesheet_link_tag "application", :media => "all" %>
|
<%= stylesheet_link_tag "application", :media => "all" %>
|
||||||
<%= javascript_include_tag "application" %>
|
<%= javascript_include_tag "application" %>
|
||||||
<%= csrf_meta_tags %>
|
<%= csrf_meta_tags %>
|
||||||
|
<% if @rss_link %>
|
||||||
|
<%= raw(@rss_link) %>
|
||||||
|
<% end %>
|
||||||
<% if @user %>
|
<% if @user %>
|
||||||
<script>
|
<script>
|
||||||
Lobsters.curUser = '<%= @user.id %>';
|
Lobsters.curUser = '<%= @user.id %>';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
Lobsters::Application.routes.draw do
|
Lobsters::Application.routes.draw do
|
||||||
root :to => "home#index"
|
root :to => "home#index"
|
||||||
|
|
||||||
get "/newest" => "home#newest"
|
get "/newest(.format)" => "home#newest"
|
||||||
|
|
||||||
get "/threads" => "comments#threads"
|
get "/threads" => "comments#threads"
|
||||||
|
|
||||||
|
@ -39,4 +39,6 @@ Lobsters::Application.routes.draw do
|
||||||
|
|
||||||
get "/p/:id/(:title)" => "stories#show"
|
get "/p/:id/(:title)" => "stories#show"
|
||||||
get "/u/:id" => "users#show"
|
get "/u/:id" => "users#show"
|
||||||
|
|
||||||
|
get "/rss" => "home#index", :format => "rss"
|
||||||
end
|
end
|
||||||
|
|
|
@ -112,7 +112,7 @@ class Markdowner
|
||||||
out.gsub!(/<p>\n?<p>/, "\n<p>")
|
out.gsub!(/<p>\n?<p>/, "\n<p>")
|
||||||
out.gsub!(/<\/p><p>/, "</p>\n<p>")
|
out.gsub!(/<\/p><p>/, "</p>\n<p>")
|
||||||
|
|
||||||
out.strip
|
out.strip.force_encoding("utf-8")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._linkify_text(chunk)
|
def self._linkify_text(chunk)
|
||||||
|
|
Loading…
Reference in a new issue