Fix `GET /api/v2/notifications/:id` and `POST /api/v2/notifications/:id/dismiss` for ungrouped notifications (#33990)

pull/33951/head
Claire 3 days ago committed by GitHub
parent 33643c4c07
commit 20531d1e07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -46,7 +46,7 @@ class Api::V2::NotificationsController < Api::BaseController
end end
def show def show
@notification = current_account.notifications.without_suspended.find_by!(group_key: params[:group_key]) @notification = current_account.notifications.without_suspended.by_group_key(params[:group_key]).take!
presenter = GroupedNotificationsPresenter.new(NotificationGroup.from_notifications([@notification])) presenter = GroupedNotificationsPresenter.new(NotificationGroup.from_notifications([@notification]))
render json: presenter, serializer: REST::DedupNotificationGroupSerializer render json: presenter, serializer: REST::DedupNotificationGroupSerializer
end end
@ -57,7 +57,7 @@ class Api::V2::NotificationsController < Api::BaseController
end end
def dismiss def dismiss
current_account.notifications.where(group_key: params[:group_key]).destroy_all current_account.notifications.by_group_key(params[:group_key]).destroy_all
render_empty render_empty
end end

@ -7,6 +7,10 @@ module Notification::Groups
GROUPABLE_NOTIFICATION_TYPES = %i(favourite reblog follow).freeze GROUPABLE_NOTIFICATION_TYPES = %i(favourite reblog follow).freeze
MAXIMUM_GROUP_SPAN_HOURS = 12 MAXIMUM_GROUP_SPAN_HOURS = 12
included do
scope :by_group_key, ->(group_key) { group_key&.start_with?('ungrouped-') ? where(id: group_key.delete_prefix('ungrouped-')) : where(group_key: group_key) }
end
def set_group_key! def set_group_key!
return if filtered? || GROUPABLE_NOTIFICATION_TYPES.exclude?(type) return if filtered? || GROUPABLE_NOTIFICATION_TYPES.exclude?(type)

@ -365,6 +365,18 @@ RSpec.describe 'Notifications' do
.to start_with('application/json') .to start_with('application/json')
end end
context 'with an ungrouped notification' do
let(:notification) { Fabricate(:notification, account: user.account, type: :favourite) }
it 'returns http success' do
get "/api/v2/notifications/ungrouped-#{notification.id}", headers: headers
expect(response).to have_http_status(200)
expect(response.content_type)
.to start_with('application/json')
end
end
context 'when notification belongs to someone else' do context 'when notification belongs to someone else' do
let(:notification) { Fabricate(:notification, group_key: 'foobar') } let(:notification) { Fabricate(:notification, group_key: 'foobar') }
@ -396,6 +408,19 @@ RSpec.describe 'Notifications' do
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
context 'with an ungrouped notification' do
let(:notification) { Fabricate(:notification, account: user.account, type: :favourite) }
it 'destroys the notification' do
post "/api/v2/notifications/ungrouped-#{notification.id}/dismiss", headers: headers
expect(response).to have_http_status(200)
expect(response.content_type)
.to start_with('application/json')
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when notification belongs to someone else' do context 'when notification belongs to someone else' do
let(:notification) { Fabricate(:notification, group_key: 'foobar') } let(:notification) { Fabricate(:notification, group_key: 'foobar') }

Loading…
Cancel
Save