2020-03-27 13:59:38 -07:00
|
|
|
import escapeTextContentForBrowser from 'escape-html';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
import emojify from '../../features/emoji/emoji';
|
|
|
|
import { unescapeHTML } from '../../utils/html';
|
|
|
|
|
|
|
|
const makeEmojiMap = record => record.emojis.reduce((obj, emoji) => {
|
|
|
|
obj[`:${emoji.shortcode}:`] = emoji;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
export function normalizeAccount(account) {
|
|
|
|
account = { ...account };
|
|
|
|
|
2022-02-07 09:50:05 -08:00
|
|
|
// Some backends can return null, or omit these required fields
|
|
|
|
if (!account.emojis) account.emojis = [];
|
|
|
|
if (!account.display_name) account.display_name = '';
|
|
|
|
if (!account.note) account.note = '';
|
2022-02-07 11:06:20 -08:00
|
|
|
if (!account.avatar) account.avatar = account.avatar_static || require('images/avatar-missing.png');
|
|
|
|
if (!account.avatar_static) account.avatar_static = account.avatar;
|
2022-02-07 09:50:05 -08:00
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
const emojiMap = makeEmojiMap(account);
|
|
|
|
const displayName = account.display_name.trim().length === 0 ? account.username : account.display_name;
|
|
|
|
|
|
|
|
account.display_name_html = emojify(escapeTextContentForBrowser(displayName), emojiMap);
|
|
|
|
account.note_emojified = emojify(account.note, emojiMap);
|
2021-09-16 16:46:04 -07:00
|
|
|
account.note_plain = unescapeHTML(account.note);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
if (account.fields) {
|
|
|
|
account.fields = account.fields.map(pair => ({
|
|
|
|
...pair,
|
|
|
|
name_emojified: emojify(escapeTextContentForBrowser(pair.name)),
|
|
|
|
value_emojified: emojify(pair.value, emojiMap),
|
|
|
|
value_plain: unescapeHTML(pair.value),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account.moved) {
|
|
|
|
account.moved = account.moved.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function normalizePoll(poll) {
|
|
|
|
const normalPoll = { ...poll };
|
|
|
|
|
|
|
|
const emojiMap = makeEmojiMap(normalPoll);
|
|
|
|
|
2021-10-22 05:52:45 -07:00
|
|
|
normalPoll.options = poll.options.map((option, index) => ({
|
2020-03-27 13:59:38 -07:00
|
|
|
...option,
|
2022-02-18 18:04:03 -08:00
|
|
|
voted: Boolean(poll.own_votes?.includes(index)),
|
2020-03-27 13:59:38 -07:00
|
|
|
title_emojified: emojify(escapeTextContentForBrowser(option.title), emojiMap),
|
|
|
|
}));
|
|
|
|
|
|
|
|
return normalPoll;
|
|
|
|
}
|
2020-08-25 10:38:21 -07:00
|
|
|
|
|
|
|
export function normalizeChat(chat, normalOldChat) {
|
|
|
|
const normalChat = { ...chat };
|
2020-08-26 18:20:14 -07:00
|
|
|
const { account, last_message: lastMessage } = chat;
|
2020-08-25 10:38:21 -07:00
|
|
|
|
2020-08-26 18:20:14 -07:00
|
|
|
if (account) normalChat.account = account.id;
|
|
|
|
if (lastMessage) normalChat.last_message = lastMessage.id;
|
2020-08-25 10:38:21 -07:00
|
|
|
|
|
|
|
return normalChat;
|
|
|
|
}
|