2022-04-12 09:52:56 -07:00
|
|
|
import {
|
|
|
|
List as ImmutableList,
|
|
|
|
Map as ImmutableMap,
|
|
|
|
Record as ImmutableRecord,
|
|
|
|
fromJS,
|
|
|
|
} from 'immutable';
|
|
|
|
|
2022-10-10 13:56:11 -07:00
|
|
|
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
|
|
|
|
|
2023-02-08 09:58:01 -08:00
|
|
|
import { normalizeEmojiReaction } from './emoji-reaction';
|
|
|
|
|
|
|
|
import type { Attachment, Card, Emoji, EmojiReaction } from 'soapbox/types/entities';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
|
|
|
export const ChatMessageRecord = ImmutableRecord({
|
|
|
|
account_id: '',
|
2023-02-02 12:43:47 -08:00
|
|
|
media_attachments: ImmutableList<Attachment>(),
|
2022-04-12 09:52:56 -07:00
|
|
|
card: null as Card | null,
|
|
|
|
chat_id: '',
|
|
|
|
content: '',
|
2022-11-02 12:23:39 -07:00
|
|
|
created_at: '',
|
2022-04-12 09:52:56 -07:00
|
|
|
emojis: ImmutableList<Emoji>(),
|
2023-02-08 09:58:01 -08:00
|
|
|
expiration: null as number | null,
|
2023-02-14 10:36:15 -08:00
|
|
|
emoji_reactions: null as ImmutableList<EmojiReaction> | null,
|
2022-04-12 09:52:56 -07:00
|
|
|
id: '',
|
|
|
|
unread: false,
|
|
|
|
deleting: false,
|
2022-11-02 12:23:39 -07:00
|
|
|
pending: false as boolean | undefined,
|
2022-04-12 09:52:56 -07:00
|
|
|
});
|
|
|
|
|
2022-10-10 13:56:11 -07:00
|
|
|
const normalizeMedia = (status: ImmutableMap<string, any>) => {
|
2023-02-02 12:43:47 -08:00
|
|
|
const attachments = status.get('media_attachments');
|
2022-10-21 21:10:38 -07:00
|
|
|
const attachment = status.get('attachment');
|
|
|
|
|
2023-02-02 12:43:47 -08:00
|
|
|
if (attachments) {
|
|
|
|
return status.set('media_attachments', ImmutableList(attachments.map(normalizeAttachment)));
|
|
|
|
} else if (attachment) {
|
|
|
|
return status.set('media_attachments', ImmutableList([normalizeAttachment(attachment)]));
|
2022-10-21 21:10:38 -07:00
|
|
|
} else {
|
2023-02-02 12:43:47 -08:00
|
|
|
return status.set('media_attachments', ImmutableList());
|
2022-10-21 21:10:38 -07:00
|
|
|
}
|
2022-10-10 13:56:11 -07:00
|
|
|
};
|
|
|
|
|
2023-02-08 09:58:01 -08:00
|
|
|
const normalizeChatMessageEmojiReaction = (chatMessage: ImmutableMap<string, any>) => {
|
|
|
|
const emojiReactions = chatMessage.get('emoji_reactions');
|
|
|
|
|
|
|
|
if (emojiReactions) {
|
|
|
|
return chatMessage.set('emoji_reactions', ImmutableList(emojiReactions.map(normalizeEmojiReaction)));
|
|
|
|
} else {
|
|
|
|
return chatMessage;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-21 07:00:16 -08:00
|
|
|
/** Rewrite `<p></p>` to empty string. */
|
|
|
|
const fixContent = (chatMessage: ImmutableMap<string, any>) => {
|
|
|
|
if (chatMessage.get('content') === '<p></p>') {
|
|
|
|
return chatMessage.set('content', '');
|
|
|
|
} else {
|
|
|
|
return chatMessage;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-12 09:52:56 -07:00
|
|
|
export const normalizeChatMessage = (chatMessage: Record<string, any>) => {
|
|
|
|
return ChatMessageRecord(
|
2022-10-10 13:56:11 -07:00
|
|
|
ImmutableMap(fromJS(chatMessage)).withMutations(chatMessage => {
|
|
|
|
normalizeMedia(chatMessage);
|
2023-02-08 09:58:01 -08:00
|
|
|
normalizeChatMessageEmojiReaction(chatMessage);
|
2023-02-21 07:00:16 -08:00
|
|
|
fixContent(chatMessage);
|
2022-10-10 13:56:11 -07:00
|
|
|
}),
|
2022-04-12 09:52:56 -07:00
|
|
|
);
|
|
|
|
};
|