work on saved searches embedded in user profiles
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -55,6 +55,7 @@ gem 'responders'
|
||||
gem 'highline'
|
||||
gem 'dtext_rb', :git => "https://github.com/r888888888/dtext_rb.git", :require => "dtext"
|
||||
gem 'google-api-client'
|
||||
gem 'cityhash'
|
||||
|
||||
# needed for looser jpeg header compat
|
||||
gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes"
|
||||
|
||||
@@ -93,6 +93,7 @@ GEM
|
||||
capistrano3-unicorn (0.2.1)
|
||||
capistrano (~> 3.1, >= 3.1.0)
|
||||
chronic (0.10.2)
|
||||
cityhash (0.8.1)
|
||||
coderay (1.1.0)
|
||||
coffee-rails (4.1.0)
|
||||
coffee-script (>= 2.2.0)
|
||||
@@ -416,6 +417,7 @@ DEPENDENCIES
|
||||
capistrano-rails
|
||||
capistrano-rbenv
|
||||
capistrano3-unicorn
|
||||
cityhash
|
||||
coffee-rails
|
||||
coinbase
|
||||
daemons
|
||||
|
||||
@@ -81,4 +81,8 @@ class Cache
|
||||
def self.sanitize(key)
|
||||
key.gsub(/\W/) {|x| "%#{x.ord}"}.slice(0, 230)
|
||||
end
|
||||
|
||||
def self.hash(string)
|
||||
CityHash.hash64(string).to_s(36)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class SavedSearch < ActiveRecord::Base
|
||||
UNCATEGORIZED_NAME = "Uncategorized"
|
||||
|
||||
module ListbooruMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
@@ -79,22 +81,32 @@ class SavedSearch < ActiveRecord::Base
|
||||
def self.post_ids(user_id, name = nil)
|
||||
return [] unless Danbooru.config.listbooru_enabled?
|
||||
|
||||
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)
|
||||
if name
|
||||
hash_name = Cache.hash(name)
|
||||
else
|
||||
hash_name = nil
|
||||
end
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
resp = http.request_get(uri.request_uri)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
resp.body.scan(/\d+/).map(&:to_i)
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
body = Cache.fetch("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) 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.scan(/\d+/).map(&:to_i)
|
||||
end
|
||||
|
||||
def normalize
|
||||
|
||||
@@ -788,6 +788,12 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SavedSearchMethods
|
||||
def unique_saved_search_categories
|
||||
[SavedSearch::UNCATEGORIZED_NAME] + saved_searches.pluck(:category).reject {|x| x.blank?}.uniq
|
||||
end
|
||||
end
|
||||
|
||||
include BanMethods
|
||||
include NameMethods
|
||||
include PasswordMethods
|
||||
@@ -803,6 +809,7 @@ class User < ActiveRecord::Base
|
||||
include CountMethods
|
||||
extend SearchMethods
|
||||
include StatisticsMethods
|
||||
include SavedSearchMethods
|
||||
|
||||
def initialize_default_image_size
|
||||
self.default_image_size = "large"
|
||||
|
||||
@@ -39,6 +39,26 @@ class UserPresenter
|
||||
permissions.join(", ")
|
||||
end
|
||||
|
||||
def posts_for_saved_search_category(category)
|
||||
if category == SavedSearch::UNCATEGORIZED_NAME
|
||||
ids = SavedSearch.post_ids(CurrentUser.user.id)
|
||||
else
|
||||
ids = SavedSearch.post_ids(CurrentUser.user.id, category)
|
||||
end
|
||||
|
||||
if ids.any?
|
||||
arel = Post.where("id in (?)", id.map(&:to_i)).order("id desc").limit(10)
|
||||
|
||||
if CurrentUser.user.hide_deleted_posts?
|
||||
arel = arel.undeleted
|
||||
end
|
||||
|
||||
arel
|
||||
else
|
||||
Post.where("false")
|
||||
end
|
||||
end
|
||||
|
||||
def posts_for_subscription(subscription)
|
||||
arel = Post.where("id in (?)", subscription.post_id_array.map(&:to_i)).order("id desc").limit(6)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<% if category.present? %>
|
||||
<%= link_to_if SavedSearch.posts_search_available?, category.tr("_", " "), posts_path(:tags => "search:#{category}") %>
|
||||
<% else %>
|
||||
<%= link_to_if SavedSearch.posts_search_available?, "Uncategorized", posts_path(:tags => "search:all") %>
|
||||
<%= link_to_if SavedSearch.posts_search_available?, SavedSearch::UNCATEGORIZED_NAME, posts_path(:tags => "search:all") %>
|
||||
<% end %>
|
||||
</h2>
|
||||
<table class="striped" width="100%">
|
||||
|
||||
Reference in New Issue
Block a user