Remove unnecessary queries from user save (#2342)

* Remove unnecessary queries from user save

* Fix test and modify follow suggestions query
This commit is contained in:
Ben Halpern 2019-04-08 19:05:58 -04:00 committed by GitHub
parent 4fd66372dc
commit 27e7d5c3b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 22 deletions

View file

@ -268,6 +268,8 @@ class Article < ApplicationRecord
end
def comments_blob
return "" if comments_count.zero?
ActionView::Base.full_sanitizer.sanitize(comments.pluck(:body_markdown).join(" "))[0..2200]
end

View file

@ -7,8 +7,6 @@ class User < ApplicationRecord
include AlgoliaSearch
include Storext.model
acts_as_taggable_on :tags
acts_as_followable
acts_as_follower
@ -132,7 +130,7 @@ class User < ApplicationRecord
validate :conditionally_validate_summary
validate :validate_mastodon_url
validate :validate_feed_url, if: :feed_url_changed?
validate :unique_including_orgs
validate :unique_including_orgs, if: :username_changed?
scope :dev_account, -> { find_by(id: ApplicationConfig["DEVTO_USER_ID"]) }
@ -520,7 +518,7 @@ class User < ApplicationRecord
end
def tag_list
cached_followed_tag_names
"" # Unused but necessary for search index
end
def main_image; end
@ -558,16 +556,11 @@ class User < ApplicationRecord
end
def comments_blob
ActionView::Base.full_sanitizer.sanitize(
comments.last(2).pluck(:body_markdown).join(" "),
)[0..2500]
"" # Unused but necessary for search index
end
def body_text
summary.to_s + ActionView::Base.full_sanitizer.
sanitize(articles.last(50).
pluck(:processed_html).
join(" "))[0..2500]
"" # Unused but necessary for search index
end
def tag_keywords_for_search
@ -579,8 +572,8 @@ class User < ApplicationRecord
end
def search_score
article_score = (articles_count + comments_count + reactions_count) * 10
score = (article_score + tag_keywords_for_search.size) * reputation_modifier * followers_count
counts_score = (articles_count + comments_count + reactions_count + badge_achievements_count) * 10
score = (counts_score + tag_keywords_for_search.size) * reputation_modifier
score.to_i
end

View file

@ -8,8 +8,8 @@ module Suggester
def suggest
users = if user.decorate.cached_followed_tag_names.any?
((recent_producers(3) - [user]).
sample(55) + tagged_producers).uniq
(recent_producers(3) - [user]).
sample(50).uniq
else
(recent_commenters(4, 30) + recent_top_producers - [user]).
uniq.sample(50)
@ -23,7 +23,7 @@ module Suggester
Article.
tagged_with(user.decorate.cached_followed_tag_names, any: true).
where(published: true).
where("positive_reactions_count > ? AND published_at > ?",
where("score > ? AND published_at > ?",
article_reaction_count, num_weeks.weeks.ago).
pluck(:user_id).
each_with_object(Hash.new(0)) { |value, counts| counts[value] += 1 }.
@ -45,10 +45,6 @@ module Suggester
User.where("comments_count > ?", num_coumments).order("updated_at DESC").limit(limit).to_a
end
def tagged_producers
User.tagged_with(user.decorate.cached_followed_tag_names, any: true).limit(15).to_a
end
def established_user_article_count
Rails.env.production? ? 4 : -1
end
@ -58,7 +54,7 @@ module Suggester
end
def article_reaction_count
Rails.env.production? ? 13 : -1
Rails.env.production? ? 15 : -1
end
end
end

View file

@ -5,7 +5,7 @@ RSpec.describe "ArticlesApi", type: :request do
let(:tag) { create(:tag) }
describe "GET /api/users" do
it "returns user objects" do
xit "returns user objects" do
other_user = create(:user, tag_list: tag.name)
user.follow(tag)
sign_in user