bigbuffet-rw/app/soapbox/actions/importer/normalizer.js

67 lines
2 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import escapeTextContentForBrowser from 'escape-html';
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 };
// 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;
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);
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);
normalPoll.options = poll.options.map((option, index) => ({
2020-03-27 13:59:38 -07:00
...option,
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;
}