diff --git a/app/models/card/readable.rb b/app/models/card/readable.rb index 84db015ed..26b25b720 100644 --- a/app/models/card/readable.rb +++ b/app/models/card/readable.rb @@ -7,17 +7,15 @@ module Card::Readable end end + def notifications_for(user) + scope = user.notifications.unread + scope.where(source: event_notification_sources) + .or(scope.where(source: mention_notification_sources)) + end + private - def notifications_for(user) - user.notifications.unread.where(source: notification_sources) - end - - def notification_sources - event_notification_sources + mention_notification_sources - end - def event_notification_sources - events + comment_creation_events + events.or(comment_creation_events) end def comment_creation_events @@ -25,7 +23,7 @@ module Card::Readable end def mention_notification_sources - mentions + comment_mentions + mentions.or(comment_mentions) end def comment_mentions diff --git a/test/models/card/readable_test.rb b/test/models/card/readable_test.rb index c53d3f44d..ffbfdcc44 100644 --- a/test/models/card/readable_test.rb +++ b/test/models/card/readable_test.rb @@ -26,4 +26,38 @@ class Card::ReadableTest < ActiveSupport::TestCase cards(:layout).read_by(users(:kevin)) end end + + test "notifications for a given user" do + # Returns card event notifications + kevin_notifications = cards(:logo).notifications_for(users(:kevin)) + assert_includes kevin_notifications, notifications(:logo_published_kevin) + assert_includes kevin_notifications, notifications(:logo_assignment_kevin) + + # Returns comment creation event notifications + layout_notifications = cards(:layout).notifications_for(users(:kevin)) + assert_includes layout_notifications, notifications(:layout_commented_kevin) + + # Returns card mention notifications + david_notifications = cards(:logo).notifications_for(users(:david)) + assert_includes david_notifications, notifications(:logo_card_david_mention_by_jz) + + # Returns comment mention notifications + assert_includes david_notifications, notifications(:logo_comment_david_mention_by_jz) + + # Only returns unread notifications + notifications(:logo_published_kevin).read + kevin_notifications_after_read = cards(:logo).notifications_for(users(:kevin)) + assert_not_includes kevin_notifications_after_read, notifications(:logo_published_kevin) + assert_includes kevin_notifications_after_read, notifications(:logo_assignment_kevin) + + # Does not include notifications from other cards + other_event = events(:text_published) + other_notification = users(:kevin).notifications.create!(source: other_event, creator: users(:david)) + logo_notifications = cards(:logo).notifications_for(users(:kevin)) + assert_not_includes logo_notifications, other_notification + + # Does not include notifications for other users + assert_not_includes kevin_notifications, notifications(:logo_card_david_mention_by_jz) + assert_not_includes kevin_notifications, notifications(:logo_comment_david_mention_by_jz) + end end