Skip to content

Commit

Permalink
Decrease count of filtered notifications when notification requests a…
Browse files Browse the repository at this point in the history
…re accepted or dismissed (mastodon#31149)
  • Loading branch information
oneiros authored Jul 26, 2024
1 parent c091fa7 commit dfd4386
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
6 changes: 6 additions & 0 deletions app/javascript/mastodon/actions/notification_policies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createAction } from '@reduxjs/toolkit';

import {
apiGetNotificationPolicy,
apiUpdateNotificationsPolicy,
Expand All @@ -14,3 +16,7 @@ export const updateNotificationsPolicy = createDataLoadingThunk(
'notificationPolicy/update',
(policy: Partial<NotificationPolicy>) => apiUpdateNotificationsPolicy(policy),
);

export const decreasePendingNotificationsCount = createAction<number>(
'notificationPolicy/decreasePendingNotificationCount',
);
15 changes: 13 additions & 2 deletions app/javascript/mastodon/actions/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
importFetchedStatuses,
} from './importer';
import { submitMarkers } from './markers';
import { decreasePendingNotificationsCount } from './notification_policies';
import { notificationsUpdate } from "./notifications_typed";
import { register as registerPushNotifications } from './push_notifications';
import { saveSettings } from './settings';
Expand Down Expand Up @@ -84,6 +85,12 @@ const fetchRelatedRelationships = (dispatch, notifications) => {
}
};

const selectNotificationCountForRequest = (state, id) => {
const requests = state.getIn(['notificationRequests', 'items']);
const thisRequest = requests.find(request => request.get('id') === id);
return thisRequest ? thisRequest.get('notifications_count') : 0;
};

export const loadPending = () => ({
type: NOTIFICATIONS_LOAD_PENDING,
});
Expand Down Expand Up @@ -433,11 +440,13 @@ export const fetchNotificationRequestFail = (id, error) => ({
error,
});

export const acceptNotificationRequest = id => (dispatch) => {
export const acceptNotificationRequest = (id) => (dispatch, getState) => {
const count = selectNotificationCountForRequest(getState(), id);
dispatch(acceptNotificationRequestRequest(id));

api().post(`/api/v1/notifications/requests/${id}/accept`).then(() => {
dispatch(acceptNotificationRequestSuccess(id));
dispatch(decreasePendingNotificationsCount(count));
}).catch(err => {
dispatch(acceptNotificationRequestFail(id, err));
});
Expand All @@ -459,11 +468,13 @@ export const acceptNotificationRequestFail = (id, error) => ({
error,
});

export const dismissNotificationRequest = id => (dispatch) => {
export const dismissNotificationRequest = (id) => (dispatch, getState) => {
const count = selectNotificationCountForRequest(getState(), id);
dispatch(dismissNotificationRequestRequest(id));

api().post(`/api/v1/notifications/requests/${id}/dismiss`).then(() =>{
dispatch(dismissNotificationRequestSuccess(id));
dispatch(decreasePendingNotificationsCount(count));
}).catch(err => {
dispatch(dismissNotificationRequestFail(id, err));
});
Expand Down
22 changes: 15 additions & 7 deletions app/javascript/mastodon/reducers/notification_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import { createReducer, isAnyOf } from '@reduxjs/toolkit';

import {
fetchNotificationPolicy,
decreasePendingNotificationsCount,
updateNotificationsPolicy,
} from 'mastodon/actions/notification_policies';
import type { NotificationPolicy } from 'mastodon/models/notification_policy';

export const notificationPolicyReducer =
createReducer<NotificationPolicy | null>(null, (builder) => {
builder.addMatcher(
isAnyOf(
fetchNotificationPolicy.fulfilled,
updateNotificationsPolicy.fulfilled,
),
(_state, action) => action.payload,
);
builder
.addCase(decreasePendingNotificationsCount, (state, action) => {
if (state) {
state.summary.pending_notifications_count -= action.payload;
state.summary.pending_requests_count -= 1;
}
})
.addMatcher(
isAnyOf(
fetchNotificationPolicy.fulfilled,
updateNotificationsPolicy.fulfilled,
),
(_state, action) => action.payload,
);
});

0 comments on commit dfd4386

Please sign in to comment.