[Glitch] Change grouped notifications API shape (take 2)

Port front-end changes from 549ab089ee to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
pull/2803/head
Claire 2024-07-31 12:50:13 +02:00
parent de1ef5b470
commit feed816398
4 changed files with 25 additions and 13 deletions

View File

@ -38,10 +38,6 @@ function dispatchAssociatedRecords(
const fetchedStatuses: ApiStatusJSON[] = []; const fetchedStatuses: ApiStatusJSON[] = [];
notifications.forEach((notification) => { notifications.forEach((notification) => {
if ('sample_accounts' in notification) {
fetchedAccounts.push(...notification.sample_accounts);
}
if (notification.type === 'admin.report') { if (notification.type === 'admin.report') {
fetchedAccounts.push(notification.report.target_account); fetchedAccounts.push(notification.report.target_account);
} }
@ -75,7 +71,9 @@ export const fetchNotifications = createDataLoadingThunk(
: excludeAllTypesExcept(activeFilter), : excludeAllTypesExcept(activeFilter),
}); });
}, },
({ notifications }, { dispatch }) => { ({ notifications, accounts, statuses }, { dispatch }) => {
dispatch(importFetchedAccounts(accounts));
dispatch(importFetchedStatuses(statuses));
dispatchAssociatedRecords(dispatch, notifications); dispatchAssociatedRecords(dispatch, notifications);
const payload: (ApiNotificationGroupJSON | NotificationGap)[] = const payload: (ApiNotificationGroupJSON | NotificationGap)[] =
notifications; notifications;
@ -95,7 +93,9 @@ export const fetchNotificationsGap = createDataLoadingThunk(
async (params: { gap: NotificationGap }) => async (params: { gap: NotificationGap }) =>
apiFetchNotifications({ max_id: params.gap.maxId }), apiFetchNotifications({ max_id: params.gap.maxId }),
({ notifications }, { dispatch }) => { ({ notifications, accounts, statuses }, { dispatch }) => {
dispatch(importFetchedAccounts(accounts));
dispatch(importFetchedStatuses(statuses));
dispatchAssociatedRecords(dispatch, notifications); dispatchAssociatedRecords(dispatch, notifications);
return { notifications }; return { notifications };

View File

@ -1,17 +1,24 @@
import api, { apiRequest, getLinks } from 'flavours/glitch/api'; import api, { apiRequest, getLinks } from 'flavours/glitch/api';
import type { ApiNotificationGroupJSON } from 'flavours/glitch/api_types/notifications'; import type { ApiNotificationGroupsResultJSON } from 'flavours/glitch/api_types/notifications';
export const apiFetchNotifications = async (params?: { export const apiFetchNotifications = async (params?: {
exclude_types?: string[]; exclude_types?: string[];
max_id?: string; max_id?: string;
}) => { }) => {
const response = await api().request<ApiNotificationGroupJSON[]>({ const response = await api().request<ApiNotificationGroupsResultJSON>({
method: 'GET', method: 'GET',
url: '/api/v2_alpha/notifications', url: '/api/v2_alpha/notifications',
params, params,
}); });
return { notifications: response.data, links: getLinks(response) }; const { statuses, accounts, notification_groups } = response.data;
return {
statuses,
accounts,
notifications: notification_groups,
links: getLinks(response),
};
}; };
export const apiClearNotifications = () => export const apiClearNotifications = () =>

View File

@ -51,7 +51,7 @@ export interface BaseNotificationGroupJSON {
group_key: string; group_key: string;
notifications_count: number; notifications_count: number;
type: NotificationType; type: NotificationType;
sample_accounts: ApiAccountJSON[]; sample_account_ids: string[];
latest_page_notification_at: string; // FIXME: This will only be present if the notification group is returned in a paginated list, not requested directly latest_page_notification_at: string; // FIXME: This will only be present if the notification group is returned in a paginated list, not requested directly
most_recent_notification_id: string; most_recent_notification_id: string;
page_min_id?: string; page_min_id?: string;
@ -143,3 +143,9 @@ export type ApiNotificationGroupJSON =
| AccountRelationshipSeveranceNotificationGroupJSON | AccountRelationshipSeveranceNotificationGroupJSON
| NotificationGroupWithStatusJSON | NotificationGroupWithStatusJSON
| ModerationWarningNotificationGroupJSON; | ModerationWarningNotificationGroupJSON;
export interface ApiNotificationGroupsResultJSON {
accounts: ApiAccountJSON[];
statuses: ApiStatusJSON[];
notification_groups: ApiNotificationGroupJSON[];
}

View File

@ -14,7 +14,7 @@ import type { ApiReportJSON } from 'flavours/glitch/api_types/reports';
export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8; export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8;
interface BaseNotificationGroup interface BaseNotificationGroup
extends Omit<BaseNotificationGroupJSON, 'sample_accounts'> { extends Omit<BaseNotificationGroupJSON, 'sample_account_ids'> {
sampleAccountIds: string[]; sampleAccountIds: string[];
} }
@ -115,8 +115,7 @@ function createAccountRelationshipSeveranceEventFromJSON(
export function createNotificationGroupFromJSON( export function createNotificationGroupFromJSON(
groupJson: ApiNotificationGroupJSON, groupJson: ApiNotificationGroupJSON,
): NotificationGroup { ): NotificationGroup {
const { sample_accounts, ...group } = groupJson; const { sample_account_ids: sampleAccountIds, ...group } = groupJson;
const sampleAccountIds = sample_accounts.map((account) => account.id);
switch (group.type) { switch (group.type) {
case 'favourite': case 'favourite':