when not logged in, store the filters in a long-lasting cookie and do not cache the home page for that one guy on hacker news that complained about lobste.rs not having this
76 lines
1.7 KiB
Ruby
76 lines
1.7 KiB
Ruby
class ApplicationController < ActionController::Base
|
|
protect_from_forgery
|
|
before_filter :authenticate_user
|
|
before_filter :increase_traffic_counter
|
|
|
|
TRAFFIC_DECREMENTER = 0.15
|
|
|
|
TAG_FILTER_COOKIE = :tag_filters
|
|
|
|
def authenticate_user
|
|
if session[:u]
|
|
@user = User.find_by_session_token(session[:u].to_s)
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
def increase_traffic_counter
|
|
@traffic = 1.0
|
|
|
|
if user_is_spider? || [ "json", "rss" ].include?(params[:format])
|
|
return true
|
|
end
|
|
|
|
Keystore.transaction do
|
|
date = (Keystore.value_for("traffic:date") || Time.now.to_i)
|
|
traffic = (Keystore.incremented_value_for("traffic:hits", 0).
|
|
to_f / 100.0) + 1.0
|
|
|
|
# every second, decrement traffic by some amount
|
|
@traffic = [ 1.0, traffic.to_f -
|
|
((Time.now.to_i - date) * TRAFFIC_DECREMENTER) ].max
|
|
|
|
Keystore.put("traffic:date", Time.now.to_i)
|
|
Keystore.put("traffic:hits", (@traffic * 100.0).to_i)
|
|
end
|
|
|
|
Rails.logger.info " Traffic level: #{@traffic}"
|
|
|
|
true
|
|
end
|
|
|
|
def require_logged_in_user
|
|
if @user
|
|
true
|
|
else
|
|
redirect_to "/login"
|
|
end
|
|
end
|
|
|
|
def require_logged_in_user_or_400
|
|
if @user
|
|
true
|
|
else
|
|
render :text => "not logged in", :status => 400
|
|
return false
|
|
end
|
|
end
|
|
|
|
@_tags_filtered = nil
|
|
def tags_filtered_by_cookie
|
|
@_tags_filtered ||= cookies[TAG_FILTER_COOKIE].to_s.split(",").map{|t|
|
|
Tag.find_by_tag(t) }.reject{|tf| !tf }
|
|
end
|
|
|
|
def user_is_spider?
|
|
!!request.env["HTTP_USER_AGENT"].to_s.match(/Googlebot/)
|
|
end
|
|
|
|
def find_user_from_rss_token
|
|
if !@user && request[:format] == "rss" && params[:token].to_s.present?
|
|
@user = User.find_by_rss_token(params[:token].to_s)
|
|
end
|
|
end
|
|
end
|