diff --git a/app/controllers/notifications/trays_controller.rb b/app/controllers/notifications/trays_controller.rb index c2bac662ad..fbb24000b8 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.preloaded.unread.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 + fresh_when etag: [ Current.user.notifications, include_unread? ] end + + private + def unread_notifications + Current.user.notifications.preloaded.unread.ordered.limit(MAX_ENTRIES_LIMIT) + end + + def read_notifications + Current.user.notifications.preloaded.read.ordered.limit(MAX_ENTRIES_LIMIT) + end + + def include_unread? + ActiveModel::Type::Boolean.new.cast(params[:include_unread]) + end end diff --git a/app/views/notifications/_notification.json.jbuilder b/app/views/notifications/_notification.json.jbuilder index ba27c5425e..5a6e604c7b 100644 --- a/app/views/notifications/_notification.json.jbuilder +++ b/app/views/notifications/_notification.json.jbuilder @@ -9,8 +9,10 @@ json.cache! notification do json.creator notification.creator, partial: "users/user", as: :user json.card do - json.(notification.card, :id, :title, :status) + json.(notification.card, :id, :number, :title, :status) + json.board_name notification.card.board.name json.url card_url(notification.card) + json.column notification.card.column, partial: "columns/column", as: :column if notification.card.column end json.url notification_url(notification) diff --git a/app/views/notifications/trays/show.json.jbuilder b/app/views/notifications/trays/show.json.jbuilder new file mode 100644 index 0000000000..0c2928cef7 --- /dev/null +++ b/app/views/notifications/trays/show.json.jbuilder @@ -0,0 +1 @@ +json.array! @notifications, partial: "notifications/notification", as: :notification diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder index 0884f225ab..ed05bc67f4 100644 --- a/app/views/users/_user.json.jbuilder +++ b/app/views/users/_user.json.jbuilder @@ -5,4 +5,5 @@ json.cache! user do json.created_at user.created_at.utc json.url user_url(user) + json.avatar_url user_avatar_url(user) end diff --git a/test/controllers/notifications/trays_controller_test.rb b/test/controllers/notifications/trays_controller_test.rb index 07034fff0d..57e2997e90 100644 --- a/test/controllers/notifications/trays_controller_test.rb +++ b/test/controllers/notifications/trays_controller_test.rb @@ -11,4 +11,24 @@ class Notifications::TraysControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_select "div", text: /Layout is broken/ end + + test "show as JSON" do + expected_ids = users(:kevin).notifications.unread.ordered.limit(100).pluck(:id) + + get tray_notifications_path(format: :json) + + assert_response :success + assert_equal expected_ids, @response.parsed_body.map { |notification| notification["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 { |notification| notification["id"] } + end end diff --git a/test/controllers/notifications_controller_test.rb b/test/controllers/notifications_controller_test.rb index e4e7bec38a..3b2223ae43 100644 --- a/test/controllers/notifications_controller_test.rb +++ b/test/controllers/notifications_controller_test.rb @@ -23,5 +23,9 @@ class NotificationsControllerTest < ActionDispatch::IntegrationTest assert_not_nil notification["created_at"] assert_not_nil notification["card"] assert_not_nil notification["creator"] + assert_not_nil notification.dig("creator", "avatar_url") + assert_not_nil notification.dig("card", "number") + assert_not_nil notification.dig("card", "board_name") + assert_not_nil notification.dig("card", "column") end end