recommendations: open user recommendations to all users.

* Open recommendations to all users (not just gold).
* Show recommendations on all posts (not just posts after 2017).
* Allow users to browse recommendations for other users.
* Increase number of recommended posts returned.
* Change endpoints to /recommended_posts?user_id=1234 and
  /recommended_posts?post_id=1234 and add json/xml support.
This commit is contained in:
evazion
2019-12-01 00:34:30 -06:00
parent c4cdba0874
commit 41b30fc64c
7 changed files with 56 additions and 83 deletions

View File

@@ -1,62 +0,0 @@
module RecommenderService
extend self
SCORE_THRESHOLD = 5
def enabled?
Danbooru.config.recommender_server.present?
end
def available_for_post?(post)
return true if Rails.env.development?
enabled? && post.created_at > Date.civil(2017, 1, 1) && post.fav_count >= SCORE_THRESHOLD
end
def available_for_user?
enabled? && CurrentUser.is_gold?
end
def recommend_for_user(user_id)
ids = Cache.get("rsu:#{user_id}", 1.hour) do
resp = HTTParty.get(
"#{Danbooru.config.recommender_server}/recommend/#{user_id}",
Danbooru.config.httparty_options.merge(
basic_auth: {
username: "danbooru",
password: Danbooru.config.recommender_key
}
)
)
JSON.parse(resp.body)
end
Post.find(ids.map(&:first))
end
def recommend_for_post(post_id)
ids = Cache.get("rss:#{post_id}", 1.hour) do
resp = HTTParty.get(
"#{Danbooru.config.recommender_server}/similar/#{post_id}",
Danbooru.config.httparty_options.merge(
basic_auth: {
username: "danbooru",
password: Danbooru.config.recommender_key
}
)
)
JSON.parse(resp.body)
end
if ids.is_a?(Hash) # error state
return []
end
Post.find(ids.reject {|x| x[0] == post_id}.map(&:first))
end
def recommend(post_id: nil, user_id: nil)
if post_id
recommend_for_post(post_id)
elsif user_id
recommend_for_user(user_id)
end
end
end