docbrown/app/services/comments/calculate_score.rb
Anna Buianova 51316c747f
Comments count according to display rules in feed (#20753)
* Move calculating comments score from worker to a service

* Display displayed comments count in feed

* Recalculating displayed comments counts on article show (if needed)

* Rubocop fixes

* Fixed lambda
2024-03-28 12:12:05 +00:00

32 lines
800 B
Ruby

module Comments
class CalculateScore
def self.call(...)
new(...).call
end
def initialize(comment)
@comment = comment
end
def call
score = BlackBox.comment_quality_score(comment)
score -= 500 if comment.user&.spam?
comment.update_columns(score: score, updated_at: Time.current)
comment.user&.touch(:last_comment_at)
# update commentable
commentable = comment.commentable
commentable.touch(:last_comment_at) if commentable.respond_to?(:last_comment_at)
Comments::Count.call(commentable, recalculate: true) if commentable.is_a?(Article)
# busting comment cache includes busting commentable cache
Comments::BustCacheWorker.new.perform(comment.id)
end
private
attr_reader :comment
end
end