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

Mark Likes on comment notifications as read when visiting a post #8442

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/services/post_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def mark_user_notifications(post_id)
return unless user
mark_comment_reshare_like_notifications_read(post_id)
mark_mention_notifications_read(post_id)
mark_like_on_comment_notifications_read(post_id)
end

def destroy(post_id, private_allowed=true)
Expand Down Expand Up @@ -101,4 +102,10 @@ def mentions_in_comments_for_post(post_id)
.joins("INNER JOIN comments ON mentions_container_id = comments.id AND mentions_container_type = 'Comment'")
.where(comments: {commentable_id: post_id, commentable_type: "Post"})
end

def mark_like_on_comment_notifications_read(post_id)
Notification.where(recipient_id: user.id, target_type: "Comment",
target_id: Comment.where(commentable_id: post_id, author_id: user.person.id),
unread: true).update_all(unread: false) # rubocop:disable Rails/SkipsModelValidations
end
end
44 changes: 44 additions & 0 deletions spec/services/post_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@
expect_any_instance_of(PostService).not_to receive(:mark_mention_notifications_read).with(post.id)
PostService.new.mark_user_notifications(post.id)
end

context "for comments" do
let(:comment) { post.comments.create(author: alice.person, text: "comment") }

it "marks a corresponding notifications as read" do
FactoryBot.create(:notification, recipient: alice, target: comment, unread: true)
FactoryBot.create(:notification, recipient: alice, target: comment, unread: true)

expect {
PostService.new(alice).mark_user_notifications(post.id)
}.to change(Notification.where(unread: true), :count).by(-2)
end

it "does not change the update_at date/time for comment notifications" do
notification = Timecop.travel(1.minute.ago) do
FactoryBot.create(:notification, recipient: alice, target: comment, unread: true)
end

expect {
PostService.new(alice).mark_user_notifications(post.id)
}.not_to(change { Notification.where(id: notification.id).pluck(:updated_at) })
end

it "does not change other users notifications" do
alice_notification = FactoryBot.create(:notification, recipient: alice, target: comment, unread: true)
bob_notification = FactoryBot.create(:notification, recipient: bob, target: comment, unread: true)

PostService.new(alice).mark_user_notifications(post.id)

expect(Notification.find(alice_notification.id).unread).to be_falsey
expect(Notification.find(bob_notification.id).unread).to be_truthy
end

it "marks notifications for all comments on a post as read" do
comment2 = post.comments.create(author: alice.person, text: "comment2")

FactoryBot.create(:notification, recipient: alice, target: comment, unread: true)
FactoryBot.create(:notification, recipient: alice, target: comment2, unread: true)

expect {
PostService.new(alice).mark_user_notifications(post.id)
}.to change(Notification.where(unread: true), :count).by(-2)
end
end
end

describe "#destroy" do
Expand Down