2012-06-17 01:15:46 +00:00
|
|
|
class HomeController < ApplicationController
|
2018-05-10 15:09:47 +00:00
|
|
|
caches_page :about, :chat, :index, :newest, :newest_by_user, :recent, :top, if: CACHE_PAGE
|
|
|
|
|
2012-09-18 15:38:03 +00:00
|
|
|
# for rss feeds, load the user's tag filters if a token is passed
|
2018-03-14 14:11:03 +00:00
|
|
|
before_action :find_user_from_rss_token, :only => [:index, :newest, :saved]
|
2017-06-18 16:04:32 +00:00
|
|
|
before_action { @page = page }
|
2018-03-14 14:11:03 +00:00
|
|
|
before_action :require_logged_in_user, :only => [:upvoted]
|
2012-09-18 15:38:03 +00:00
|
|
|
|
2017-04-17 17:19:41 +00:00
|
|
|
def four_oh_four
|
|
|
|
begin
|
|
|
|
@title = "Resource Not Found"
|
|
|
|
render :action => "404", :status => 404
|
|
|
|
rescue ActionView::MissingTemplate
|
2017-06-18 22:40:37 +00:00
|
|
|
render :html => ("<div class=\"box wide\">" <<
|
2017-04-17 17:19:41 +00:00
|
|
|
"<div class=\"legend\">404</div>" <<
|
|
|
|
"Resource not found" <<
|
2017-06-18 22:40:37 +00:00
|
|
|
"</div>").html_safe, :layout => "application"
|
2017-04-17 17:19:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-03 15:26:10 +00:00
|
|
|
def about
|
|
|
|
begin
|
2016-05-02 22:56:16 +00:00
|
|
|
@title = "About"
|
2014-04-03 15:26:10 +00:00
|
|
|
render :action => "about"
|
2017-04-17 17:19:41 +00:00
|
|
|
rescue ActionView::MissingTemplate
|
2017-06-18 22:40:37 +00:00
|
|
|
render :html => ("<div class=\"box wide\">" <<
|
2014-04-03 15:26:10 +00:00
|
|
|
"A mystery." <<
|
2017-06-18 22:40:37 +00:00
|
|
|
"</div>").html_safe, :layout => "application"
|
2014-04-03 15:26:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-11 17:53:48 +00:00
|
|
|
def chat
|
2016-04-13 02:34:04 +00:00
|
|
|
begin
|
2016-05-02 22:56:16 +00:00
|
|
|
@title = "Chat"
|
2016-04-13 02:34:04 +00:00
|
|
|
render :action => "chat"
|
2017-04-17 17:19:41 +00:00
|
|
|
rescue ActionView::MissingTemplate
|
2017-06-18 22:40:37 +00:00
|
|
|
render :html => ("<div class=\"box wide\">" <<
|
2017-04-17 17:19:41 +00:00
|
|
|
"<div class=\"legend\">Chat</div>" <<
|
2016-04-13 02:34:04 +00:00
|
|
|
"Keep it on-site" <<
|
2017-06-18 22:40:37 +00:00
|
|
|
"</div>").html_safe, :layout => "application"
|
2016-04-13 02:34:04 +00:00
|
|
|
end
|
2015-10-11 17:53:48 +00:00
|
|
|
end
|
|
|
|
|
2014-07-07 16:05:53 +00:00
|
|
|
def privacy
|
|
|
|
begin
|
2016-05-02 22:56:16 +00:00
|
|
|
@title = "Privacy"
|
2014-07-07 16:05:53 +00:00
|
|
|
render :action => "privacy"
|
2017-04-17 17:19:41 +00:00
|
|
|
rescue ActionView::MissingTemplate
|
2018-02-03 09:17:46 +00:00
|
|
|
render :html => ("<div class=\"box wide\">" <<
|
2018-03-14 15:15:35 +00:00
|
|
|
"You apparently have no privacy." <<
|
2018-03-21 20:19:48 +00:00
|
|
|
"</div>").html_safe, :layout => "application"
|
2014-07-07 16:05:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-03 23:14:33 +00:00
|
|
|
def hidden
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(hidden: true) {
|
|
|
|
paginate stories.hidden
|
|
|
|
}
|
2014-03-03 23:14:33 +00:00
|
|
|
|
|
|
|
@heading = @title = "Hidden Stories"
|
|
|
|
@cur_url = "/hidden"
|
|
|
|
|
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
2012-06-30 22:43:45 +00:00
|
|
|
def index
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(hottest: true) {
|
|
|
|
paginate stories.hottest
|
|
|
|
}
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2018-03-14 13:01:14 +00:00
|
|
|
@rss_link ||= {
|
|
|
|
:title => "RSS 2.0",
|
2018-03-18 00:00:47 +00:00
|
|
|
:href => user_token_link("/rss"),
|
2018-03-14 13:01:14 +00:00
|
|
|
}
|
|
|
|
@comments_rss_link ||= {
|
|
|
|
:title => "Comments - RSS 2.0",
|
2018-03-18 00:00:47 +00:00
|
|
|
:href => user_token_link("/comments.rss"),
|
2018-03-14 13:01:14 +00:00
|
|
|
}
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2012-09-07 14:18:15 +00:00
|
|
|
@heading = @title = ""
|
2012-07-11 20:34:53 +00:00
|
|
|
@cur_url = "/"
|
|
|
|
|
2012-06-30 23:42:53 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
2012-09-18 15:38:03 +00:00
|
|
|
format.rss {
|
2017-06-05 14:49:59 +00:00
|
|
|
if @user
|
2012-09-18 15:38:03 +00:00
|
|
|
@title = "Private feed for #{@user.username}"
|
2017-06-05 14:49:59 +00:00
|
|
|
render :action => "rss", :layout => false
|
|
|
|
else
|
|
|
|
content = Rails.cache.fetch("rss", :expires_in => (60 * 2)) {
|
|
|
|
render_to_string :action => "rss", :layout => false
|
|
|
|
}
|
2017-06-18 16:04:32 +00:00
|
|
|
render :plain => content, :layout => false
|
2012-09-18 15:38:03 +00:00
|
|
|
end
|
|
|
|
}
|
2012-12-17 02:05:27 +00:00
|
|
|
format.json { render :json => @stories }
|
2012-06-17 01:15:46 +00:00
|
|
|
end
|
2012-06-30 23:42:53 +00:00
|
|
|
end
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2012-06-30 23:42:53 +00:00
|
|
|
def newest
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(newest: true) {
|
|
|
|
paginate stories.newest
|
|
|
|
}
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2012-09-07 14:18:15 +00:00
|
|
|
@heading = @title = "Newest Stories"
|
2012-07-11 20:34:53 +00:00
|
|
|
@cur_url = "/newest"
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2018-03-14 13:01:14 +00:00
|
|
|
@rss_link = {
|
|
|
|
:title => "RSS 2.0 - Newest Items",
|
2018-03-18 00:00:47 +00:00
|
|
|
:href => user_token_link("/newest.rss"),
|
2018-03-14 13:01:14 +00:00
|
|
|
}
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2012-06-30 23:42:53 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
2012-09-18 15:38:03 +00:00
|
|
|
format.rss {
|
|
|
|
if @user && params[:token].present?
|
|
|
|
@title += " - Private feed for #{@user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "rss", :layout => false
|
|
|
|
}
|
2012-12-17 02:05:27 +00:00
|
|
|
format.json { render :json => @stories }
|
2012-06-30 23:42:53 +00:00
|
|
|
end
|
2012-06-30 19:14:35 +00:00
|
|
|
end
|
|
|
|
|
2012-09-03 22:21:41 +00:00
|
|
|
def newest_by_user
|
2014-01-25 16:38:07 +00:00
|
|
|
by_user = User.where(:username => params[:user]).first!
|
2012-09-03 22:21:41 +00:00
|
|
|
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(by_user: by_user) {
|
2014-08-16 14:08:24 +00:00
|
|
|
paginate stories.newest_by_user(by_user)
|
2014-08-04 01:47:13 +00:00
|
|
|
}
|
2013-02-14 00:50:51 +00:00
|
|
|
|
2014-01-25 16:38:07 +00:00
|
|
|
@heading = @title = "Newest Stories by #{by_user.username}"
|
|
|
|
@cur_url = "/newest/#{by_user.username}"
|
2012-09-03 22:21:41 +00:00
|
|
|
|
|
|
|
@newest = true
|
2014-01-25 16:38:07 +00:00
|
|
|
@for_user = by_user.username
|
2012-09-03 22:21:41 +00:00
|
|
|
|
2016-04-29 16:39:04 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss {
|
|
|
|
render :action => "rss", :layout => false
|
|
|
|
}
|
|
|
|
format.json { render :json => @stories }
|
|
|
|
end
|
2012-09-03 22:21:41 +00:00
|
|
|
end
|
|
|
|
|
2014-01-25 17:13:14 +00:00
|
|
|
def recent
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(recent: true) {
|
2018-04-26 15:07:53 +00:00
|
|
|
paginate Story.recent(@user, filtered_tag_ids)
|
2014-08-04 01:47:13 +00:00
|
|
|
}
|
2014-01-25 17:13:14 +00:00
|
|
|
|
|
|
|
@heading = @title = "Recent Stories"
|
|
|
|
@cur_url = "/recent"
|
|
|
|
|
2018-04-26 15:07:53 +00:00
|
|
|
# our list is unstable because upvoted stories get removed, so point at /newest.rss
|
|
|
|
@rss_link = { :title => "RSS 2.0 - Newest Items", :href => user_token_link("/newest.rss") }
|
2014-03-12 22:50:51 +00:00
|
|
|
|
2014-01-25 17:13:14 +00:00
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
2017-07-13 20:38:33 +00:00
|
|
|
def saved
|
|
|
|
@stories, @show_more = get_from_cache(hidden: true) {
|
|
|
|
paginate stories.saved
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:01:14 +00:00
|
|
|
@rss_link ||= {
|
|
|
|
:title => "RSS 2.0",
|
2018-03-18 00:00:47 +00:00
|
|
|
:href => user_token_link("/saved.rss"),
|
2018-03-14 13:01:14 +00:00
|
|
|
}
|
2017-07-13 20:38:33 +00:00
|
|
|
|
|
|
|
@heading = @title = "Saved Stories"
|
|
|
|
@cur_url = "/saved"
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss {
|
|
|
|
if @user
|
|
|
|
@title = "Private feed of saved stories for #{@user.username}"
|
|
|
|
end
|
2018-03-19 21:15:24 +00:00
|
|
|
render :action => "rss", :layout => false
|
2017-07-13 20:38:33 +00:00
|
|
|
}
|
|
|
|
format.json { render :json => @stories }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-17 01:15:46 +00:00
|
|
|
def tagged
|
2013-12-24 03:20:06 +00:00
|
|
|
@tag = Tag.where(:tag => params[:tag]).first!
|
2014-01-25 16:38:07 +00:00
|
|
|
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(tag: @tag) {
|
|
|
|
paginate stories.tagged(@tag)
|
|
|
|
}
|
2012-06-30 23:42:53 +00:00
|
|
|
|
2018-05-02 12:45:18 +00:00
|
|
|
@heading = @title = [@tag.tag, @tag.description].compact.join(' - ')
|
2012-07-11 20:34:53 +00:00
|
|
|
@cur_url = tag_url(@tag.tag)
|
2012-06-17 01:15:46 +00:00
|
|
|
|
2018-03-14 13:01:14 +00:00
|
|
|
@rss_link = {
|
|
|
|
:title => "RSS 2.0 - Tagged #{@tag.tag} (#{@tag.description})",
|
|
|
|
:href => "/t/#{@tag.tag}.rss",
|
|
|
|
}
|
2012-06-30 23:42:53 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss { render :action => "rss", :layout => false }
|
2015-08-04 00:12:39 +00:00
|
|
|
format.json { render :json => @stories }
|
2012-06-30 23:42:53 +00:00
|
|
|
end
|
2012-06-17 01:15:46 +00:00
|
|
|
end
|
2012-07-06 22:33:27 +00:00
|
|
|
|
2018-03-18 00:37:50 +00:00
|
|
|
TOP_INTVS = { "d" => "Day", "w" => "Week", "m" => "Month", "y" => "Year" }.freeze
|
2014-04-03 18:20:24 +00:00
|
|
|
def top
|
|
|
|
@cur_url = "/top"
|
|
|
|
length = { :dur => 1, :intv => "Week" }
|
|
|
|
|
2018-03-14 17:10:17 +00:00
|
|
|
if (m = params[:length].to_s.match(/\A(\d+)([#{TOP_INTVS.keys.join}])\z/))
|
2014-04-03 18:20:24 +00:00
|
|
|
length[:dur] = m[1].to_i
|
|
|
|
length[:intv] = TOP_INTVS[m[2]]
|
|
|
|
|
|
|
|
@cur_url << "/#{params[:length]}"
|
|
|
|
end
|
|
|
|
|
2014-08-04 01:47:13 +00:00
|
|
|
@stories, @show_more = get_from_cache(top: true, length: length) {
|
|
|
|
paginate stories.top(length)
|
|
|
|
}
|
2014-04-03 18:20:24 +00:00
|
|
|
|
|
|
|
if length[:dur] > 1
|
|
|
|
@heading = @title = "Top Stories of the Past #{length[:dur]} " <<
|
2018-03-14 15:15:35 +00:00
|
|
|
length[:intv] << "s"
|
2014-04-03 18:20:24 +00:00
|
|
|
else
|
|
|
|
@heading = @title = "Top Stories of the Past " << length[:intv]
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
2013-03-11 18:19:26 +00:00
|
|
|
def upvoted
|
|
|
|
@stories, @show_more = get_from_cache(upvoted: true, user: @user) {
|
|
|
|
paginate @user.upvoted_stories.order('votes.id DESC')
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:10:32 +00:00
|
|
|
@heading = @title = "Your Upvoted Stories"
|
2013-03-11 18:19:26 +00:00
|
|
|
@cur_url = "/upvoted"
|
|
|
|
|
2018-03-14 13:01:14 +00:00
|
|
|
@rss_link = {
|
|
|
|
:title => "RSS 2.0 - Your Upvoted Stories",
|
2018-03-18 00:00:47 +00:00
|
|
|
:href => user_token_link("/upvoted.rss"),
|
2018-03-14 13:01:14 +00:00
|
|
|
}
|
2013-03-11 18:19:26 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss {
|
|
|
|
if @user && params[:token].present?
|
|
|
|
@title += " - Private feed for #{@user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "rss", :layout => false
|
|
|
|
}
|
|
|
|
format.json { render :json => @stories }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-06 22:33:27 +00:00
|
|
|
private
|
2018-03-02 04:50:11 +00:00
|
|
|
|
2014-07-07 16:05:53 +00:00
|
|
|
def filtered_tag_ids
|
|
|
|
if @user
|
2018-03-18 05:51:58 +00:00
|
|
|
@user.tag_filters.map(&:tag_id)
|
2012-07-10 19:43:51 +00:00
|
|
|
else
|
2018-03-18 05:51:58 +00:00
|
|
|
tags_filtered_by_cookie.map(&:id)
|
2012-07-10 19:43:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-07 16:05:53 +00:00
|
|
|
def stories
|
|
|
|
StoryRepository.new(@user, exclude_tags: filtered_tag_ids)
|
|
|
|
end
|
2012-07-06 22:33:27 +00:00
|
|
|
|
2014-07-07 16:05:53 +00:00
|
|
|
def page
|
2016-07-31 17:36:28 +00:00
|
|
|
p = params[:page].to_i
|
|
|
|
if p == 0
|
|
|
|
p = 1
|
|
|
|
elsif p < 0 || p > (2 ** 32)
|
|
|
|
raise ActionController::RoutingError.new("page out of bounds")
|
|
|
|
end
|
|
|
|
p
|
2014-07-07 16:05:53 +00:00
|
|
|
end
|
2012-07-06 22:33:27 +00:00
|
|
|
|
2014-07-07 16:05:53 +00:00
|
|
|
def paginate(scope)
|
|
|
|
StoriesPaginator.new(scope, page, @user).get
|
|
|
|
end
|
2013-02-07 23:35:20 +00:00
|
|
|
|
2018-03-19 19:30:49 +00:00
|
|
|
def get_from_cache(opts = {}, &block)
|
2014-07-07 16:05:53 +00:00
|
|
|
if Rails.env.development? || @user || tags_filtered_by_cookie.any?
|
|
|
|
yield
|
2014-04-07 01:43:16 +00:00
|
|
|
else
|
2018-03-19 01:21:55 +00:00
|
|
|
key = opts.merge(page: page).sort.map {|k, v| "#{k}=#{v.to_param}" }.join(" ")
|
2017-03-09 17:20:32 +00:00
|
|
|
begin
|
|
|
|
Rails.cache.fetch("stories #{key}", :expires_in => 45, &block)
|
|
|
|
rescue Errno::ENOENT => e
|
|
|
|
Rails.logger.error "error fetching stories #{key}: #{e}"
|
|
|
|
yield
|
|
|
|
end
|
2012-07-06 22:33:27 +00:00
|
|
|
end
|
|
|
|
end
|
2018-03-18 00:00:47 +00:00
|
|
|
|
|
|
|
def user_token_link(url)
|
2018-03-21 20:56:07 +00:00
|
|
|
@user ? "#{url}?token=#{@user.rss_token}" : url
|
2018-03-18 00:00:47 +00:00
|
|
|
end
|
2012-06-17 01:15:46 +00:00
|
|
|
end
|