recommendations: add search form, allow searching by username.

This commit is contained in:
evazion
2019-12-02 02:04:46 -06:00
parent ae46f7a665
commit efda9f37e1
11 changed files with 57 additions and 40 deletions

View File

@@ -1,9 +1,9 @@
class RecommendedPostsController < ApplicationController
respond_to :html, :json, :xml, :js
def show
def index
limit = params.fetch(:limit, 100).to_i.clamp(0, 200)
@recs = RecommenderService.search(params).take(limit)
@recs = RecommenderService.search(search_params).take(limit)
@posts = @recs.map { |rec| rec[:post] }
respond_with(@recs)

View File

@@ -434,7 +434,7 @@ Post.initialize_post_sections = function() {
$("#comments").hide();
$("#edit").hide();
$("#recommended").show();
$.get("/recommended_posts.js", { post_id: Utility.meta("post-id"), max_recommendations: Post.MAX_RECOMMENDATIONS });
$.get("/recommended_posts.js", { search: { post_id: Utility.meta("post-id") }, limit: Post.MAX_RECOMMENDATIONS });
} else {
$("#edit").hide();
$("#comments").hide();

View File

@@ -17,27 +17,27 @@ module RecommenderService
enabled? && user.favorite_count > MIN_USER_FAVS
end
def recommend_for_user(user_id, limit = 50)
body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/recommend/#{user_id}", params: { limit: limit }, expiry: CACHE_LIFETIME)
def recommend_for_user(user, limit = 50)
body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/recommend/#{user.id}", params: { limit: limit }, expiry: CACHE_LIFETIME)
return [] if status != 200
process_recs(body, uploader_id: user_id, favoriter_id: user_id)
process_recs(body, uploader: user, favoriter: user)
end
def recommend_for_post(post_id, limit = 50)
body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/similar/#{post_id}", params: { limit: limit }, expiry: CACHE_LIFETIME)
def recommend_for_post(post, limit = 50)
body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/similar/#{post.id}", params: { limit: limit }, expiry: CACHE_LIFETIME)
return [] if status != 200
process_recs(body, post_id: post_id)
process_recs(body, post: post)
end
def process_recs(recs, post_id: nil, uploader_id: nil, favoriter_id: nil)
def process_recs(recs, uploader: nil, favoriter: nil)
recs = JSON.parse(recs)
posts = Post.where(id: recs.map(&:first))
posts = posts.where.not(id: post_id) if post_id
posts = posts.where.not(uploader_id: uploader_id) if uploader_id
posts = posts.where.not(id: Favorite.where(user_id: favoriter_id).select(:post_id)) if favoriter_id
posts = posts.where.not(id: post.id) if post
posts = posts.where.not(uploader_id: uploader.id) if uploader
posts = posts.where.not(id: favoriter.favorites.select(:post_id)) if favoriter
id_to_score = recs.to_h
recs = posts.map { |post| { score: id_to_score[post.id], post: post } }
@@ -46,14 +46,21 @@ module RecommenderService
end
def search(params)
if params[:user_id].present?
if params[:user_name].present?
user = User.find_by_name(params[:user_name])
elsif params[:user_id].present?
user = User.find(params[:user_id])
elsif params[:post_id].present?
post = Post.find(params[:post_id])
end
if user.present?
raise User::PrivilegeError if user.hide_favorites?
max_recommendations = params.fetch(:max_recommendations, user.favorite_count + 500).to_i.clamp(0, 50000)
recs = RecommenderService.recommend_for_user(params[:user_id], max_recommendations)
elsif params[:post_id].present?
max_recommendations = params.fetch(:max_recommendations, 50).to_i.clamp(0, 200)
recs = RecommenderService.recommend_for_post(params[:post_id], max_recommendations)
recs = RecommenderService.recommend_for_user(user, max_recommendations)
elsif post.present?
max_recommendations = params.fetch(:max_recommendations, 100).to_i.clamp(0, 1000)
recs = RecommenderService.recommend_for_post(post, max_recommendations)
else
recs = []
end

View File

@@ -3,7 +3,7 @@
<%= subnav_link_to "Upload", new_upload_path %>
<%= subnav_link_to "Hot", posts_path(:tags => "order:rank", :d => "1") %>
<% if RecommenderService.available_for_user?(CurrentUser.user) %>
<%= subnav_link_to "Recommended", recommended_posts_path(user_id: CurrentUser.id) %>
<%= subnav_link_to "Recommended", recommended_posts_path(search: { user_name: CurrentUser.name }) %>
<% end %>
<% unless CurrentUser.is_anonymous? %>
<%= subnav_link_to "Favorites", posts_path(tags: "ordfav:#{CurrentUser.user.name}") %>

View File

@@ -0,0 +1,9 @@
<section class="recommended-posts user-disable-cropped-<%= Danbooru.config.enable_image_cropping && CurrentUser.user.disable_cropped_thumbnails? %>">
<% if @recs.empty? %>
No recommendations found.
<% end %>
<% @recs.each do |rec| %>
<%= PostPresenter.preview(rec[:post], recommended: 100*rec[:score]) %>
<% end %>
</section>

View File

@@ -1,3 +0,0 @@
<section class="recommended-posts user-disable-cropped-<%= Danbooru.config.enable_image_cropping && CurrentUser.user.disable_cropped_thumbnails? %>">
<%= PostSets::Recommended.new(@posts).presenter.post_previews_html(self) %>
</section>

View File

@@ -0,0 +1,20 @@
<div id="c-recommended-posts">
<div id="a-index">
<h1>Recommended Posts</h1>
<%= search_form_for(recommended_posts_path) do |f| %>
<%= f.input :user_name, label: "User", input_html: { value: params[:search][:user_name], "data-autocomplete": "user" } %>
<%= f.input :post_id, label: "Post", input_html: { value: params[:search][:post_id] } %>
<%= f.submit "Search" %>
<% end %>
<%= render "posts/partials/common/inline_blacklist" %>
<%= render partial: "index" %>
</div>
</div>
<%= render "posts/partials/common/secondary_links" %>
<% content_for(:page_title) do %>
Recommended Posts - <%= Danbooru.config.app_name %>
<% end %>

View File

@@ -0,0 +1 @@
$("#recommended").html("<%= j render "recommended_posts/index" %>");

View File

@@ -1,16 +0,0 @@
<div id="c-recommended-posts">
<div id="a-show">
<h1>Recommended Posts</h1>
<p>Based on your favorites, you may enjoy these posts. Favorite more posts to get more accurate results. These recommendations update every few hours.</p>
<%= render "posts/partials/common/inline_blacklist" %>
<%= render partial: "show" %>
</div>
</div>
<%= render "posts/partials/common/secondary_links" %>
<% content_for(:page_title) do %>
Recommended Posts - <%= Danbooru.config.app_name %>
<% end %>

View File

@@ -1 +0,0 @@
$("#recommended").html("<%= j render "recommended_posts/show" %>");

View File

@@ -241,7 +241,7 @@ Rails.application.routes.draw do
get "reports/upload_tags" => "reports#upload_tags"
get "reports/down_voting_post" => "reports#down_voting_post"
post "reports/down_voting_post_create" => "reports#down_voting_post_create"
resource :recommended_posts, only: [:show]
resources :recommended_posts, only: [:index]
resources :saved_searches, :except => [:show] do
collection do
get :labels