allow non-logged-in users to define tag filters
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
This commit is contained in:
parent
92b794748f
commit
249dd85ec3
|
@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
TRAFFIC_DECREMENTER = 0.15
|
TRAFFIC_DECREMENTER = 0.15
|
||||||
|
|
||||||
|
TAG_FILTER_COOKIE = :tag_filters
|
||||||
|
|
||||||
def authenticate_user
|
def authenticate_user
|
||||||
if session[:u]
|
if session[:u]
|
||||||
@user = User.find_by_session_token(session[:u].to_s)
|
@user = User.find_by_session_token(session[:u].to_s)
|
||||||
|
@ -55,6 +57,12 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
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?
|
def user_is_spider?
|
||||||
!!request.env["HTTP_USER_AGENT"].to_s.match(/Googlebot/)
|
!!request.env["HTTP_USER_AGENT"].to_s.match(/Googlebot/)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
class FiltersController < ApplicationController
|
class FiltersController < ApplicationController
|
||||||
before_filter :require_logged_in_user
|
before_filter :authenticate_user
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@cur_url = "/filters"
|
@cur_url = "/filters"
|
||||||
@title = "Filtered Tags"
|
@title = "Filtered Tags"
|
||||||
|
|
||||||
@filtered_tags = @user.tag_filters.reload
|
if @user
|
||||||
|
@filtered_tags = @user.tag_filters.reload
|
||||||
|
else
|
||||||
|
@filtered_tags = tags_filtered_by_cookie.map{|t|
|
||||||
|
tf = TagFilter.new
|
||||||
|
tf.tag = t
|
||||||
|
tf
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
render :action => "index"
|
render :action => "index"
|
||||||
end
|
end
|
||||||
|
@ -20,22 +28,26 @@ class FiltersController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@user.tag_filters(:include => :tag).each do |tf|
|
if @user
|
||||||
if tf.tag && new_filters.include?(tf.tag.tag)
|
@user.tag_filters(:include => :tag).each do |tf|
|
||||||
new_filters.reject!{|t| t == tf.tag.tag }
|
if tf.tag && new_filters.include?(tf.tag.tag)
|
||||||
else
|
new_filters.reject!{|t| t == tf.tag.tag }
|
||||||
tf.destroy
|
else
|
||||||
|
tf.destroy
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
new_filters.each do |t|
|
||||||
|
tf = TagFilter.new
|
||||||
|
tf.user_id = @user.id
|
||||||
|
tf.tag_id = Tag.find_by_tag(t).id
|
||||||
|
tf.save!
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cookies.permanent[TAG_FILTER_COOKIE] = new_filters.join(",")
|
||||||
end
|
end
|
||||||
|
|
||||||
new_filters.each do |t|
|
flash[:success] = "Your filters have been updated."
|
||||||
tf = TagFilter.new
|
return redirect_to "/filters"
|
||||||
tf.user_id = @user.id
|
|
||||||
tf.tag_id = Tag.find_by_tag(t).id
|
|
||||||
tf.save
|
|
||||||
end
|
|
||||||
|
|
||||||
flash.now[:success] = "Your filters have been updated."
|
|
||||||
index
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -96,8 +96,9 @@ private
|
||||||
@page = params[:page].to_i
|
@page = params[:page].to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
# guest views have caching, but don't bother for logged-in users (or dev)
|
# guest views have caching, but don't bother for logged-in users or dev or
|
||||||
if Rails.env == "development" || user
|
# when the user has tag filters
|
||||||
|
if Rails.env == "development" || user || tags_filtered_by_cookie.any?
|
||||||
stories, @show_more =
|
stories, @show_more =
|
||||||
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
||||||
newest, by_user)
|
newest, by_user)
|
||||||
|
@ -129,7 +130,8 @@ private
|
||||||
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
||||||
else
|
else
|
||||||
# for logged-out users, filter defaults
|
# for logged-out users, filter defaults
|
||||||
filtered_tag_ids = Tag.where(:filtered_by_default => true).map{|t| t.id }
|
filtered_tag_ids = Tag.where(:filtered_by_default => true).
|
||||||
|
map{|t| t.id } + tags_filtered_by_cookie.map{|t| t.id }
|
||||||
end
|
end
|
||||||
|
|
||||||
if tag
|
if tag
|
||||||
|
|
|
@ -5,7 +5,7 @@ class Tag < ActiveRecord::Base
|
||||||
attr_accessor :filtered_count
|
attr_accessor :filtered_count
|
||||||
|
|
||||||
scope :accessible_to, ->(user) do
|
scope :accessible_to, ->(user) do
|
||||||
user.is_admin?? all : where(:privileged => false)
|
user && user.is_admin?? all : where(:privileged => false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.all_with_filtered_counts_for(user)
|
def self.all_with_filtered_counts_for(user)
|
||||||
|
|
|
@ -8,6 +8,14 @@
|
||||||
check them below.
|
check them below.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<% if !@user %>
|
||||||
|
<p>
|
||||||
|
Since you are not logged in, your filters will be stored in a long-lasting
|
||||||
|
browser cookie. To permanently store your tag filters and have them work
|
||||||
|
across browsers, <a href="/login">login</a> to your account.
|
||||||
|
</p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= form_tag "/filters", :method => :post do %>
|
<%= form_tag "/filters", :method => :post do %>
|
||||||
<table class="data zebra" cellspacing=0 width="75%">
|
<table class="data zebra" cellspacing=0 width="75%">
|
||||||
<tr>
|
<tr>
|
||||||
|
|
|
@ -66,10 +66,9 @@
|
||||||
|
|
||||||
<div id="headerright" class="<%= @user ? "loggedin" : "" %>">
|
<div id="headerright" class="<%= @user ? "loggedin" : "" %>">
|
||||||
<span class="headerlinks">
|
<span class="headerlinks">
|
||||||
|
<a href="/filters" <%= @cur_url == "/filters" ?
|
||||||
|
raw("class=\"cur_url\"") : "" %>>Filters</a>
|
||||||
<% if @user %>
|
<% if @user %>
|
||||||
<a href="/filters" <%= @cur_url == "/filters" ?
|
|
||||||
raw("class=\"cur_url\"") : "" %>>Filters</a>
|
|
||||||
|
|
||||||
<% if (count = @user.unread_message_count) > 0 %>
|
<% if (count = @user.unread_message_count) > 0 %>
|
||||||
<a href="/messages" class="new_messages <%= @cur_url == "/messages" ?
|
<a href="/messages" class="new_messages <%= @cur_url == "/messages" ?
|
||||||
"cur_url" : "" %>"><%= count %> New Message<%= count == 1 ? "" :
|
"cur_url" : "" %>"><%= count %> New Message<%= count == 1 ? "" :
|
||||||
|
|
Loading…
Reference in a new issue