diff --git a/app/controllers/recommended_posts_controller.rb b/app/controllers/recommended_posts_controller.rb
index 213a877a9..10b42ce57 100644
--- a/app/controllers/recommended_posts_controller.rb
+++ b/app/controllers/recommended_posts_controller.rb
@@ -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)
diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb
index 26ce1aae2..580c11e89 100644
--- a/app/javascript/src/javascripts/posts.js.erb
+++ b/app/javascript/src/javascripts/posts.js.erb
@@ -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();
diff --git a/app/logical/recommender_service.rb b/app/logical/recommender_service.rb
index b5f904caf..fe6798779 100644
--- a/app/logical/recommender_service.rb
+++ b/app/logical/recommender_service.rb
@@ -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
diff --git a/app/views/posts/partials/common/_secondary_links.html.erb b/app/views/posts/partials/common/_secondary_links.html.erb
index a95ce54c8..0076a0eb7 100644
--- a/app/views/posts/partials/common/_secondary_links.html.erb
+++ b/app/views/posts/partials/common/_secondary_links.html.erb
@@ -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}") %>
diff --git a/app/views/recommended_posts/_index.html.erb b/app/views/recommended_posts/_index.html.erb
new file mode 100644
index 000000000..7fbf2606c
--- /dev/null
+++ b/app/views/recommended_posts/_index.html.erb
@@ -0,0 +1,9 @@
+
Based on your favorites, you may enjoy these posts. Favorite more posts to get more accurate results. These recommendations update every few hours.
- - <%= render "posts/partials/common/inline_blacklist" %> - <%= render partial: "show" %> -