Adjust comment notification logic (#20534)

* Adjust comment notification logic

* Update spec/models/comment_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update spec/models/comment_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update spec/models/comment_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Ben Halpern 2024-01-17 12:22:50 -05:00 committed by GitHub
parent fb855c97bd
commit 66f3ae71b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 4 deletions

View file

@ -317,9 +317,8 @@ class Comment < ApplicationRecord
end
def synchronous_spam_score_check
return unless Settings::RateLimit.trigger_spam_for?(text: [title, body_markdown].join("\n"))
self.score = -1 # ensure notification is not sent if possibly spammy
self.score = -3 if user.registered_at > 48.hours.ago && body_markdown.include?("http")
self.score = -5 if Settings::RateLimit.trigger_spam_for?(text: [title, body_markdown].join("\n"))
end
def create_conditional_autovomits
@ -332,6 +331,7 @@ class Comment < ApplicationRecord
parent_user != user &&
parent_user.notification_setting.email_comment_notifications &&
parent_user.email &&
user&.badge_achievements_count&.positive? &&
parent_or_root_article.receive_notifications
end

View file

@ -431,7 +431,7 @@ RSpec.describe Comment do
# rubocop:disable RSpec/ExampleLength
it "enqueues a worker to send email" do
comment.save!
child_comment_user = create(:user)
child_comment_user = create(:user, badge_achievements_count: 1)
child_comment = build(:comment, parent: comment, user: child_comment_user, commentable: article)
expect do
@ -440,6 +440,16 @@ RSpec.describe Comment do
end
# rubocop:enable RSpec/ExampleLength
it "does not send email notification if commenter has no badges" do
comment.save!
child_comment_user = create(:user, badge_achievements_count: 0)
child_comment = build(:comment, parent: comment, user: child_comment_user, commentable: article)
expect do
child_comment.save!
end.not_to change(Comments::SendEmailNotificationWorker.jobs, :size)
end
it "enqueues a worker to bust comment cache" do
expect do
comment.save
@ -492,6 +502,28 @@ RSpec.describe Comment do
comment.save
expect(Spam::Handler).to have_received(:handle_comment!).with(comment: comment)
end
it "marks score as negative 3 if new user and comment includes htttp" do
comment = build(:comment, user: user, commentable: article)
comment.body_markdown = "http://example.com this has a link"
comment.save!
expect(comment.score).to eq(-3)
end
it "does not mark as negative 3 if not new user" do
user.update_column(:registered_at, 5.days.ago)
comment = build(:comment, user: user, commentable: article)
comment.body_markdown = "http://example.com this has a link"
comment.save
expect(comment.score).to eq(0)
end
it "marks score as negative 5 if spam trigger is called" do
# Settings::RateLimit.trigger_spam_for?(text: [title, body_markdown].join("\n"))
allow(Settings::RateLimit).to receive(:trigger_spam_for?).and_return(true)
comment = create(:comment, user: user, commentable: article)
expect(comment.score).to eq(-5)
end
end
describe "#privileged_reaction_counts" do