2024-07-19 00:36:09 +10:00
|
|
|
import { createAction } from '@reduxjs/toolkit';
|
|
|
|
|
|
|
|
import {
|
|
|
|
apiClearNotifications,
|
2024-09-16 19:54:03 +10:00
|
|
|
apiFetchNotificationGroups,
|
2024-07-19 00:36:09 +10:00
|
|
|
} from 'mastodon/api/notifications';
|
|
|
|
import type { ApiAccountJSON } from 'mastodon/api_types/accounts';
|
|
|
|
import type {
|
|
|
|
ApiNotificationGroupJSON,
|
|
|
|
ApiNotificationJSON,
|
|
|
|
} from 'mastodon/api_types/notifications';
|
|
|
|
import { allNotificationTypes } from 'mastodon/api_types/notifications';
|
|
|
|
import type { ApiStatusJSON } from 'mastodon/api_types/statuses';
|
2024-08-22 00:41:31 +10:00
|
|
|
import { usePendingItems } from 'mastodon/initial_state';
|
2024-07-19 00:36:09 +10:00
|
|
|
import type { NotificationGap } from 'mastodon/reducers/notification_groups';
|
|
|
|
import {
|
|
|
|
selectSettingsNotificationsExcludedTypes,
|
|
|
|
selectSettingsNotificationsQuickFilterActive,
|
2024-08-12 23:40:29 +10:00
|
|
|
selectSettingsNotificationsShows,
|
2024-07-19 00:36:09 +10:00
|
|
|
} from 'mastodon/selectors/settings';
|
2024-09-04 23:28:16 +10:00
|
|
|
import type { AppDispatch, RootState } from 'mastodon/store';
|
2024-07-19 00:36:09 +10:00
|
|
|
import {
|
|
|
|
createAppAsyncThunk,
|
|
|
|
createDataLoadingThunk,
|
|
|
|
} from 'mastodon/store/typed_functions';
|
|
|
|
|
|
|
|
import { importFetchedAccounts, importFetchedStatuses } from './importer';
|
|
|
|
import { NOTIFICATIONS_FILTER_SET } from './notifications';
|
|
|
|
import { saveSettings } from './settings';
|
|
|
|
|
|
|
|
function excludeAllTypesExcept(filter: string) {
|
|
|
|
return allNotificationTypes.filter((item) => item !== filter);
|
|
|
|
}
|
|
|
|
|
2024-09-04 23:28:16 +10:00
|
|
|
function getExcludedTypes(state: RootState) {
|
|
|
|
const activeFilter = selectSettingsNotificationsQuickFilterActive(state);
|
|
|
|
|
|
|
|
return activeFilter === 'all'
|
|
|
|
? selectSettingsNotificationsExcludedTypes(state)
|
|
|
|
: excludeAllTypesExcept(activeFilter);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:36:09 +10:00
|
|
|
function dispatchAssociatedRecords(
|
|
|
|
dispatch: AppDispatch,
|
|
|
|
notifications: ApiNotificationGroupJSON[] | ApiNotificationJSON[],
|
|
|
|
) {
|
|
|
|
const fetchedAccounts: ApiAccountJSON[] = [];
|
|
|
|
const fetchedStatuses: ApiStatusJSON[] = [];
|
|
|
|
|
|
|
|
notifications.forEach((notification) => {
|
|
|
|
if (notification.type === 'admin.report') {
|
|
|
|
fetchedAccounts.push(notification.report.target_account);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notification.type === 'moderation_warning') {
|
|
|
|
fetchedAccounts.push(notification.moderation_warning.target_account);
|
|
|
|
}
|
|
|
|
|
2024-08-14 16:47:15 +10:00
|
|
|
if ('status' in notification && notification.status) {
|
2024-07-19 00:36:09 +10:00
|
|
|
fetchedStatuses.push(notification.status);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fetchedAccounts.length > 0)
|
|
|
|
dispatch(importFetchedAccounts(fetchedAccounts));
|
|
|
|
|
|
|
|
if (fetchedStatuses.length > 0)
|
|
|
|
dispatch(importFetchedStatuses(fetchedStatuses));
|
|
|
|
}
|
|
|
|
|
2024-09-25 23:36:19 +10:00
|
|
|
const supportedGroupedNotificationTypes = ['favourite', 'reblog'];
|
|
|
|
|
2024-10-01 18:22:14 +10:00
|
|
|
export function shouldGroupNotificationType(type: string) {
|
|
|
|
return supportedGroupedNotificationTypes.includes(type);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:36:09 +10:00
|
|
|
export const fetchNotifications = createDataLoadingThunk(
|
|
|
|
'notificationGroups/fetch',
|
2024-09-04 23:28:16 +10:00
|
|
|
async (_params, { getState }) =>
|
2024-09-25 23:36:19 +10:00
|
|
|
apiFetchNotificationGroups({
|
|
|
|
grouped_types: supportedGroupedNotificationTypes,
|
|
|
|
exclude_types: getExcludedTypes(getState()),
|
|
|
|
}),
|
2024-07-31 20:50:13 +10:00
|
|
|
({ notifications, accounts, statuses }, { dispatch }) => {
|
|
|
|
dispatch(importFetchedAccounts(accounts));
|
|
|
|
dispatch(importFetchedStatuses(statuses));
|
2024-07-19 00:36:09 +10:00
|
|
|
dispatchAssociatedRecords(dispatch, notifications);
|
|
|
|
const payload: (ApiNotificationGroupJSON | NotificationGap)[] =
|
|
|
|
notifications;
|
|
|
|
|
|
|
|
// TODO: might be worth not using gaps for that…
|
|
|
|
// if (nextLink) payload.push({ type: 'gap', loadUrl: nextLink.uri });
|
|
|
|
if (notifications.length > 1)
|
|
|
|
payload.push({ type: 'gap', maxId: notifications.at(-1)?.page_min_id });
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
// dispatch(submitMarkers());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const fetchNotificationsGap = createDataLoadingThunk(
|
|
|
|
'notificationGroups/fetchGap',
|
2024-09-04 23:28:16 +10:00
|
|
|
async (params: { gap: NotificationGap }, { getState }) =>
|
2024-09-16 19:54:03 +10:00
|
|
|
apiFetchNotificationGroups({
|
2024-09-25 23:36:19 +10:00
|
|
|
grouped_types: supportedGroupedNotificationTypes,
|
2024-09-04 23:28:16 +10:00
|
|
|
max_id: params.gap.maxId,
|
|
|
|
exclude_types: getExcludedTypes(getState()),
|
|
|
|
}),
|
2024-07-31 20:50:13 +10:00
|
|
|
({ notifications, accounts, statuses }, { dispatch }) => {
|
|
|
|
dispatch(importFetchedAccounts(accounts));
|
2024-08-22 00:41:31 +10:00
|
|
|
dispatch(importFetchedStatuses(statuses));
|
|
|
|
dispatchAssociatedRecords(dispatch, notifications);
|
|
|
|
|
|
|
|
return { notifications };
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const pollRecentNotifications = createDataLoadingThunk(
|
|
|
|
'notificationGroups/pollRecentNotifications',
|
|
|
|
async (_params, { getState }) => {
|
2024-09-16 19:54:03 +10:00
|
|
|
return apiFetchNotificationGroups({
|
2024-09-25 23:36:19 +10:00
|
|
|
grouped_types: supportedGroupedNotificationTypes,
|
2024-08-22 00:41:31 +10:00
|
|
|
max_id: undefined,
|
2024-09-04 23:28:16 +10:00
|
|
|
exclude_types: getExcludedTypes(getState()),
|
2024-08-22 00:41:31 +10:00
|
|
|
// In slow mode, we don't want to include notifications that duplicate the already-displayed ones
|
|
|
|
since_id: usePendingItems
|
|
|
|
? getState().notificationGroups.groups.find(
|
|
|
|
(group) => group.type !== 'gap',
|
|
|
|
)?.page_max_id
|
|
|
|
: undefined,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
({ notifications, accounts, statuses }, { dispatch }) => {
|
|
|
|
dispatch(importFetchedAccounts(accounts));
|
2024-07-31 20:50:13 +10:00
|
|
|
dispatch(importFetchedStatuses(statuses));
|
2024-07-19 00:36:09 +10:00
|
|
|
dispatchAssociatedRecords(dispatch, notifications);
|
|
|
|
|
|
|
|
return { notifications };
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const processNewNotificationForGroups = createAppAsyncThunk(
|
|
|
|
'notificationGroups/processNew',
|
2024-08-12 23:40:29 +10:00
|
|
|
(notification: ApiNotificationJSON, { dispatch, getState }) => {
|
|
|
|
const state = getState();
|
|
|
|
const activeFilter = selectSettingsNotificationsQuickFilterActive(state);
|
|
|
|
const notificationShows = selectSettingsNotificationsShows(state);
|
|
|
|
|
|
|
|
const showInColumn =
|
|
|
|
activeFilter === 'all'
|
|
|
|
? notificationShows[notification.type]
|
|
|
|
: activeFilter === notification.type;
|
|
|
|
|
|
|
|
if (!showInColumn) return;
|
|
|
|
|
|
|
|
if (
|
|
|
|
(notification.type === 'mention' || notification.type === 'update') &&
|
2024-08-14 16:47:15 +10:00
|
|
|
notification.status?.filtered
|
2024-08-12 23:40:29 +10:00
|
|
|
) {
|
|
|
|
const filters = notification.status.filtered.filter((result) =>
|
|
|
|
result.filter.context.includes('notifications'),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (filters.some((result) => result.filter.filter_action === 'hide')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:36:09 +10:00
|
|
|
dispatchAssociatedRecords(dispatch, [notification]);
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const loadPending = createAction('notificationGroups/loadPending');
|
|
|
|
|
2024-08-20 01:59:06 +10:00
|
|
|
export const updateScrollPosition = createAppAsyncThunk(
|
2024-07-19 00:36:09 +10:00
|
|
|
'notificationGroups/updateScrollPosition',
|
2024-08-20 01:59:06 +10:00
|
|
|
({ top }: { top: boolean }, { dispatch, getState }) => {
|
|
|
|
if (
|
|
|
|
top &&
|
|
|
|
getState().notificationGroups.mergedNotifications === 'needs-reload'
|
|
|
|
) {
|
|
|
|
void dispatch(fetchNotifications());
|
|
|
|
}
|
|
|
|
|
|
|
|
return { top };
|
|
|
|
},
|
2024-07-19 00:36:09 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
export const setNotificationsFilter = createAppAsyncThunk(
|
|
|
|
'notifications/filter/set',
|
|
|
|
({ filterType }: { filterType: string }, { dispatch }) => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_FILTER_SET,
|
|
|
|
path: ['notifications', 'quickFilter', 'active'],
|
|
|
|
value: filterType,
|
|
|
|
});
|
|
|
|
void dispatch(fetchNotifications());
|
|
|
|
dispatch(saveSettings());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const clearNotifications = createDataLoadingThunk(
|
|
|
|
'notifications/clear',
|
|
|
|
() => apiClearNotifications(),
|
|
|
|
);
|
|
|
|
|
|
|
|
export const markNotificationsAsRead = createAction(
|
|
|
|
'notificationGroups/markAsRead',
|
|
|
|
);
|
|
|
|
|
2024-08-20 01:59:06 +10:00
|
|
|
export const mountNotifications = createAppAsyncThunk(
|
|
|
|
'notificationGroups/mount',
|
|
|
|
(_, { dispatch, getState }) => {
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
if (
|
|
|
|
state.notificationGroups.mounted === 0 &&
|
|
|
|
state.notificationGroups.mergedNotifications === 'needs-reload'
|
|
|
|
) {
|
|
|
|
void dispatch(fetchNotifications());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-07-19 00:36:09 +10:00
|
|
|
export const unmountNotifications = createAction('notificationGroups/unmount');
|
2024-08-20 01:59:06 +10:00
|
|
|
|
|
|
|
export const refreshStaleNotificationGroups = createAppAsyncThunk<{
|
|
|
|
deferredRefresh: boolean;
|
|
|
|
}>('notificationGroups/refreshStale', (_, { dispatch, getState }) => {
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
if (
|
|
|
|
state.notificationGroups.scrolledToTop ||
|
|
|
|
!state.notificationGroups.mounted
|
|
|
|
) {
|
|
|
|
void dispatch(fetchNotifications());
|
|
|
|
return { deferredRefresh: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { deferredRefresh: true };
|
|
|
|
});
|