Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send comment email to suspended or inactive users. #1363

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/models/polymorphic/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
# Notify subscribed users when a comment is added, unless user created this comment
def notify_subscribers
commentable.subscribed_users.reject { |user_id| user_id == user.id }.each do |subscriber_id|
if subscriber = User.find_by_id(subscriber_id)
subscriber = User.find_by_id(subscriber_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this is an activerecord relation; not an array of ids (hard to tell), the below

commentable.subscribed_users.reject { |user_id| user_id == user.id }

should be refactored to a scope or .where.not(user+id: user.id); so that you don't do a loop of user objects you already have.

If its an array of user ids; it should still be refactored to something like

other_users = User.where(id: commentable.subscribed_users.reject { |user_id| user_id == user.id }.collect(&:subscriber_id))

Then you can further add scopes for currently_emailable applying your conditions below.

Avoid N+1 issues

if subscriber && subscriber.confirmed? && !subscriber.awaits_approval? && !subscriber.suspended? && subscriber.email.present?
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
CloCkWeRX marked this conversation as resolved.
Show resolved Hide resolved
SubscriptionMailer.comment_notification(subscriber, self).deliver_later
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/user_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
deleted_at { nil }
updated_at { FactoryBot.generate(:time) }
created_at { FactoryBot.generate(:time) }
confirmed_at { Time.now.utc }
suspended_at { nil }
password { "password" }
password_confirmation { "password" }
Expand Down
32 changes: 32 additions & 0 deletions spec/models/polymorphic/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@
expect(entity.subscribed_users).to include(user.id)
end
end

describe "notify_subscribers" do
let(:subscriber) { create(:user) }
let(:entity) { create(:lead, subscribed_users: [subscriber.id]) }
before(:each) do
allow(SubscriptionMailer).to receive_message_chain(:comment_notification, :deliver_later)
end

it "should notify subscribers when a comment is added" do
Comment.create!(comment: "Hello", user: create(:user), commentable: entity)
expect(SubscriptionMailer).to have_received(:comment_notification).with(subscriber, instance_of(Comment))
end

it "should not notify the user who created the comment" do
user = create(:user, confirmed_at: Time.now, email: "[email protected]")
Comment.create!(comment: "Hello", user: user, commentable: entity)

Check notice

Code scanning / Rubocop

Prefer Ruby 1.9 hash syntax { a: 1, b: 2 } over 1.8 syntax { :a => 1, :b => 2 }. Note test

Style/HashSyntax: Omit the hash value.
expect(SubscriptionMailer).not_to have_received(:comment_notification).with(user, instance_of(Comment))
end

it "should not notify suspended users" do
subscriber.update(suspended_at: Time.now)
Comment.create!(comment: "Hello", user: create(:user), commentable: entity)
expect(SubscriptionMailer).not_to have_received(:comment_notification).with(subscriber, instance_of(Comment))
end

it "should not notify users awaiting approval" do
subscriber.update(sign_in_count: 0, suspended_at: Time.now)
allow(Setting).to receive(:user_signup).and_return(:not_allowed)
Comment.create!(comment: "Hello", user: create(:user), commentable: entity)
expect(SubscriptionMailer).not_to have_received(:comment_notification).with(subscriber, instance_of(Comment))
end
end
end
Loading