From a626a4a30c41227ba64011bb8a8ba81811240324 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 1 Aug 2024 16:08:54 +0200 Subject: [PATCH] Switch to batch queries for accepting/dismissing all notification requests --- .../mastodon/actions/notifications.js | 64 +++++++++++++++++++ .../features/notifications/requests.jsx | 16 ++--- .../reducers/notification_requests.js | 5 ++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/app/javascript/mastodon/actions/notifications.js b/app/javascript/mastodon/actions/notifications.js index 48afb003ad..7d6a9d5a32 100644 --- a/app/javascript/mastodon/actions/notifications.js +++ b/app/javascript/mastodon/actions/notifications.js @@ -64,6 +64,14 @@ export const NOTIFICATION_REQUEST_DISMISS_REQUEST = 'NOTIFICATION_REQUEST_DISMIS export const NOTIFICATION_REQUEST_DISMISS_SUCCESS = 'NOTIFICATION_REQUEST_DISMISS_SUCCESS'; export const NOTIFICATION_REQUEST_DISMISS_FAIL = 'NOTIFICATION_REQUEST_DISMISS_FAIL'; +export const NOTIFICATION_REQUESTS_ACCEPT_REQUEST = 'NOTIFICATION_REQUESTS_ACCEPT_REQUEST'; +export const NOTIFICATION_REQUESTS_ACCEPT_SUCCESS = 'NOTIFICATION_REQUESTS_ACCEPT_SUCCESS'; +export const NOTIFICATION_REQUESTS_ACCEPT_FAIL = 'NOTIFICATION_REQUESTS_ACCEPT_FAIL'; + +export const NOTIFICATION_REQUESTS_DISMISS_REQUEST = 'NOTIFICATION_REQUESTS_DISMISS_REQUEST'; +export const NOTIFICATION_REQUESTS_DISMISS_SUCCESS = 'NOTIFICATION_REQUESTS_DISMISS_SUCCESS'; +export const NOTIFICATION_REQUESTS_DISMISS_FAIL = 'NOTIFICATION_REQUESTS_DISMISS_FAIL'; + export const NOTIFICATIONS_FOR_REQUEST_FETCH_REQUEST = 'NOTIFICATIONS_FOR_REQUEST_FETCH_REQUEST'; export const NOTIFICATIONS_FOR_REQUEST_FETCH_SUCCESS = 'NOTIFICATIONS_FOR_REQUEST_FETCH_SUCCESS'; export const NOTIFICATIONS_FOR_REQUEST_FETCH_FAIL = 'NOTIFICATIONS_FOR_REQUEST_FETCH_FAIL'; @@ -496,6 +504,62 @@ export const dismissNotificationRequestFail = (id, error) => ({ error, }); +export const acceptNotificationRequests = (ids) => (dispatch, getState) => { + const count = ids.reduce((count, id) => count + selectNotificationCountForRequest(getState(), id), 0); + dispatch(acceptNotificationRequestsRequest(ids)); + + api().post(`/api/v1/notifications/requests/accept`, { id: ids }).then(() => { + dispatch(acceptNotificationRequestsSuccess(ids)); + dispatch(decreasePendingNotificationsCount(count)); + }).catch(err => { + dispatch(acceptNotificationRequestFail(ids, err)); + }); +}; + +export const acceptNotificationRequestsRequest = ids => ({ + type: NOTIFICATION_REQUESTS_ACCEPT_REQUEST, + ids, +}); + +export const acceptNotificationRequestsSuccess = ids => ({ + type: NOTIFICATION_REQUESTS_ACCEPT_SUCCESS, + ids, +}); + +export const acceptNotificationRequestsFail = (ids, error) => ({ + type: NOTIFICATION_REQUESTS_ACCEPT_FAIL, + ids, + error, +}); + +export const dismissNotificationRequests = (ids) => (dispatch, getState) => { + const count = ids.reduce((count, id) => count + selectNotificationCountForRequest(getState(), id), 0); + dispatch(acceptNotificationRequestsRequest(ids)); + + api().post(`/api/v1/notifications/requests/dismiss`, { id: ids }).then(() => { + dispatch(dismissNotificationRequestsSuccess(ids)); + dispatch(decreasePendingNotificationsCount(count)); + }).catch(err => { + dispatch(dismissNotificationRequestFail(ids, err)); + }); +}; + +export const dismissNotificationRequestsRequest = ids => ({ + type: NOTIFICATION_REQUESTS_DISMISS_REQUEST, + ids, +}); + +export const dismissNotificationRequestsSuccess = ids => ({ + type: NOTIFICATION_REQUESTS_DISMISS_SUCCESS, + ids, +}); + +export const dismissNotificationRequestsFail = (ids, error) => ({ + type: NOTIFICATION_REQUESTS_DISMISS_FAIL, + ids, + error, +}); + export const fetchNotificationsForRequest = accountId => (dispatch, getState) => { const current = getState().getIn(['notificationRequests', 'current']); const params = { account_id: accountId }; diff --git a/app/javascript/mastodon/features/notifications/requests.jsx b/app/javascript/mastodon/features/notifications/requests.jsx index d4db9072f1..a0bccfda82 100644 --- a/app/javascript/mastodon/features/notifications/requests.jsx +++ b/app/javascript/mastodon/features/notifications/requests.jsx @@ -10,7 +10,7 @@ import { useSelector, useDispatch } from 'react-redux'; import InventoryIcon from '@/material-icons/400-24px/inventory_2.svg?react'; import MoreHorizIcon from '@/material-icons/400-24px/more_horiz.svg?react'; import { openModal } from 'mastodon/actions/modal'; -import { fetchNotificationRequests, expandNotificationRequests, acceptNotificationRequest, dismissNotificationRequest } from 'mastodon/actions/notifications'; +import { fetchNotificationRequests, expandNotificationRequests, acceptNotificationRequests, dismissNotificationRequests } from 'mastodon/actions/notifications'; import Column from 'mastodon/components/column'; import ColumnHeader from 'mastodon/components/column_header'; import ScrollableList from 'mastodon/components/scrollable_list'; @@ -57,11 +57,8 @@ export const NotificationRequests = ({ multiColumn }) => { title: intl.formatMessage(messages.confirm_accept_all_title), message: intl.formatMessage(messages.confirm_accept_all_message, { count: notificationRequests.size }), confirm: intl.formatMessage(messages.confirm_accept_all_button), - onConfirm: () => { - notificationRequests.forEach((request) => - dispatch(acceptNotificationRequest(request.get('id'))) - ); - }, + onConfirm: () => + dispatch(acceptNotificationRequests(notificationRequests.map((request) => request.get('id')))), }, })); }, [dispatch, notificationRequests, intl]); @@ -73,11 +70,8 @@ export const NotificationRequests = ({ multiColumn }) => { title: intl.formatMessage(messages.confirm_dismiss_all_title), message: intl.formatMessage(messages.confirm_dismiss_all_message, { count: notificationRequests.size }), confirm: intl.formatMessage(messages.confirm_dismiss_all_button), - onConfirm: () => { - notificationRequests.forEach((request) => - dispatch(dismissNotificationRequest(request.get('id'))) - ); - }, + onConfirm: () => + dispatch(dismissNotificationRequests(notificationRequests.map((request) => request.get('id')))), } })); }, [dispatch, notificationRequests, intl]); diff --git a/app/javascript/mastodon/reducers/notification_requests.js b/app/javascript/mastodon/reducers/notification_requests.js index 4247062a58..03746d014a 100644 --- a/app/javascript/mastodon/reducers/notification_requests.js +++ b/app/javascript/mastodon/reducers/notification_requests.js @@ -12,6 +12,8 @@ import { NOTIFICATION_REQUEST_FETCH_FAIL, NOTIFICATION_REQUEST_ACCEPT_REQUEST, NOTIFICATION_REQUEST_DISMISS_REQUEST, + NOTIFICATION_REQUESTS_ACCEPT_REQUEST, + NOTIFICATION_REQUESTS_DISMISS_REQUEST, NOTIFICATIONS_FOR_REQUEST_FETCH_REQUEST, NOTIFICATIONS_FOR_REQUEST_FETCH_SUCCESS, NOTIFICATIONS_FOR_REQUEST_FETCH_FAIL, @@ -74,6 +76,9 @@ export const notificationRequestsReducer = (state = initialState, action) => { case NOTIFICATION_REQUEST_ACCEPT_REQUEST: case NOTIFICATION_REQUEST_DISMISS_REQUEST: return removeRequest(state, action.id); + case NOTIFICATION_REQUESTS_ACCEPT_REQUEST: + case NOTIFICATION_REQUESTS_DISMISS_REQUEST: + return action.ids.reduce((state, id) => removeRequest(state, id), state); case NOTIFICATION_REQUEST_FETCH_REQUEST: return state.set('current', initialState.get('current').set('isLoading', true)); case NOTIFICATION_REQUEST_FETCH_SUCCESS: