2012-06-17 03:15:46 +02:00
|
|
|
class HomeController < ApplicationController
|
2012-07-07 00:33:27 +02:00
|
|
|
STORIES_PER_PAGE = 20
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def index
|
2012-07-01 01:42:53 +02:00
|
|
|
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, false)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
|
|
|
"title=\"RSS 2.0\" href=\"/rss\" />"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss { render :action => "rss", :layout => false }
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2012-07-01 01:42:53 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
def newest
|
|
|
|
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, true)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
@page_title = "Newest Stories"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
@rss_link = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
|
|
|
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss\" />"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
@title = "Newest Stories"
|
|
|
|
@title_url = "/newest"
|
2012-07-02 00:06:33 +02:00
|
|
|
@newest = true
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss { render :action => "rss", :layout => false }
|
|
|
|
end
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def tagged
|
2012-07-01 01:42:53 +02:00
|
|
|
@tag = Tag.find_by_tag!(params[:tag])
|
|
|
|
@stories = find_stories_for_user_and_tag_and_newest(@user, @tag, false)
|
|
|
|
|
|
|
|
@page_title = @tag.description
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
@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
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2012-07-07 00:33:27 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false)
|
|
|
|
conds = [ "is_expired = 0 AND is_moderated = 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
|
|
|
|
elsif user
|
|
|
|
conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " <<
|
|
|
|
"tag_filters WHERE user_id = ?)"
|
|
|
|
conds.push @user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
@page = 1
|
|
|
|
if params[:page].to_i > 0
|
|
|
|
@page = params[:page].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
stories = Story.find(
|
|
|
|
:all,
|
|
|
|
:conditions => conds,
|
|
|
|
:include => [ :user, { :taggings => :tag } ],
|
|
|
|
:limit => STORIES_PER_PAGE + 1,
|
|
|
|
:offset => ((@page - 1) * STORIES_PER_PAGE),
|
|
|
|
:order => (newest ? "stories.created_at DESC" : "hotness")
|
|
|
|
)
|
|
|
|
|
|
|
|
@show_more = false
|
|
|
|
|
|
|
|
if stories.count > STORIES_PER_PAGE
|
|
|
|
@show_more = true
|
|
|
|
stories.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: figure out a better sorting algorithm for newest, including some
|
|
|
|
# older stories that got one or two votes
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
stories
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|