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
|
||||
|
||||
TAG_FILTER_COOKIE = :tag_filters
|
||||
|
||||
def authenticate_user
|
||||
if session[:u]
|
||||
@user = User.find_by_session_token(session[:u].to_s)
|
||||
|
@ -55,6 +57,12 @@ class ApplicationController < ActionController::Base
|
|||
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
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
class FiltersController < ApplicationController
|
||||
before_filter :require_logged_in_user
|
||||
before_filter :authenticate_user
|
||||
|
||||
def index
|
||||
@cur_url = "/filters"
|
||||
@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"
|
||||
end
|
||||
|
@ -20,22 +28,26 @@ class FiltersController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
@user.tag_filters(:include => :tag).each do |tf|
|
||||
if tf.tag && new_filters.include?(tf.tag.tag)
|
||||
new_filters.reject!{|t| t == tf.tag.tag }
|
||||
else
|
||||
tf.destroy
|
||||
if @user
|
||||
@user.tag_filters(:include => :tag).each do |tf|
|
||||
if tf.tag && new_filters.include?(tf.tag.tag)
|
||||
new_filters.reject!{|t| t == tf.tag.tag }
|
||||
else
|
||||
tf.destroy
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
flash.now[:success] = "Your filters have been updated."
|
||||
index
|
||||
flash[:success] = "Your filters have been updated."
|
||||
return redirect_to "/filters"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -96,8 +96,9 @@ private
|
|||
@page = params[:page].to_i
|
||||
end
|
||||
|
||||
# guest views have caching, but don't bother for logged-in users (or dev)
|
||||
if Rails.env == "development" || user
|
||||
# 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?
|
||||
stories, @show_more =
|
||||
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
||||
newest, by_user)
|
||||
|
@ -129,7 +130,8 @@ private
|
|||
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
||||
else
|
||||
# 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
|
||||
|
||||
if tag
|
||||
|
|
|
@ -5,7 +5,7 @@ class Tag < ActiveRecord::Base
|
|||
attr_accessor :filtered_count
|
||||
|
||||
scope :accessible_to, ->(user) do
|
||||
user.is_admin?? all : where(:privileged => false)
|
||||
user && user.is_admin?? all : where(:privileged => false)
|
||||
end
|
||||
|
||||
def self.all_with_filtered_counts_for(user)
|
||||
|
|
|
@ -8,6 +8,14 @@
|
|||
check them below.
|
||||
</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 %>
|
||||
<table class="data zebra" cellspacing=0 width="75%">
|
||||
<tr>
|
||||
|
|
|
@ -66,10 +66,9 @@
|
|||
|
||||
<div id="headerright" class="<%= @user ? "loggedin" : "" %>">
|
||||
<span class="headerlinks">
|
||||
<a href="/filters" <%= @cur_url == "/filters" ?
|
||||
raw("class=\"cur_url\"") : "" %>>Filters</a>
|
||||
<% if @user %>
|
||||
<a href="/filters" <%= @cur_url == "/filters" ?
|
||||
raw("class=\"cur_url\"") : "" %>>Filters</a>
|
||||
|
||||
<% if (count = @user.unread_message_count) > 0 %>
|
||||
<a href="/messages" class="new_messages <%= @cur_url == "/messages" ?
|
||||
"cur_url" : "" %>"><%= count %> New Message<%= count == 1 ? "" :
|
||||
|
|
Loading…
Reference in a new issue