hide saved search functionality if not enabled
This commit is contained in:
@@ -44,6 +44,12 @@ IQDB integration is now delegated to the [IQDBS service](https://github.com/r888
|
||||
You will need to install your own copy and enable the appropriate
|
||||
configuration settings.
|
||||
|
||||
### Listbooru Service
|
||||
|
||||
In order to access saved search functionality you will need to install and
|
||||
configure the [Listbooru service](https://github.com/r888888888/listbooru).
|
||||
|
||||
### Archive Service
|
||||
|
||||
In order to access versioned data for pools (and eventually posts) you will need to install and configure the [Archive service](https://github.com/r888888888/archives).
|
||||
In order to access versioned data for pools (and eventually posts) you will
|
||||
need to install and configure the [Archives service](https://github.com/r888888888/archives).
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class SavedSearchCategoryChangesController < ApplicationController
|
||||
before_filter :member_only
|
||||
before_filter :check_availabililty
|
||||
respond_to :html
|
||||
|
||||
def new
|
||||
@@ -11,4 +12,22 @@ class SavedSearchCategoryChangesController < ApplicationController
|
||||
flash[:notice] = "Saved searches will be renamed"
|
||||
redirect_to saved_searches_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_availabililty
|
||||
if !SavedSearch.enabled?
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
flash[:notice] = "Listbooru service is not configured. Saved searches are not available."
|
||||
redirect_to :back
|
||||
end
|
||||
format.json do
|
||||
render json: {success: false, reason: "Listbooru service is not configured"}.to_json, status: 501
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class SavedSearchesController < ApplicationController
|
||||
before_filter :member_only
|
||||
before_fitler :check_availability
|
||||
respond_to :html, :xml, :json, :js
|
||||
|
||||
def index
|
||||
@@ -47,6 +48,22 @@ class SavedSearchesController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def check_availabililty
|
||||
if !SavedSearch.enabled?
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
flash[:notice] = "Listbooru service is not configured. Saved searches are not available."
|
||||
redirect_to :back
|
||||
end
|
||||
format.json do
|
||||
render json: {success: false, reason: "Listbooru service is not configured"}.to_json, status: 501
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def saved_searches
|
||||
CurrentUser.user.saved_searches
|
||||
end
|
||||
|
||||
@@ -23,11 +23,12 @@ module Moderator
|
||||
tags = Tag.scan_tags(antecedent, :strip_metatags => true)
|
||||
conds = tags.map {|x| "tag_query like ?"}.join(" AND ")
|
||||
conds = [conds, *tags.map {|x| "%#{x}%"}]
|
||||
SavedSearch.where(*conds).find_each do |ss|
|
||||
ss.tag_query = (ss.tag_query_array - tags + antecedent).uniq.join(" ")
|
||||
ss.save
|
||||
if SavedSearch.enabled?
|
||||
SavedSearch.where(*conds).find_each do |ss|
|
||||
ss.tag_query = (ss.tag_query_array - tags + antecedent).uniq.join(" ")
|
||||
ss.save
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,15 +102,17 @@ class PostQueryBuilder
|
||||
end
|
||||
|
||||
def add_saved_search_relation(saved_searches, relation)
|
||||
saved_searches.each do |saved_search|
|
||||
if saved_search == "all"
|
||||
post_ids = SavedSearch.post_ids(CurrentUser.id)
|
||||
else
|
||||
post_ids = SavedSearch.post_ids(CurrentUser.id, saved_search)
|
||||
end
|
||||
if SavedSearch.enabled?
|
||||
saved_searches.each do |saved_search|
|
||||
if saved_search == "all"
|
||||
post_ids = SavedSearch.post_ids(CurrentUser.id)
|
||||
else
|
||||
post_ids = SavedSearch.post_ids(CurrentUser.id, saved_search)
|
||||
end
|
||||
|
||||
post_ids = [0] if post_ids.empty?
|
||||
relation = relation.where(["posts.id IN (?)", post_ids])
|
||||
post_ids = [0] if post_ids.empty?
|
||||
relation = relation.where(["posts.id IN (?)", post_ids])
|
||||
end
|
||||
end
|
||||
|
||||
relation
|
||||
|
||||
@@ -5,63 +5,96 @@ class SavedSearch < ActiveRecord::Base
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def enabled?
|
||||
Danbooru.config.aws_sqs_saved_search_url.present?
|
||||
end
|
||||
|
||||
def posts_search_available?
|
||||
Danbooru.config.listbooru_server.present? && CurrentUser.is_gold?
|
||||
enabled? && CurrentUser.is_gold?
|
||||
end
|
||||
|
||||
def sqs_service
|
||||
SqsService.new(Danbooru.config.aws_sqs_saved_search_url)
|
||||
end
|
||||
|
||||
def refresh_listbooru(user_id)
|
||||
return false unless Danbooru.config.listbooru_enabled?
|
||||
return false unless enabled?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
sqs.send_message("refresh\n#{user_id}")
|
||||
sqs_service.send_message("refresh\n#{user_id}")
|
||||
end
|
||||
|
||||
def reset_listbooru(user_id)
|
||||
return false unless Danbooru.config.listbooru_enabled?
|
||||
return false unless enabled?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
user = User.find(user_id)
|
||||
|
||||
sqs.send_message("delete\n#{user_id}\nall\n")
|
||||
sqs_service.send_message("delete\n#{user_id}\nall\n")
|
||||
|
||||
user.saved_searches.each do |saved_search|
|
||||
sqs.send_message("create\n#{user_id}\n#{saved_search.category}\n#{saved_search.tag_query}", :delay_seconds => 30)
|
||||
sqs_service.send_message("create\n#{user_id}\n#{saved_search.category}\n#{saved_search.tag_query}", :delay_seconds => 30)
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def rename_listbooru(user_id, old_category, new_category)
|
||||
return false unless Danbooru.config.listbooru_enabled?
|
||||
return false unless enabled?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
sqs.send_message("rename\n#{user_id}\n#{old_category}\n#{new_category}\n")
|
||||
sqs_service.send_message("rename\n#{user_id}\n#{old_category}\n#{new_category}\n")
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def post_ids(user_id, name = nil)
|
||||
return [] unless enabled?
|
||||
|
||||
if name
|
||||
hash_name = Cache.hash(name)
|
||||
else
|
||||
hash_name = nil
|
||||
end
|
||||
|
||||
body = Cache.get("ss-pids-#{user_id}-#{hash_name}", 60) do
|
||||
params = {
|
||||
"key" => Danbooru.config.listbooru_auth_key,
|
||||
"user_id" => user_id,
|
||||
"name" => name
|
||||
}
|
||||
uri = URI.parse("#{Danbooru.config.listbooru_server}/users")
|
||||
uri.query = URI.encode_www_form(params)
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
||||
resp = http.request_get(uri.request_uri)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
resp.body
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
body.to_s.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
end
|
||||
|
||||
def update_listbooru_on_create
|
||||
return unless Danbooru.config.listbooru_enabled?
|
||||
return unless SavedSearch.enabled?
|
||||
return unless user.is_gold?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
sqs.send_message("create\n#{user_id}\n#{category}\n#{tag_query}")
|
||||
SavedSearch.sqs_service.send_message("create\n#{user_id}\n#{category}\n#{tag_query}")
|
||||
end
|
||||
|
||||
def update_listbooru_on_destroy
|
||||
return unless Danbooru.config.listbooru_enabled?
|
||||
return unless SavedSearch.enabled?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
sqs.send_message("delete\n#{user_id}\n#{category}\n#{tag_query}")
|
||||
SavedSearch.sqs_service.send_message("delete\n#{user_id}\n#{category}\n#{tag_query}")
|
||||
end
|
||||
|
||||
def update_listbooru_on_update
|
||||
return unless Danbooru.config.listbooru_enabled?
|
||||
return unless SavedSearch.enabled?
|
||||
return unless user.is_gold?
|
||||
|
||||
sqs = SqsService.new(Danbooru.config.aws_sqs_queue_url)
|
||||
sqs.send_message("update\n#{user_id}\n#{category_was}\n#{tag_query_was}\n#{category}\n#{tag_query}")
|
||||
SavedSearch.sqs_service.send_message("update\n#{user_id}\n#{category_was}\n#{tag_query_was}\n#{category}\n#{tag_query}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -99,37 +132,6 @@ class SavedSearch < ActiveRecord::Base
|
||||
rename_listbooru(user_id, old_category, new_category)
|
||||
end
|
||||
|
||||
def self.post_ids(user_id, name = nil)
|
||||
return [] unless Danbooru.config.listbooru_enabled?
|
||||
|
||||
if name
|
||||
hash_name = Cache.hash(name)
|
||||
else
|
||||
hash_name = nil
|
||||
end
|
||||
|
||||
body = Cache.get("ss-pids-#{user_id}-#{hash_name}", 60) do
|
||||
params = {
|
||||
"key" => Danbooru.config.listbooru_auth_key,
|
||||
"user_id" => user_id,
|
||||
"name" => name
|
||||
}
|
||||
uri = URI.parse("#{Danbooru.config.listbooru_server}/users")
|
||||
uri.query = URI.encode_www_form(params)
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
||||
resp = http.request_get(uri.request_uri)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
resp.body
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
body.to_s.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
|
||||
def normalize
|
||||
self.category = SavedSearch.normalize_category(category) if category
|
||||
self.tag_query = SavedSearch.normalize(tag_query)
|
||||
|
||||
@@ -180,9 +180,12 @@ class TagAlias < ActiveRecord::Base
|
||||
|
||||
def move_saved_searches
|
||||
escaped = Regexp.escape(antecedent_name)
|
||||
SavedSearch.where("tag_query like ?", "%#{antecedent_name}%").find_each do |ss|
|
||||
ss.tag_query = ss.tag_query.sub(/(?:^| )#{escaped}(?:$| )/, " #{consequent_name} ").strip.gsub(/ /, " ")
|
||||
ss.save
|
||||
|
||||
if SavedSearch.enabled?
|
||||
SavedSearch.where("tag_query like ?", "%#{antecedent_name}%").find_each do |ss|
|
||||
ss.tag_query = ss.tag_query.sub(/(?:^| )#{escaped}(?:$| )/, " #{consequent_name} ").strip.gsub(/ /, " ")
|
||||
ss.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -847,15 +847,19 @@ class User < ActiveRecord::Base
|
||||
|
||||
module SavedSearchMethods
|
||||
def unique_saved_search_categories
|
||||
categories = saved_searches.pluck(:category)
|
||||
|
||||
if categories.any? {|x| x.blank?}
|
||||
categories.reject! {|x| x.blank?}
|
||||
categories.unshift(SavedSearch::UNCATEGORIZED_NAME)
|
||||
if SavedSearch.enabled?
|
||||
categories = saved_searches.pluck(:category)
|
||||
|
||||
if categories.any? {|x| x.blank?}
|
||||
categories.reject! {|x| x.blank?}
|
||||
categories.unshift(SavedSearch::UNCATEGORIZED_NAME)
|
||||
end
|
||||
|
||||
categories.uniq!
|
||||
categories
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
categories.uniq!
|
||||
categories
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ class UserPresenter
|
||||
end
|
||||
|
||||
def posts_for_saved_search_category(category)
|
||||
if !SavedSearch.enabled?
|
||||
return Post.where("false")
|
||||
end
|
||||
|
||||
if category == SavedSearch::UNCATEGORIZED_NAME
|
||||
ids = SavedSearch.post_ids(CurrentUser.user.id)
|
||||
else
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
<% if CurrentUser.show_saved_searches? %>
|
||||
<% if SavedSearch.enabled? && CurrentUser.show_saved_searches? %>
|
||||
<%= button_tag "Save search", :id => "save-search" %>
|
||||
<% end %>
|
||||
|
||||
<div id="save-search-dialog" title="Save Search" style="display: none;">
|
||||
<%= form_tag(saved_searches_path, :class => "simple_form", :remote => true) do %>
|
||||
<%= hidden_field_tag "saved_search_tags", params[:tags] %>
|
||||
<% if SavedSearch.enabled? %>
|
||||
<div id="save-search-dialog" title="Save Search" style="display: none;">
|
||||
<%= form_tag(saved_searches_path, :class => "simple_form", :remote => true) do %>
|
||||
<%= hidden_field_tag "saved_search_tags", params[:tags] %>
|
||||
|
||||
<div class="input">
|
||||
<label>
|
||||
<%= text_field_tag "saved_search_category", "", :placeholder => "Category" %>
|
||||
</label>
|
||||
</div>
|
||||
<div class="input">
|
||||
<label>
|
||||
<%= text_field_tag "saved_search_category", "", :placeholder => "Category" %>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<p><label><%= check_box_tag "saved_search_disable_categories" %> Disable categorization</label></p>
|
||||
<% end %>
|
||||
</div>
|
||||
<p><label><%= check_box_tag "saved_search_disable_categories" %> Disable categorization</label></p>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -43,12 +43,14 @@
|
||||
<td>2,000</td>
|
||||
<td>5,000</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Saved Searches</td>
|
||||
<td>No</td>
|
||||
<td>Yes</td>
|
||||
<td>Yes</td>
|
||||
</tr>
|
||||
<% if SavedSearch.enabled? %>
|
||||
<tr>
|
||||
<td>Saved Searches</td>
|
||||
<td>No</td>
|
||||
<td>Yes</td>
|
||||
<td>Yes</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
<tr>
|
||||
<td>See Censored Tags</td>
|
||||
<td>No</td>
|
||||
|
||||
@@ -480,7 +480,7 @@ module Danbooru
|
||||
false
|
||||
end
|
||||
|
||||
def aws_sqs_queue_url
|
||||
def aws_sqs_saved_search_url
|
||||
end
|
||||
|
||||
def aws_sqs_reltagcalc_url
|
||||
|
||||
20
test/helpers/saved_search_test_helper.rb
Normal file
20
test/helpers/saved_search_test_helper.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module SavedSearchTestHelper
|
||||
def mock_saved_search_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def initialize
|
||||
@commands = []
|
||||
end
|
||||
|
||||
def commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def send_message(msg)
|
||||
@commands << msg.split(/\n/).first
|
||||
end
|
||||
end
|
||||
|
||||
service = mock_sqs_service.new
|
||||
SavedSearch.stubs(:sqs_service).returns(service)
|
||||
end
|
||||
end
|
||||
@@ -1,13 +1,18 @@
|
||||
require 'test_helper'
|
||||
require 'helpers/saved_search_test_helper'
|
||||
|
||||
class SavedSearchTest < ActiveSupport::TestCase
|
||||
context "Fetching the post ids for a search" do
|
||||
setup do
|
||||
Danbooru.config.stubs(:listbooru_enabled?).returns(true)
|
||||
Danbooru.config.stubs(:listbooru_auth_key).returns("blahblahblah")
|
||||
Danbooru.config.stubs(:listbooru_server).returns("http://localhost:3001")
|
||||
end
|
||||
include SavedSearchTestHelper
|
||||
|
||||
def setup
|
||||
super
|
||||
mock_saved_search_service!
|
||||
Danbooru.config.stubs(:listbooru_enabled?).returns(true)
|
||||
Danbooru.config.stubs(:listbooru_auth_key).returns("blahblahblah")
|
||||
Danbooru.config.stubs(:listbooru_server).returns("http://localhost:3001")
|
||||
end
|
||||
|
||||
context "Fetching the post ids for a search" do
|
||||
context "with a name" do
|
||||
should "return a list of ids" do
|
||||
MEMCACHE.expects(:get).returns(nil)
|
||||
@@ -31,7 +36,7 @@ class SavedSearchTest < ActiveSupport::TestCase
|
||||
|
||||
context "Creating a saved search" do
|
||||
setup do
|
||||
@user = FactoryGirl.create(:user)
|
||||
@user = FactoryGirl.create(:gold_user)
|
||||
@saved_search = @user.saved_searches.create(:tag_query => " xxx ")
|
||||
end
|
||||
|
||||
@@ -43,12 +48,15 @@ class SavedSearchTest < ActiveSupport::TestCase
|
||||
should "normalize whitespace" do
|
||||
assert_equal("xxx", @saved_search.tag_query)
|
||||
end
|
||||
|
||||
should "send messages" do
|
||||
assert_equal(%w(create), SavedSearch.sqs_service.commands)
|
||||
end
|
||||
end
|
||||
|
||||
context "Destroying a saved search" do
|
||||
setup do
|
||||
SqsService.any_instance.stubs(:send_message)
|
||||
@user = FactoryGirl.create(:user)
|
||||
@user = FactoryGirl.create(:gold_user)
|
||||
@saved_search = @user.saved_searches.create(:tag_query => "xxx")
|
||||
@saved_search.destroy
|
||||
end
|
||||
@@ -57,11 +65,15 @@ class SavedSearchTest < ActiveSupport::TestCase
|
||||
@user.reload
|
||||
assert(!@user.has_saved_searches?, "should not have the saved_searches bitpref set")
|
||||
end
|
||||
|
||||
should "send messages" do
|
||||
assert_equal(%w(create delete), SavedSearch.sqs_service.commands)
|
||||
end
|
||||
end
|
||||
|
||||
context "A user with max saved searches" do
|
||||
setup do
|
||||
@user = FactoryGirl.create(:user)
|
||||
@user = FactoryGirl.create(:gold_user)
|
||||
User.any_instance.stubs(:max_saved_searches).returns(0)
|
||||
@saved_search = @user.saved_searches.create(:tag_query => "xxx")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user