soapbox/src/normalizers/notification.ts

46 wiersze
1.4 KiB
TypeScript
Czysty Zwykły widok Historia

2022-03-12 21:01:00 +00:00
/**
* Notification normalizer:
* Converts API notifications into our internal format.
* @see {@link https://docs.joinmastodon.org/entities/notification/}
*/
2022-03-11 18:13:36 +00:00
import {
List as ImmutableList,
2022-03-11 18:13:36 +00:00
Map as ImmutableMap,
Record as ImmutableRecord,
2022-03-17 02:33:09 +00:00
fromJS,
2022-03-11 18:13:36 +00:00
} from 'immutable';
2022-03-31 22:00:31 +00:00
import type { Account, Status, EmbeddedEntity } from 'soapbox/types/entities';
2022-03-11 18:13:36 +00:00
// https://docs.joinmastodon.org/entities/notification/
2022-03-17 02:15:38 +00:00
export const NotificationRecord = ImmutableRecord({
2022-03-31 22:00:31 +00:00
account: null as EmbeddedEntity<Account>,
accounts: null as ImmutableList<EmbeddedEntity<Account>> | null,
2022-03-31 22:00:31 +00:00
chat_message: null as ImmutableMap<string, any> | string | null, // pleroma:chat_mention
2022-03-11 18:13:36 +00:00
created_at: new Date(),
2022-03-31 22:00:31 +00:00
emoji: null as string | null, // pleroma:emoji_reaction
emoji_url: null as string | null, // pleroma:emoji_reaction
2022-03-11 18:13:36 +00:00
id: '',
2022-03-31 22:00:31 +00:00
status: null as EmbeddedEntity<Status>,
target: null as EmbeddedEntity<Account>, // move
type: '',
2022-05-24 20:49:59 +00:00
total_count: null as number | null, // grouped notifications
2022-03-11 18:13:36 +00:00
});
const normalizeType = (notification: ImmutableMap<string, any>) => {
if (notification.get('type') === 'group_mention') {
return notification.set('type', 'mention');
}
return notification;
};
2022-03-17 02:33:09 +00:00
export const normalizeNotification = (notification: Record<string, any>) => {
return NotificationRecord(
ImmutableMap(fromJS(notification))
.withMutations((notification: ImmutableMap<string, any>) => {
normalizeType(notification);
}),
2022-03-17 02:33:09 +00:00
);
2022-03-11 18:13:36 +00:00
};