forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port f587ff6 to glitch-soc Co-authored-by: Eugen Rochko <eugen@zeonfederated.com> Co-authored-by: Claire <claire.github-309c@sitedethib.com> Signed-off-by: Claire <claire.github-309c@sitedethib.com>
- Loading branch information
1 parent
c75fe09
commit 3a8a672
Showing
56 changed files
with
3,267 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
app/javascript/flavours/glitch/actions/notification_groups.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { createAction } from '@reduxjs/toolkit'; | ||
|
||
import { | ||
apiClearNotifications, | ||
apiFetchNotifications, | ||
} from 'flavours/glitch/api/notifications'; | ||
import type { ApiAccountJSON } from 'flavours/glitch/api_types/accounts'; | ||
import type { | ||
ApiNotificationGroupJSON, | ||
ApiNotificationJSON, | ||
} from 'flavours/glitch/api_types/notifications'; | ||
import { allNotificationTypes } from 'flavours/glitch/api_types/notifications'; | ||
import type { ApiStatusJSON } from 'flavours/glitch/api_types/statuses'; | ||
import type { NotificationGap } from 'flavours/glitch/reducers/notification_groups'; | ||
import { | ||
selectSettingsNotificationsExcludedTypes, | ||
selectSettingsNotificationsQuickFilterActive, | ||
} from 'flavours/glitch/selectors/settings'; | ||
import type { AppDispatch } from 'flavours/glitch/store'; | ||
import { | ||
createAppAsyncThunk, | ||
createDataLoadingThunk, | ||
} from 'flavours/glitch/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); | ||
} | ||
|
||
function dispatchAssociatedRecords( | ||
dispatch: AppDispatch, | ||
notifications: ApiNotificationGroupJSON[] | ApiNotificationJSON[], | ||
) { | ||
const fetchedAccounts: ApiAccountJSON[] = []; | ||
const fetchedStatuses: ApiStatusJSON[] = []; | ||
|
||
notifications.forEach((notification) => { | ||
if ('sample_accounts' in notification) { | ||
fetchedAccounts.push(...notification.sample_accounts); | ||
} | ||
|
||
if (notification.type === 'admin.report') { | ||
fetchedAccounts.push(notification.report.target_account); | ||
} | ||
|
||
if (notification.type === 'moderation_warning') { | ||
fetchedAccounts.push(notification.moderation_warning.target_account); | ||
} | ||
|
||
if ('status' in notification) { | ||
fetchedStatuses.push(notification.status); | ||
} | ||
}); | ||
|
||
if (fetchedAccounts.length > 0) | ||
dispatch(importFetchedAccounts(fetchedAccounts)); | ||
|
||
if (fetchedStatuses.length > 0) | ||
dispatch(importFetchedStatuses(fetchedStatuses)); | ||
} | ||
|
||
export const fetchNotifications = createDataLoadingThunk( | ||
'notificationGroups/fetch', | ||
async (_params, { getState }) => { | ||
const activeFilter = | ||
selectSettingsNotificationsQuickFilterActive(getState()); | ||
|
||
return apiFetchNotifications({ | ||
exclude_types: | ||
activeFilter === 'all' | ||
? selectSettingsNotificationsExcludedTypes(getState()) | ||
: excludeAllTypesExcept(activeFilter), | ||
}); | ||
}, | ||
({ notifications }, { dispatch }) => { | ||
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', | ||
async (params: { gap: NotificationGap }) => | ||
apiFetchNotifications({ max_id: params.gap.maxId }), | ||
|
||
({ notifications }, { dispatch }) => { | ||
dispatchAssociatedRecords(dispatch, notifications); | ||
|
||
return { notifications }; | ||
}, | ||
); | ||
|
||
export const processNewNotificationForGroups = createAppAsyncThunk( | ||
'notificationGroups/processNew', | ||
(notification: ApiNotificationJSON, { dispatch }) => { | ||
dispatchAssociatedRecords(dispatch, [notification]); | ||
|
||
return notification; | ||
}, | ||
); | ||
|
||
export const loadPending = createAction('notificationGroups/loadPending'); | ||
|
||
export const updateScrollPosition = createAction<{ top: boolean }>( | ||
'notificationGroups/updateScrollPosition', | ||
); | ||
|
||
export const setNotificationsFilter = createAppAsyncThunk( | ||
'notifications/filter/set', | ||
({ filterType }: { filterType: string }, { dispatch }) => { | ||
dispatch({ | ||
type: NOTIFICATIONS_FILTER_SET, | ||
path: ['notifications', 'quickFilter', 'active'], | ||
value: filterType, | ||
}); | ||
// dispatch(expandNotifications({ forceLoad: true })); | ||
void dispatch(fetchNotifications()); | ||
dispatch(saveSettings()); | ||
}, | ||
); | ||
|
||
export const clearNotifications = createDataLoadingThunk( | ||
'notifications/clear', | ||
() => apiClearNotifications(), | ||
); | ||
|
||
export const markNotificationsAsRead = createAction( | ||
'notificationGroups/markAsRead', | ||
); | ||
|
||
export const mountNotifications = createAction('notificationGroups/mount'); | ||
export const unmountNotifications = createAction('notificationGroups/unmount'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/javascript/flavours/glitch/actions/notifications_migration.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createAppAsyncThunk } from 'flavours/glitch/store'; | ||
|
||
import { fetchNotifications } from './notification_groups'; | ||
import { expandNotifications } from './notifications'; | ||
|
||
export const initializeNotifications = createAppAsyncThunk( | ||
'notifications/initialize', | ||
(_, { dispatch, getState }) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
const enableBeta = getState().settings.getIn( | ||
['notifications', 'groupingBeta'], | ||
false, | ||
) as boolean; | ||
|
||
if (enableBeta) void dispatch(fetchNotifications()); | ||
else dispatch(expandNotifications()); | ||
}, | ||
); |
9 changes: 2 additions & 7 deletions
9
app/javascript/flavours/glitch/actions/notifications_typed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import api, { apiRequest, getLinks } from 'flavours/glitch/api'; | ||
import type { ApiNotificationGroupJSON } from 'flavours/glitch/api_types/notifications'; | ||
|
||
export const apiFetchNotifications = async (params?: { | ||
exclude_types?: string[]; | ||
max_id?: string; | ||
}) => { | ||
const response = await api().request<ApiNotificationGroupJSON[]>({ | ||
method: 'GET', | ||
url: '/api/v2_alpha/notifications', | ||
params, | ||
}); | ||
|
||
return { notifications: response.data, links: getLinks(response) }; | ||
}; | ||
|
||
export const apiClearNotifications = () => | ||
apiRequest<undefined>('POST', 'v1/notifications/clear'); |
Oops, something went wrong.