From 1fcffa573cf1ca9373ac1b65b2f7e805cc691927 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 4 Sep 2024 14:54:15 +0200 Subject: [PATCH] Fix 500 error in `GET /api/v2_alpha/notifications` when there are no notifications to return (#31746) --- .../api/v2_alpha/notifications_controller.rb | 2 ++ spec/requests/api/v2_alpha/notifications_spec.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/api/v2_alpha/notifications_controller.rb b/app/controllers/api/v2_alpha/notifications_controller.rb index bd6979955a..e8aa0b9e49 100644 --- a/app/controllers/api/v2_alpha/notifications_controller.rb +++ b/app/controllers/api/v2_alpha/notifications_controller.rb @@ -77,6 +77,8 @@ class Api::V2Alpha::NotificationsController < Api::BaseController end def load_grouped_notifications + return [] if @notifications.empty? + MastodonOTELTracer.in_span('Api::V2Alpha::NotificationsController#load_grouped_notifications') do NotificationGroup.from_notifications(@notifications, pagination_range: (@notifications.last.id)..(@notifications.first.id), grouped_types: params[:grouped_types]) end diff --git a/spec/requests/api/v2_alpha/notifications_spec.rb b/spec/requests/api/v2_alpha/notifications_spec.rb index 7663d215eb..8009e7edce 100644 --- a/spec/requests/api/v2_alpha/notifications_spec.rb +++ b/spec/requests/api/v2_alpha/notifications_spec.rb @@ -116,6 +116,19 @@ RSpec.describe 'Notifications' do it_behaves_like 'forbidden for wrong scope', 'write write:notifications' + context 'when there are no notifications' do + before do + user.account.notifications.destroy_all + end + + it 'returns 0 notifications' do + subject + + expect(response).to have_http_status(200) + expect(body_as_json[:notification_groups]).to eq [] + end + end + context 'with no options' do it 'returns expected notification types', :aggregate_failures do subject