improve notifications minification
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
5c047c4ef6
commit
7c2611ffdb
4 changed files with 23 additions and 24 deletions
|
@ -16,7 +16,9 @@ const STATUS_NOTIFICATION_TYPES = [
|
|||
const normalizeNotification = (notification: BaseNotification) => ({
|
||||
...notification,
|
||||
account: normalizeAccount(notification.account),
|
||||
account_id: notification.account.id,
|
||||
accounts: [normalizeAccount(notification.account)],
|
||||
account_ids: [notification.account.id],
|
||||
});
|
||||
|
||||
const normalizeNotifications = (notifications: Array<BaseNotification>) => {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Record as ImmutableRecord, OrderedMap as ImmutableOrderedMap } from 'immutable';
|
||||
import omit from 'lodash/omit';
|
||||
|
||||
import {
|
||||
ACCOUNT_BLOCK_SUCCESS,
|
||||
|
@ -63,15 +64,15 @@ const comparator = (a: Pick<Notification, 'id'>, b: Pick<Notification, 'id'>) =>
|
|||
const minifyNotification = (notification: Notification) => {
|
||||
// @ts-ignore
|
||||
const minifiedNotification: {
|
||||
account: string;
|
||||
accounts: string[];
|
||||
account_id: string;
|
||||
account_ids: string[];
|
||||
created_at: string;
|
||||
id: string;
|
||||
} & (
|
||||
| { type: 'follow' | 'follow_request' | 'admin.sign_up' | 'bite' }
|
||||
| {
|
||||
type: 'mention' | 'status' | 'reblog' | 'favourite' | 'poll' | 'update' | 'event_reminder';
|
||||
status: string;
|
||||
status_id: string;
|
||||
}
|
||||
| {
|
||||
type: 'admin.report';
|
||||
|
@ -87,40 +88,36 @@ const minifyNotification = (notification: Notification) => {
|
|||
}
|
||||
| {
|
||||
type: 'move';
|
||||
target: string;
|
||||
target_id: string;
|
||||
}
|
||||
| {
|
||||
type: 'emoji_reaction';
|
||||
emoji: string;
|
||||
emoji_url: string | null;
|
||||
status: string;
|
||||
status_id: string;
|
||||
}
|
||||
| {
|
||||
type: 'chat_mention';
|
||||
chat_message: string;
|
||||
chat_message_id: string;
|
||||
}
|
||||
| {
|
||||
type: 'participation_accepted' | 'participation_request';
|
||||
status: string;
|
||||
status_id: string;
|
||||
participation_message: string | null;
|
||||
}
|
||||
) = {
|
||||
...notification,
|
||||
account: notification.account.id,
|
||||
accounts: notification.accounts.map(({ id }) => id),
|
||||
...omit(notification, ['account', 'accounts']),
|
||||
created_at: notification.created_at,
|
||||
id: notification.id,
|
||||
type: notification.type,
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
minifiedNotification.status = notification.status?.id;
|
||||
if (notification.status) minifiedNotification.status_id = notification.status.id;
|
||||
// @ts-ignore
|
||||
minifiedNotification.target = notification.target?.id;
|
||||
if (notification.target) minifiedNotification.target_id = notification.target.id;
|
||||
// @ts-ignore
|
||||
minifiedNotification.status = notification.status?.id;
|
||||
// @ts-ignore
|
||||
minifiedNotification.chat_message = notification.chat_message?.id;
|
||||
if (notification.chat_message) minifiedNotification.chat_message_id = notification.chat_message.id;
|
||||
|
||||
return minifiedNotification;
|
||||
};
|
||||
|
@ -163,10 +160,10 @@ const expandNormalizedNotifications = (state: State, notifications: Notification
|
|||
};
|
||||
|
||||
const filterNotifications = (state: State, relationship: Relationship) =>
|
||||
state.update('items', map => map.filterNot(item => item !== null && item.accounts.includes(relationship.id)));
|
||||
state.update('items', map => map.filterNot(item => item !== null && item.account_ids.includes(relationship.id)));
|
||||
|
||||
const filterNotificationIds = (state: State, accountIds: Array<string>, type?: string) => {
|
||||
const helper = (list: ImmutableOrderedMap<string, MinifiedNotification>) => list.filterNot(item => item !== null && accountIds.includes(item.accounts[0]) && (type === undefined || type === item.type));
|
||||
const helper = (list: ImmutableOrderedMap<string, MinifiedNotification>) => list.filterNot(item => item !== null && accountIds.includes(item.account_ids[0]) && (type === undefined || type === item.type));
|
||||
return state.update('items', helper);
|
||||
};
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ import {
|
|||
NOTIFICATIONS_UPDATE,
|
||||
} from 'soapbox/actions/notifications';
|
||||
|
||||
import type { Account, PaginatedResponse } from 'pl-api';
|
||||
import type { Account, Notification, PaginatedResponse } from 'pl-api';
|
||||
import type { APIEntity } from 'soapbox/types/entities';
|
||||
|
||||
const ListRecord = ImmutableRecord({
|
||||
|
@ -134,7 +134,7 @@ const removeFromList = (state: State, path: NestedListPath | ListPath, accountId
|
|||
(map as List).update('items', list => (list as Items).filterNot(item => item === accountId)),
|
||||
);
|
||||
|
||||
const normalizeFollowRequest = (state: State, notification: APIEntity) =>
|
||||
const normalizeFollowRequest = (state: State, notification: Notification) =>
|
||||
state.updateIn(['follow_requests', 'items'], list =>
|
||||
ImmutableOrderedSet([notification.account.id]).union(list as Items),
|
||||
);
|
||||
|
|
|
@ -22,7 +22,7 @@ import type { MinifiedNotification } from 'soapbox/reducers/notifications';
|
|||
import type { MinifiedStatus } from 'soapbox/reducers/statuses';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
const normalizeId = (id: any): string => typeof id === 'string' ? id : '';
|
||||
const normalizeId = (id: any): string => typeof id === 'string' ? id : typeof id === 'object' ? normalizeId(id.id) : '';
|
||||
|
||||
const selectAccount = (state: RootState, accountId: string) =>
|
||||
state.entities[Entities.ACCOUNTS]?.store[accountId] as Account | undefined;
|
||||
|
@ -182,12 +182,12 @@ type SelectedStatus = Exclude<ReturnType<ReturnType<typeof makeGetStatus>>, null
|
|||
const makeGetNotification = () => createSelector([
|
||||
(_state: RootState, notification: MinifiedNotification) => notification,
|
||||
// @ts-ignore
|
||||
(state: RootState, notification: MinifiedNotification) => selectAccount(state, normalizeId(notification.account)),
|
||||
(state: RootState, notification: MinifiedNotification) => selectAccount(state, normalizeId(notification.account_id)),
|
||||
// @ts-ignore
|
||||
(state: RootState, notification: MinifiedNotification) => selectAccount(state, normalizeId(notification.target)),
|
||||
(state: RootState, notification: MinifiedNotification) => selectAccount(state, normalizeId(notification.target_id)),
|
||||
// @ts-ignore
|
||||
(state: RootState, notification: MinifiedNotification) => state.statuses.get(normalizeId(notification.status)),
|
||||
(state: RootState, notification: MinifiedNotification) => notification.accounts ? selectAccounts(state, notification.accounts?.map(normalizeId)) : null,
|
||||
(state: RootState, notification: MinifiedNotification) => state.statuses.get(normalizeId(notification.status_id)),
|
||||
(state: RootState, notification: MinifiedNotification) => notification.account_ids ? selectAccounts(state, notification.account_ids?.map(normalizeId)) : null,
|
||||
], (notification, account, target, status, accounts): Notification => ({
|
||||
...notification,
|
||||
// @ts-ignore
|
||||
|
|
Loading…
Reference in a new issue