Skip to content

Commit

Permalink
Send notifications to note subscribers instead of commenters
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Nov 8, 2024
1 parent 3a1b4cb commit 2347906
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/controllers/api/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ def add_comment(note, text, event, notify: true)

comment = note.comments.create!(attributes)

note.comments.map(&:author).uniq.each do |user|
UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?
if notify
note.subscribers.visible.each do |user|
UserMailer.note_comment_notification(comment, user).deliver_later if current_user != user
end
end

NoteSubscription.find_or_create_by(:note => note, :user => current_user) if current_user
Expand Down
54 changes: 54 additions & 0 deletions test/controllers/api/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,58 @@ def test_comment_success
assert_equal user.display_name, js["properties"]["comments"].last["user"]
end

def test_comment_without_notifications_success
# Ensure that emails are sent to users
first_user = create(:user)
second_user = create(:user)
third_user = create(:user)

note_with_comments_by_users = create(:note) do |note|
create(:note_comment, :note => note, :author => first_user)
create(:note_comment, :note => note, :author => second_user)
end

auth_header = bearer_authorization_header third_user

assert_difference "NoteComment.count", 1 do
assert_difference "NoteSubscription.count", 1 do
assert_no_difference "ActionMailer::Base.deliveries.size" do
perform_enqueued_jobs do
post comment_api_note_path(note_with_comments_by_users, :text => "This is an additional comment", :format => "json"), :headers => auth_header
end
end
end
end
assert_response :success
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal "Feature", js["type"]
assert_equal note_with_comments_by_users.id, js["properties"]["id"]
assert_equal "open", js["properties"]["status"]
assert_equal 3, js["properties"]["comments"].count
assert_equal "commented", js["properties"]["comments"].last["action"]
assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
assert_equal third_user.display_name, js["properties"]["comments"].last["user"]

subscription = NoteSubscription.last
assert_equal third_user, subscription.user
assert_equal note_with_comments_by_users, subscription.note

get api_note_path(note_with_comments_by_users, :format => "json")
assert_response :success
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal "Feature", js["type"]
assert_equal note_with_comments_by_users.id, js["properties"]["id"]
assert_equal "open", js["properties"]["status"]
assert_equal 3, js["properties"]["comments"].count
assert_equal "commented", js["properties"]["comments"].last["action"]
assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
assert_equal third_user.display_name, js["properties"]["comments"].last["user"]

ActionMailer::Base.deliveries.clear
end

def test_comment_with_notifications_success
# Ensure that emails are sent to users
first_user = create(:user)
Expand All @@ -281,6 +333,8 @@ def test_comment_with_notifications_success
create(:note_comment, :note => note, :author => first_user)
create(:note_comment, :note => note, :author => second_user)
end
create(:note_subscription, :note => note_with_comments_by_users, :user => first_user)
create(:note_subscription, :note => note_with_comments_by_users, :user => second_user)

auth_header = bearer_authorization_header third_user

Expand Down
3 changes: 3 additions & 0 deletions test/factories/note_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryBot.define do
factory :note_subscription
end

0 comments on commit 2347906

Please sign in to comment.