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 all commits
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
7 changes: 3 additions & 4 deletions app/models/polymorphic/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ def subscribe_user_to_entity(u = user)

# 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)
SubscriptionMailer.comment_notification(subscriber, self).deliver_later
end
users_to_notify = User.where(id: commentable.subscribed_users.reject { |user_id| user_id == user.id })
users_to_notify.select(&:emailable?).each do |subscriber|
SubscriptionMailer.comment_notification(subscriber, self).deliver_later
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/users/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def inactive_message
end
end

# Send emails to active users only
#----------------------------------------------------------------------------
def emailable?
confirmed? && !awaits_approval? && !suspended? && email.present?
end

#----------------------------------------------------------------------------
def preference
@preference ||= preferences.build
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
3 changes: 2 additions & 1 deletion spec/features/devise/sign_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
password: 'password',
password_confirmation: 'password',
email: '[email protected]',
sign_in_count: 0
sign_in_count: 0,
confirmed_at: nil
end

scenario 'without confirmation' do
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
20 changes: 20 additions & 0 deletions spec/models/users/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,24 @@
expect(search.first).to eql(user)
end
end

describe "emailable?" do
let(:user) { create(:user) }
it "should return true for standard user" do
expect(user.emailable?).to eql(true)
end
it "should return false for unconfirmed user" do
user.update(confirmed_at: nil)
expect(user.emailable?).to eql(false)
end
it "should return false for user awaiting approval" do
user.update(sign_in_count: 0, suspended_at: Time.now)
allow(Setting).to receive(:user_signup).and_return(:not_allowed)
expect(user.emailable?).to eql(false)
end
it "should return false for suspended user" do
user.update(suspended_at: Time.now)
expect(user.emailable?).to eql(false)
end
end
end
Loading