docbrown/app/services/users/suggest_recent.rb
Michael Kohl 08d82e7256
Profile generalization: cleanup (#12947)
* Remove hardcoded instances of education field

* WIP Access display_email_on_profile via User Settings

* Remove unused profile column

* WIP Fix reference to bg_color_hex and text_color_hex

* WIP fix issues revealed by systems specs

* WIP fix issues revealed by services specs

* WIP Fix failing tests

* WIP Fix spec failures

* wip

* Move two attributes from controller to decorator

* Update comment

* Match user settings changes

* More consistently use user.tag_line

Even before the profile changes we sometimes used the user.summary
attribute directly but used the user.tag_line method in other places.

* Remove delegation, rename inline concern

* Drop profile columns from users table

* Remove duplicated work display from header

* Update work profile field handling

* Update DUS + spec

* Delegate more carefully

* Update delegation guard

* Adapt for removed delegation

* Undo accidental schema changes

* Fix seeds

* Remove accidentaly change

* Fix User#processed_website_url

* Update guard clause

* Update profile card content

* Add Organization#profile

* Be more conservative with profile fields

* Spec fixes round 1

* Fix typo

* Update spec

* Limit number of header fields and update card content

* Decorate correct model

* Update factory

* Update schema.rb

* Fix validation

* How bad could this possibly be?

* Pretty bad, nevermind

* Remove obsolete code

* Reset profile fields during test runs

* Move profile fields back to before(:suite)

* Spec fixes

* Remove accidentally re-added files

* More spec fixes

* Specs

* Change User#tag_keywords_for_search

* More spec fixes

* Add comment

* Undo accidental schema changes

* Attempt spec fix

* Remove fix attempt

* Fix e2e test

* Update spec

* Remove guard clause

* Remove hardcoded instances of education field

* WIP Access display_email_on_profile via User Settings

* Remove unused profile column

* WIP fix issues revealed by systems specs

* WIP fix issues revealed by services specs

* WIP Fix failing tests

* WIP Fix spec failures

* wip

* Move two attributes from controller to decorator

* Update comment

* More consistently use user.tag_line

Even before the profile changes we sometimes used the user.summary
attribute directly but used the user.tag_line method in other places.

* Remove education

* Add comment

* WIP

* Clean up mostly_work_with

* WIP

* Update work profile field handling

* More work-related changes

* Remove settings_only from display_area enum

* Remove quickfix from _metadata partial

* Remove special attributes

* Remove leftover spec

* Retrieve location from profile, not user

* Profile.special_attributes no longer exists

* Update specs

* More spec fixes

* Update UsersController

* Update UsersController and spec

* Fix e2e seeds

* Minor cleanup

* More e2e seed fixes

* Fix profile field CSV

* Fix e2e seeds

* Move one more attribute in e2e seeds

* Remove duplicate line

* Clear inputs before typing in them

* Fix formatting issues

* Profiles::Update -> Users::Update

* Remove RegistrationsController#resolve_profile_field_issues

* Fix schema.rb

* More cleanup

* Fix specs

* Fix remaining spec

Co-authored-by: Jacob Herrington <jacobherringtondeveloper@gmail.com>
2021-08-20 16:36:02 +02:00

77 lines
2.5 KiB
Ruby

module Users
class SuggestRecent
def self.call(user, attributes_to_select: [])
new(user, attributes_to_select: attributes_to_select).suggest
end
def initialize(user, attributes_to_select: [])
@user = user
@cached_followed_tag_names = user.decorate.cached_followed_tag_names
@attributes_to_select = attributes_to_select
end
def suggest
if cached_followed_tag_names.any?
(recent_producers(3) - [user]).sample(50).uniq
else
(recent_commenters(4, 30) + recent_top_producers - [user]).uniq.sample(50)
end
end
private
attr_reader :user, :attributes_to_select, :cached_followed_tag_names
def tagged_article_user_ids(num_weeks = 1)
Article.published
.tagged_with(cached_followed_tag_names.sample(5), any: true)
.where(score: article_score_average.., published_at: num_weeks.weeks.ago..)
.pluck(:user_id)
.each_with_object(Hash.new(0)) { |value, counts| counts[value] += 1 }
.sort_by { |_key, value| value }
.map(&:first)
end
def recent_producers(num_weeks = 1)
relation_as_array(
User.where(id: tagged_article_user_ids(num_weeks)),
limit: 80,
)
end
def recent_top_producers
relation = User.where(
articles_count: established_user_article_count..,
comments_count: established_user_comment_count..,
)
relation_as_array(relation, limit: 50)
end
def recent_commenters(num_comments = 2, limit = 8)
relation_as_array(User.where(comments_count: num_comments + 1..), limit: limit)
end
def relation_as_array(relation, limit:)
relation = relation.joins(:profile).select(attributes_to_select) if attributes_to_select.any?
relation.order(updated_at: :desc).limit(limit).to_a
end
def established_user_article_count
Rails.cache.fetch("established_user_article_count", expires_in: 1.day) do
User.where("articles_count > 0").average(:articles_count) || User.average(:articles_count)
end
end
def established_user_comment_count
Rails.cache.fetch("established_user_comment_count", expires_in: 1.day) do
User.where(comments_count: 1).average(:comments_count) || User.average(:comments_count)
end
end
def article_score_average
Rails.cache.fetch("article_score_average", expires_in: 1.day) do
Article.where(score: 0..).average(:score) || Article.average(:score)
end
end
end
end