2020-06-12 17:01:54 +01:00
|
|
|
import escapeTextContentForBrowser from 'escape-html'
|
|
|
|
import emojify from '../../components/emoji/emoji'
|
|
|
|
import { unescapeHTML } from '../../utils/html'
|
|
|
|
import { expandSpoilers } from '../../initial_state'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-06-12 17:01:54 +01:00
|
|
|
const domParser = new DOMParser()
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
const makeEmojiMap = record => record.emojis.reduce((obj, emoji) => {
|
2020-11-25 21:22:37 +00:00
|
|
|
obj[`:${emoji.shortcode}:`] = emoji
|
|
|
|
return obj
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const normalizeAccount = (account) => {
|
|
|
|
account = { ...account }
|
|
|
|
|
|
|
|
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.display_name_plain = emojify(escapeTextContentForBrowser(displayName), emojiMap, true)
|
|
|
|
account.note_emojified = emojify(account.note, emojiMap)
|
2020-07-17 21:18:56 +01:00
|
|
|
account.note_plain = unescapeHTML(account.note)
|
2019-07-02 08:10:25 +01: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),
|
2020-11-25 21:22:37 +00:00
|
|
|
}))
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (account.moved) {
|
2020-11-25 21:22:37 +00:00
|
|
|
account.moved = account.moved.id
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
return account
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const normalizeStatus = (status, normalOldStatus) => {
|
|
|
|
const normalStatus = { ...status }
|
|
|
|
normalStatus.account = status.account_id || status.account.id
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (status.reblog && status.reblog.id) {
|
2020-11-25 21:22:37 +00:00
|
|
|
normalStatus.reblog = status.reblog.id
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2019-07-31 20:45:53 +01:00
|
|
|
if (status.quote && status.quote.id) {
|
2020-11-25 21:22:37 +00:00
|
|
|
normalStatus.quote = status.quote.id
|
2019-07-31 20:45:53 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
if (status.poll && status.poll.id) {
|
2020-11-25 21:22:37 +00:00
|
|
|
normalStatus.poll = status.poll.id
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!!status.group || !!status.group_id) {
|
2020-11-25 21:22:37 +00:00
|
|
|
normalStatus.group = status.group_id || status.group.id
|
2020-11-15 18:48:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
// Only calculate these values when status first encountered
|
|
|
|
// Otherwise keep the ones already in the reducer
|
2019-08-23 00:05:20 +01:00
|
|
|
if (normalOldStatus && normalOldStatus.get('content') === normalStatus.content && normalOldStatus.get('spoiler_text') === normalStatus.spoiler_text) {
|
2020-11-25 21:22:37 +00:00
|
|
|
normalStatus.search_index = normalOldStatus.get('search_index')
|
|
|
|
normalStatus.contentHtml = normalOldStatus.get('contentHtml')
|
|
|
|
normalStatus.spoilerHtml = normalOldStatus.get('spoilerHtml')
|
|
|
|
normalStatus.hidden = normalOldStatus.get('hidden')
|
2019-07-02 08:10:25 +01:00
|
|
|
} else {
|
2020-11-25 21:22:37 +00:00
|
|
|
const spoilerText = normalStatus.spoiler_text || ''
|
|
|
|
const searchContent = [spoilerText, status.content].join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n')
|
|
|
|
const emojiMap = makeEmojiMap(normalStatus)
|
|
|
|
const theContent = !!normalStatus.rich_content ? normalStatus.rich_content : normalStatus.content
|
|
|
|
|
|
|
|
normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent
|
|
|
|
normalStatus.contentHtml = emojify(theContent, emojiMap, false, true)
|
|
|
|
normalStatus.spoilerHtml = emojify(escapeTextContentForBrowser(spoilerText), emojiMap)
|
|
|
|
normalStatus.hidden = expandSpoilers ? false : spoilerText.length > 0 || normalStatus.sensitive
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
return normalStatus
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const normalizePoll = (poll) => {
|
|
|
|
const normalPoll = { ...poll }
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
const emojiMap = makeEmojiMap(normalPoll)
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
normalPoll.options = poll.options.map((option) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
...option,
|
|
|
|
title_emojified: emojify(escapeTextContentForBrowser(option.title), emojiMap),
|
2020-11-25 21:22:37 +00:00
|
|
|
}))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
return normalPoll
|
2020-06-17 22:29:24 +01:00
|
|
|
}
|