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

Conversation

steveyken
Copy link
Member

Users who are suspended, inactive or not yet confirmed should not receive comment notifications.

app/models/polymorphic/comment.rb Fixed Show fixed Hide fixed
app/models/polymorphic/comment.rb Fixed Show fixed Hide fixed

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.
@steveyken
Copy link
Member Author

I need to fix failing feature spec before merging

@@ -53,7 +53,8 @@ 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)
subscriber = User.find_by_id(subscriber_id)
if subscriber&.confirmed? && !subscriber.awaits_approval? && !subscriber.suspended? && subscriber.email.present?
Copy link
Member

Choose a reason for hiding this comment

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

Can you refactor this to a method on User, something like emailable?

@@ -53,7 +53,8 @@ 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)
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

Copy link
Member

@CloCkWeRX CloCkWeRX left a comment

Choose a reason for hiding this comment

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

N+1 issues if it is likely to be more than 1-2 subscribed users for a comment; .emailable? or a similar method to consolidate the logic in one spot.

@steveyken
Copy link
Member Author

steveyken commented Sep 21, 2024 via email

@steveyken steveyken requested a review from CloCkWeRX September 21, 2024 06:38
@steveyken
Copy link
Member Author

@CloCkWeRX ok to merge? let me know if any further thoughts

@CloCkWeRX CloCkWeRX merged commit fc25091 into master Sep 26, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants