From 0a6b75b71eae0dd9eb9d5d8c73e1d210820eeb51 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 24 Sep 2024 17:03:38 +0200 Subject: [PATCH] Fix multiple bugs in notification requests and notification policies (#32062) --- .../mastodon/actions/notification_policies.ts | 4 +- .../mastodon/actions/notification_requests.ts | 40 +++++-------------- app/javascript/mastodon/api/notifications.ts | 2 +- .../filtered_notifications_banner.tsx | 4 +- .../mastodon/reducers/notification_policy.ts | 7 ++-- 5 files changed, 18 insertions(+), 39 deletions(-) diff --git a/app/javascript/mastodon/actions/notification_policies.ts b/app/javascript/mastodon/actions/notification_policies.ts index b182bcf699..fd798eaad7 100644 --- a/app/javascript/mastodon/actions/notification_policies.ts +++ b/app/javascript/mastodon/actions/notification_policies.ts @@ -17,6 +17,6 @@ export const updateNotificationsPolicy = createDataLoadingThunk( (policy: Partial) => apiUpdateNotificationsPolicy(policy), ); -export const decreasePendingNotificationsCount = createAction( - 'notificationPolicy/decreasePendingNotificationCount', +export const decreasePendingRequestsCount = createAction( + 'notificationPolicy/decreasePendingRequestsCount', ); diff --git a/app/javascript/mastodon/actions/notification_requests.ts b/app/javascript/mastodon/actions/notification_requests.ts index ef9cbef03e..8352ff2aad 100644 --- a/app/javascript/mastodon/actions/notification_requests.ts +++ b/app/javascript/mastodon/actions/notification_requests.ts @@ -13,11 +13,11 @@ import type { ApiNotificationJSON, } from 'mastodon/api_types/notifications'; import type { ApiStatusJSON } from 'mastodon/api_types/statuses'; -import type { AppDispatch, RootState } from 'mastodon/store'; +import type { AppDispatch } from 'mastodon/store'; import { createDataLoadingThunk } from 'mastodon/store/typed_functions'; import { importFetchedAccounts, importFetchedStatuses } from './importer'; -import { decreasePendingNotificationsCount } from './notification_policies'; +import { decreasePendingRequestsCount } from './notification_policies'; // TODO: refactor with notification_groups function dispatchAssociatedRecords( @@ -169,19 +169,11 @@ export const expandNotificationsForRequest = createDataLoadingThunk( }, ); -const selectNotificationCountForRequest = (state: RootState, id: string) => { - const requests = state.notificationRequests.items; - const thisRequest = requests.find((request) => request.id === id); - return thisRequest ? thisRequest.notifications_count : 0; -}; - export const acceptNotificationRequest = createDataLoadingThunk( 'notificationRequest/accept', ({ id }: { id: string }) => apiAcceptNotificationRequest(id), - (_data, { dispatch, getState, discardLoadData, actionArg: { id } }) => { - const count = selectNotificationCountForRequest(getState(), id); - - dispatch(decreasePendingNotificationsCount(count)); + (_data, { dispatch, discardLoadData }) => { + dispatch(decreasePendingRequestsCount(1)); // The payload is not used in any functions return discardLoadData; @@ -191,10 +183,8 @@ export const acceptNotificationRequest = createDataLoadingThunk( export const dismissNotificationRequest = createDataLoadingThunk( 'notificationRequest/dismiss', ({ id }: { id: string }) => apiDismissNotificationRequest(id), - (_data, { dispatch, getState, discardLoadData, actionArg: { id } }) => { - const count = selectNotificationCountForRequest(getState(), id); - - dispatch(decreasePendingNotificationsCount(count)); + (_data, { dispatch, discardLoadData }) => { + dispatch(decreasePendingRequestsCount(1)); // The payload is not used in any functions return discardLoadData; @@ -204,13 +194,8 @@ export const dismissNotificationRequest = createDataLoadingThunk( export const acceptNotificationRequests = createDataLoadingThunk( 'notificationRequests/acceptBulk', ({ ids }: { ids: string[] }) => apiAcceptNotificationRequests(ids), - (_data, { dispatch, getState, discardLoadData, actionArg: { ids } }) => { - const count = ids.reduce( - (count, id) => count + selectNotificationCountForRequest(getState(), id), - 0, - ); - - dispatch(decreasePendingNotificationsCount(count)); + (_data, { dispatch, discardLoadData, actionArg: { ids } }) => { + dispatch(decreasePendingRequestsCount(ids.length)); // The payload is not used in any functions return discardLoadData; @@ -220,13 +205,8 @@ export const acceptNotificationRequests = createDataLoadingThunk( export const dismissNotificationRequests = createDataLoadingThunk( 'notificationRequests/dismissBulk', ({ ids }: { ids: string[] }) => apiDismissNotificationRequests(ids), - (_data, { dispatch, getState, discardLoadData, actionArg: { ids } }) => { - const count = ids.reduce( - (count, id) => count + selectNotificationCountForRequest(getState(), id), - 0, - ); - - dispatch(decreasePendingNotificationsCount(count)); + (_data, { dispatch, discardLoadData, actionArg: { ids } }) => { + dispatch(decreasePendingRequestsCount(ids.length)); // The payload is not used in any functions return discardLoadData; diff --git a/app/javascript/mastodon/api/notifications.ts b/app/javascript/mastodon/api/notifications.ts index 24d526655c..92863ac5ca 100644 --- a/app/javascript/mastodon/api/notifications.ts +++ b/app/javascript/mastodon/api/notifications.ts @@ -91,5 +91,5 @@ export const apiAcceptNotificationRequests = async (id: string[]) => { }; export const apiDismissNotificationRequests = async (id: string[]) => { - return apiRequestPost('v1/notifications/dismiss/dismiss', { id }); + return apiRequestPost('v1/notifications/requests/dismiss', { id }); }; diff --git a/app/javascript/mastodon/features/notifications/components/filtered_notifications_banner.tsx b/app/javascript/mastodon/features/notifications/components/filtered_notifications_banner.tsx index d9e60d48cd..d2241af389 100644 --- a/app/javascript/mastodon/features/notifications/components/filtered_notifications_banner.tsx +++ b/app/javascript/mastodon/features/notifications/components/filtered_notifications_banner.tsx @@ -31,7 +31,7 @@ export const FilteredNotificationsIconButton: React.FC<{ history.push('/notifications/requests'); }, [history]); - if (policy === null || policy.summary.pending_notifications_count === 0) { + if (policy === null || policy.summary.pending_requests_count <= 0) { return null; } @@ -70,7 +70,7 @@ export const FilteredNotificationsBanner: React.FC = () => { }; }, [dispatch]); - if (policy === null || policy.summary.pending_notifications_count === 0) { + if (policy === null || policy.summary.pending_requests_count <= 0) { return null; } diff --git a/app/javascript/mastodon/reducers/notification_policy.ts b/app/javascript/mastodon/reducers/notification_policy.ts index ed912dde5d..a883f9c1ee 100644 --- a/app/javascript/mastodon/reducers/notification_policy.ts +++ b/app/javascript/mastodon/reducers/notification_policy.ts @@ -2,7 +2,7 @@ import { createReducer, isAnyOf } from '@reduxjs/toolkit'; import { fetchNotificationPolicy, - decreasePendingNotificationsCount, + decreasePendingRequestsCount, updateNotificationsPolicy, } from 'mastodon/actions/notification_policies'; import type { NotificationPolicy } from 'mastodon/models/notification_policy'; @@ -10,10 +10,9 @@ import type { NotificationPolicy } from 'mastodon/models/notification_policy'; export const notificationPolicyReducer = createReducer(null, (builder) => { builder - .addCase(decreasePendingNotificationsCount, (state, action) => { + .addCase(decreasePendingRequestsCount, (state, action) => { if (state) { - state.summary.pending_notifications_count -= action.payload; - state.summary.pending_requests_count -= 1; + state.summary.pending_requests_count -= action.payload; } }) .addMatcher(