import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { normalizeStatus } from 'soapbox/normalizers/status'; import { calculateStatus } from 'soapbox/reducers/statuses'; import { makeGetAccount } from 'soapbox/selectors'; import type { RootState } from 'soapbox/store'; const getAccount = makeGetAccount(); const buildMentions = (pendingStatus: ImmutableMap) => { if (pendingStatus.get('in_reply_to_id')) { return ImmutableList(pendingStatus.get('to') || []).map(acct => ImmutableMap({ acct })); } else { return ImmutableList(); } }; const buildPoll = (pendingStatus: ImmutableMap) => { if (pendingStatus.hasIn(['poll', 'options'])) { return pendingStatus.get('poll').update('options', (options: ImmutableMap) => { return options.map((title: string) => ImmutableMap({ title })); }); } else { return null; } }; export const buildStatus = (state: RootState, pendingStatus: ImmutableMap, idempotencyKey: string) => { const me = state.me as string; const account = getAccount(state, me); const inReplyToId = pendingStatus.get('in_reply_to_id'); const status = ImmutableMap({ account, content: pendingStatus.get('status', '').replace(new RegExp('\n', 'g'), '
'), /* eslint-disable-line no-control-regex */ id: `ęœ«pending-${idempotencyKey}`, in_reply_to_account_id: state.statuses.getIn([inReplyToId, 'account'], null), in_reply_to_id: inReplyToId, media_attachments: pendingStatus.get('media_ids', ImmutableList()).map((id: string) => ImmutableMap({ id })), mentions: buildMentions(pendingStatus), poll: buildPoll(pendingStatus), quote: pendingStatus.get('quote_id', null), sensitive: pendingStatus.get('sensitive', false), visibility: pendingStatus.get('visibility', 'public'), }); return calculateStatus(normalizeStatus(status)); };