Update Users::Sidebar not to cache AR object (#8394)
This commit is contained in:
parent
d5ac4cd423
commit
8cb74b3e20
2 changed files with 31 additions and 26 deletions
|
|
@ -4,33 +4,39 @@ module Suggester
|
|||
def initialize(user, given_tag)
|
||||
@user = user
|
||||
@given_tag = given_tag
|
||||
@minimum_reaction_count = Rails.env.production? ? 25 : 0
|
||||
end
|
||||
|
||||
def suggest
|
||||
Rails.cache.fetch(generate_cache_name, expires_in: 120.hours) do
|
||||
reaction_count = Rails.env.production? ? 25 : 0
|
||||
user_ids = Article.published.tagged_with([given_tag], any: true).
|
||||
where("public_reactions_count > ?", reaction_count).
|
||||
where("published_at > ?", 4.months.ago).
|
||||
where("user_id != ?", user.id).
|
||||
where.not(user_id: user.following_by_type("User")).
|
||||
pluck(:user_id)
|
||||
group_one = User.select(:id, :name, :username, :profile_image, :summary).
|
||||
where(id: user_ids).
|
||||
order("reputation_modifier DESC").limit(20).to_a
|
||||
group_two = User.select(:id, :name, :username, :profile_image, :summary).
|
||||
where(id: user_ids).
|
||||
order(Arel.sql("RANDOM()")).limit(20).to_a
|
||||
(group_one + group_two).uniq
|
||||
suggested_user_ids = Rails.cache.fetch(generate_cache_name, expires_in: 120.hours) do
|
||||
(reputable_user_ids + random_user_ids).uniq
|
||||
end
|
||||
User.select(:id, :name, :username, :profile_image, :summary).where(id: suggested_user_ids)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :user, :given_tag
|
||||
attr_reader :user, :given_tag, :minimum_reaction_count
|
||||
|
||||
def generate_cache_name
|
||||
"tag-#{given_tag}_user-#{user.id}-#{user.last_followed_at}/tag-follow-sugggestions"
|
||||
"tag-#{given_tag}-user-#{user.id}-#{user.last_followed_at}/tag-follow-suggestions"
|
||||
end
|
||||
|
||||
def active_authors_for_given_tags
|
||||
@active_authors_for_given_tags ||= Article.published.tagged_with([given_tag], any: true).
|
||||
where("public_reactions_count >= ?", minimum_reaction_count).
|
||||
where("published_at > ?", 4.months.ago).
|
||||
where("user_id != ?", user.id).
|
||||
where.not(user_id: user.following_by_type("User")).
|
||||
pluck(:user_id)
|
||||
end
|
||||
|
||||
def reputable_user_ids
|
||||
User.where(id: active_authors_for_given_tags).order("reputation_modifier DESC").limit(20).pluck(:id)
|
||||
end
|
||||
|
||||
def random_user_ids
|
||||
User.where(id: active_authors_for_given_tags).order(Arel.sql("RANDOM()")).limit(20).pluck(:id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,17 +3,16 @@ require "rails_helper"
|
|||
RSpec.describe Suggester::Users::Sidebar, type: :service do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it "does not include calling user" do
|
||||
create_list(:user, 3)
|
||||
tags = []
|
||||
3.times { tags << create(:tag) }
|
||||
expect(described_class.new(user, tags).suggest).not_to include(user)
|
||||
it "returns user suggestions" do
|
||||
tags = "html"
|
||||
article1 = create(:article, tags: tags)
|
||||
article2 = create(:article, tags: tags)
|
||||
expect(described_class.new(user, tags).suggest.to_a).to eq([article1.user, article2.user])
|
||||
end
|
||||
|
||||
it "returns the same number created" do
|
||||
it "returns no user if there's not enough sample" do
|
||||
create_list(:user, 3)
|
||||
tags = []
|
||||
3.times { tags << create(:tag) }
|
||||
expect(described_class.new(user, tags).suggest.size).to eq(0)
|
||||
tags = create_list(:tag, 3)
|
||||
expect(described_class.new(user, tags).suggest).to be_empty
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue