2012-06-17 03:15:46 +02:00
|
|
|
class HomeController < ApplicationController
|
2012-07-07 01:00:00 +02:00
|
|
|
STORIES_PER_PAGE = 25
|
2012-07-07 00:33:27 +02:00
|
|
|
|
2012-09-18 17:38:03 +02:00
|
|
|
# for rss feeds, load the user's tag filters if a token is passed
|
|
|
|
before_filter :find_user_from_rss_token, :only => [ :index, :newest ]
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def index
|
2012-09-04 00:21:41 +02:00
|
|
|
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
|
|
|
nil, false, nil)
|
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\" " <<
|
2012-09-18 17:38:03 +02:00
|
|
|
"title=\"RSS 2.0\" href=\"/rss" <<
|
|
|
|
(@user ? "?token=#{@user.rss_token}" : "") << "\" />"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = ""
|
2012-07-11 22:34:53 +02:00
|
|
|
@cur_url = "/"
|
|
|
|
|
2012-07-01 01:42:53 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
2012-09-18 17:38:03 +02:00
|
|
|
format.rss {
|
|
|
|
if @user && params[:token].present?
|
|
|
|
@title = "Private feed for #{@user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "rss", :layout => false
|
|
|
|
}
|
2012-12-17 03:05:27 +01:00
|
|
|
format.json { render :json => @stories }
|
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
|
2012-09-04 00:21:41 +02:00
|
|
|
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
|
|
|
nil, true, nil)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = "Newest Stories"
|
2012-07-11 22:34:53 +02:00
|
|
|
@cur_url = "/newest"
|
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\" " <<
|
2012-09-18 17:38:03 +02:00
|
|
|
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss" <<
|
|
|
|
(@user ? "?token=#{@user.rss_token}" : "") << "\" />"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
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" }
|
2012-09-18 17:38:03 +02:00
|
|
|
format.rss {
|
|
|
|
if @user && params[:token].present?
|
|
|
|
@title += " - Private feed for #{@user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "rss", :layout => false
|
|
|
|
}
|
2012-12-17 03:05:27 +01:00
|
|
|
format.json { render :json => @stories }
|
2012-07-01 01:42:53 +02:00
|
|
|
end
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
|
2012-09-04 00:21:41 +02:00
|
|
|
def newest_by_user
|
2013-12-24 04:20:06 +01:00
|
|
|
for_user = User.where(:username => params[:user]).first!
|
2012-09-04 00:21:41 +02:00
|
|
|
|
|
|
|
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
|
|
|
nil, false, for_user.id)
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = "Newest Stories by #{for_user.username}"
|
2012-09-04 00:21:41 +02:00
|
|
|
@cur_url = "/newest/#{for_user.username}"
|
|
|
|
|
|
|
|
@newest = true
|
|
|
|
@for_user = for_user.username
|
|
|
|
|
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def tagged
|
2013-12-24 04:20:06 +01:00
|
|
|
@tag = Tag.where(:tag => params[:tag]).first!
|
2012-09-04 00:21:41 +02:00
|
|
|
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
|
|
|
@tag, false, nil)
|
2012-07-01 01:42:53 +02:00
|
|
|
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = @tag.description.blank?? @tag.tag : @tag.description
|
2012-07-11 22:34:53 +02:00
|
|
|
@cur_url = tag_url(@tag.tag)
|
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)} " <<
|
2013-03-29 22:20:52 +01:00
|
|
|
"(#{CGI.escape(@tag.description.to_s)})\" href=\"/t/" +
|
2012-07-01 01:42:53 +02:00
|
|
|
"#{CGI.escape(@tag.tag)}.rss\" />"
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-10-18 22:55:20 +02:00
|
|
|
def privacy
|
|
|
|
begin
|
|
|
|
render :action => "privacy"
|
|
|
|
rescue
|
|
|
|
render :text => "<div class=\"box wide\">" <<
|
|
|
|
"You apparently have no privacy." <<
|
|
|
|
"</div>", :layout => "application"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def about
|
|
|
|
begin
|
|
|
|
render :action => "about"
|
|
|
|
rescue
|
|
|
|
render :text => "<div class=\"box wide\">" <<
|
|
|
|
"A mystery." <<
|
|
|
|
"</div>", :layout => "application"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-07 00:33:27 +02:00
|
|
|
private
|
2012-09-04 00:21:41 +02:00
|
|
|
def find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil,
|
|
|
|
newest = false, by_user = nil)
|
2012-07-10 21:43:51 +02:00
|
|
|
@page = 1
|
|
|
|
if params[:page].to_i > 0
|
|
|
|
@page = params[:page].to_i
|
|
|
|
end
|
|
|
|
|
2013-08-05 09:16:33 +02:00
|
|
|
# guest views have caching, but don't bother for logged-in users or dev or
|
|
|
|
# when the user has tag filters
|
|
|
|
if Rails.env == "development" || user || tags_filtered_by_cookie.any?
|
2012-09-04 00:21:41 +02:00
|
|
|
stories, @show_more =
|
|
|
|
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
|
|
|
newest, by_user)
|
2012-07-10 21:43:51 +02:00
|
|
|
else
|
|
|
|
stories, @show_more = Rails.cache.fetch("stories tag:" <<
|
2012-09-04 00:21:41 +02:00
|
|
|
"#{tag ? tag.tag : ""} new:#{newest} page:#{@page.to_i} by:#{by_user}",
|
2012-07-10 21:43:51 +02:00
|
|
|
:expires_in => 45) do
|
2012-09-04 00:21:41 +02:00
|
|
|
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
|
|
|
newest, by_user)
|
2012-07-10 21:43:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stories
|
|
|
|
end
|
|
|
|
|
2012-09-04 00:21:41 +02:00
|
|
|
def _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil,
|
|
|
|
newest = false, by_user = nil)
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = Story.where(:is_expired => false)
|
2012-07-07 00:33:27 +02:00
|
|
|
|
2012-09-04 00:21:41 +02:00
|
|
|
if user && !(newest || by_user)
|
2012-07-07 00:33:27 +02:00
|
|
|
# exclude downvoted items
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = stories.where(
|
|
|
|
Story.arel_table[:id].not_in(
|
|
|
|
Vote.arel_table.where(
|
|
|
|
Vote.arel_table[:user_id].eq(user.id)
|
|
|
|
).where(
|
|
|
|
Vote.arel_table[:vote].lt(0)
|
|
|
|
).where(
|
|
|
|
Vote.arel_table[:comment_id].eq(nil)
|
|
|
|
).project(
|
|
|
|
Vote.arel_table[:story_id]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-07-07 00:33:27 +02:00
|
|
|
end
|
|
|
|
|
2013-02-08 00:35:20 +01:00
|
|
|
filtered_tag_ids = []
|
|
|
|
if user
|
|
|
|
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
|
|
|
else
|
|
|
|
# for logged-out users, filter defaults
|
2013-08-05 09:16:33 +02:00
|
|
|
filtered_tag_ids = Tag.where(:filtered_by_default => true).
|
|
|
|
map{|t| t.id } + tags_filtered_by_cookie.map{|t| t.id }
|
2013-02-08 00:35:20 +01:00
|
|
|
end
|
|
|
|
|
2012-07-07 00:33:27 +02:00
|
|
|
if tag
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = stories.where(
|
|
|
|
Story.arel_table[:id].in(
|
|
|
|
Tagging.arel_table.where(
|
|
|
|
Tagging.arel_table[:tag_id].eq(tag.id)
|
|
|
|
).project(
|
|
|
|
Tagging.arel_table[:story_id]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-09-04 00:21:41 +02:00
|
|
|
elsif by_user
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = stories.where(:user_id => by_user)
|
2013-02-08 00:35:20 +01:00
|
|
|
elsif filtered_tag_ids.any?
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = stories.where(
|
|
|
|
Story.arel_table[:id].not_in(
|
|
|
|
Tagging.arel_table.where(
|
|
|
|
Tagging.arel_table[:tag_id].in(filtered_tag_ids)
|
|
|
|
).project(
|
|
|
|
Tagging.arel_table[:story_id]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2012-07-07 00:33:27 +02:00
|
|
|
end
|
|
|
|
|
2013-12-30 19:48:57 +01:00
|
|
|
stories = stories.includes(
|
2013-12-24 04:20:06 +01:00
|
|
|
:user, :taggings => :tag
|
|
|
|
).limit(
|
|
|
|
STORIES_PER_PAGE + 1
|
|
|
|
).offset(
|
|
|
|
(@page - 1) * STORIES_PER_PAGE
|
|
|
|
).order(
|
|
|
|
newest ? "stories.created_at DESC" : "hotness"
|
2013-12-30 23:29:00 +01:00
|
|
|
).to_a
|
2012-07-07 00:33:27 +02:00
|
|
|
|
2012-07-10 21:43:51 +02:00
|
|
|
show_more = false
|
2012-07-07 00:33:27 +02:00
|
|
|
|
|
|
|
if stories.count > STORIES_PER_PAGE
|
2012-07-10 21:43:51 +02:00
|
|
|
show_more = true
|
2012-07-07 00:33:27 +02:00
|
|
|
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
|
|
|
|
|
2012-07-10 21:43:51 +02:00
|
|
|
# eager load comment counts
|
|
|
|
if stories.any?
|
|
|
|
comment_counts = {}
|
2013-12-24 04:20:06 +01:00
|
|
|
Keystore.where(stories.map{|s|
|
2012-07-10 21:43:51 +02:00
|
|
|
"`key` = 'story:#{s.id}:comment_count'" }.join(" OR ")).each do |ks|
|
|
|
|
comment_counts[ks.key[/\d+/].to_i] = ks.value
|
|
|
|
end
|
|
|
|
|
|
|
|
stories.each do |s|
|
|
|
|
s._comment_count = comment_counts[s.id].to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[ stories, show_more ]
|
2012-07-07 00:33:27 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|