docbrown/app/services/email_digest_article_collector.rb
Michael Kohl 6dfabd578f
Rename SiteConfig to Settings::General (#13573)
* Rename SiteConfig

* More renaming

* Update spec

* Update mandatory settings mapping

* More renaming

* e2e test fixes

* You have a rename, and you have a rename

* Spec fix

* More changes

* Temporarily disable specs

* After-merge update

* Undo rename for migration

* undo rename of DUS

* Fix DUS

* Fix merge problem

* Remove redundant DUS

* Fix specs

* Remove unused code

* Change wrong class name

* More cleanup

* Re-add missing values to constant

* Fix constant

* Fix spec

* Remove obsolete fields

* Add accidentally removed field

* Update spec

* Move methods from Settings::General to ForemInstance

* Remove unneeded model

* Change mentions of 'site config'
2021-05-21 14:45:37 +02:00

74 lines
2.4 KiB
Ruby

class EmailDigestArticleCollector
include Instrumentation
ARTICLES_TO_SEND = "EmailDigestArticleCollector#articles_to_send".freeze
def initialize(user)
@user = user
end
def articles_to_send
# rubocop:disable Metrics/BlockLength
instrument ARTICLES_TO_SEND, tags: { user_id: @user.id } do
return [] unless should_receive_email?
articles = if user_has_followings?
experience_level_rating = (@user.experience_level || 5)
experience_level_rating_min = experience_level_rating - 3.6
experience_level_rating_max = experience_level_rating + 3.6
@user.followed_articles
.select(:title, :description, :path)
.published
.where("published_at > ?", cutoff_date)
.where(email_digest_eligible: true)
.where.not(user_id: @user.id)
.where("score > ?", 12)
.where("experience_level_rating > ? AND experience_level_rating < ?",
experience_level_rating_min, experience_level_rating_max)
.order(score: :desc)
.limit(6)
else
Article.select(:title, :description, :path)
.published
.where("published_at > ?", cutoff_date)
.where(featured: true, email_digest_eligible: true)
.where.not(user_id: @user.id)
.where("score > ?", 25)
.order(score: :desc)
.limit(6)
end
articles.length < 3 ? [] : articles
end
# rubocop:enable Metrics/BlockLength
end
private
def should_receive_email?
return true unless last_email_sent_at
# Has it been at least x days since @user received an email?
Time.current - last_email_sent_at >= Settings::General.periodic_email_digest
end
def last_email_sent_at
last_user_emails.last&.sent_at
end
def cutoff_date
a_few_days_ago = 4.days.ago.utc
return a_few_days_ago unless last_email_sent_at
[a_few_days_ago, last_email_sent_at].max
end
def user_has_followings?
@user.following_users_count.positive? || @user.cached_followed_tag_names.any?
end
def last_user_emails
@last_user_emails ||= @user.email_messages.select(:sent_at).where(mailer: "DigestMailer#digest_email").limit(10)
end
end