forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comments count according to display rules in feed (forem#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
- Loading branch information
1 parent
ec32a8e
commit 51316c7
Showing
11 changed files
with
144 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20240306121718_add_displayed_comments_count_to_articles.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddDisplayedCommentsCountToArticles < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :articles, :displayed_comments_count, :integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Comments::CalculateScore, type: :service do | ||
let(:article) { create(:article) } | ||
let(:comment) { create(:comment, commentable: article) } | ||
let(:user) { instance_double(User, spam?: false) } | ||
|
||
before do | ||
allow(BlackBox).to receive(:comment_quality_score).and_return(7) | ||
end | ||
|
||
it "updates the score" do | ||
described_class.call(comment) | ||
comment.reload | ||
expect(comment.score).to be(7) | ||
end | ||
|
||
context "when adding spam role" do | ||
before do | ||
comment.user.add_role(:spam) | ||
comment.update_column(:updated_at, 1.day.ago) | ||
end | ||
|
||
it "updates the score and updated_at with a penalty if the user is a spammer", :aggregate_failures do | ||
described_class.call(comment) | ||
comment.reload | ||
expect(comment.score).to be(-493) | ||
expect(comment.updated_at).to be_within(1.minute).of(Time.current) | ||
end | ||
|
||
it "updates article displayed comments count" do | ||
other_comment = create(:comment, commentable: article) | ||
create(:comment, commentable: article, parent: other_comment) | ||
described_class.call(comment) | ||
article.reload | ||
expect(article.comments_count).to eq(3) | ||
expect(article.displayed_comments_count).to eq(2) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters