From 5de9adf0f51fc05931ba084b13e799e51ba30f42 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 19 Aug 2016 16:40:14 -0700 Subject: [PATCH] work on saved searches embedded in user profiles --- Gemfile | 1 + Gemfile.lock | 2 ++ app/logical/cache.rb | 4 +++ app/models/saved_search.rb | 38 ++++++++++++++++--------- app/models/user.rb | 7 +++++ app/presenters/user_presenter.rb | 20 +++++++++++++ app/views/saved_searches/index.html.erb | 2 +- 7 files changed, 60 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 16d8954cf..b90676312 100644 --- a/Gemfile +++ b/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" diff --git a/Gemfile.lock b/Gemfile.lock index 5b386d31a..922e19b1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/logical/cache.rb b/app/logical/cache.rb index 7110b9d51..78eafc2d8 100644 --- a/app/logical/cache.rb +++ b/app/logical/cache.rb @@ -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 diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index 0e3c8fe41..52b3025d0 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 5678b1cea..ed3cfa018 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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" diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb index a97b0b03f..ca9dfba60 100644 --- a/app/presenters/user_presenter.rb +++ b/app/presenters/user_presenter.rb @@ -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) diff --git a/app/views/saved_searches/index.html.erb b/app/views/saved_searches/index.html.erb index a043c70db..2fb2e628b 100644 --- a/app/views/saved_searches/index.html.erb +++ b/app/views/saved_searches/index.html.erb @@ -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 %>