diff --git a/app/controllers/notifications/trays_controller.rb b/app/controllers/notifications/trays_controller.rb index 0adc0e17b..980d1ba1f 100644 --- a/app/controllers/notifications/trays_controller.rb +++ b/app/controllers/notifications/trays_controller.rb @@ -1,9 +1,27 @@ class Notifications::TraysController < ApplicationController + MAX_ENTRIES_LIMIT = 100 + def show - @notifications = Current.user.notifications.unread.preloaded.ordered.limit(100) + @notifications = unread_notifications + if include_unread? + @notifications += read_notifications + end # Invalidate on the whole set instead of the unread set since the max updated at in the unread set # can stay the same when reading old notifications. fresh_when Current.user.notifications end + + private + def unread_notifications + Current.user.notifications.unread.preloaded.ordered.limit(MAX_ENTRIES_LIMIT) + end + + def read_notifications + Current.user.notifications.read.preloaded.ordered.limit(MAX_ENTRIES_LIMIT) + end + + def include_unread? + ActiveModel::Type::Boolean.new.cast(params[:include_unread]) + end end diff --git a/test/controllers/notifications/trays_controller_test.rb b/test/controllers/notifications/trays_controller_test.rb index d0916e305..89dc42c6f 100644 --- a/test/controllers/notifications/trays_controller_test.rb +++ b/test/controllers/notifications/trays_controller_test.rb @@ -18,6 +18,17 @@ class Notifications::TraysControllerTest < ActionDispatch::IntegrationTest get tray_notifications_path(format: :json) assert_response :success - assert_equal expected_ids, @response.parsed_body.map { |notification| notification["id"] } + assert_equal expected_ids, @response.parsed_body.map { |s| s["id"] } + end + + test "show as JSON with include_unread includes read notifications" do + notifications = users(:kevin).notifications + expected_ids = notifications.unread.ordered.limit(100).pluck(:id) + + notifications.read.ordered.limit(100).pluck(:id) + + get tray_notifications_path(format: :json, include_unread: true) + + assert_response :success + assert_equal expected_ids, @response.parsed_body.map { |s| s["id"] } end end