bigbuffet-rw/app/soapbox/features/ui/util/pending_status_builder.js

48 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-02-23 19:11:40 -08:00
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
2022-02-19 23:27:29 -08:00
import { normalizeStatus } from 'soapbox/normalizers/status';
2022-02-21 00:56:31 -08:00
import { calculateStatus } from 'soapbox/reducers/statuses';
2022-02-23 17:37:38 -08:00
import { makeGetAccount } from 'soapbox/selectors';
2021-10-09 14:56:03 -07:00
2022-02-23 17:37:38 -08:00
const getAccount = makeGetAccount();
2021-10-09 14:56:03 -07:00
2022-02-23 19:11:40 -08:00
const buildMentions = pendingStatus => {
if (pendingStatus.get('in_reply_to_id')) {
2022-02-23 19:31:35 -08:00
return ImmutableList(pendingStatus.get('to') || []).map(acct => ImmutableMap({ acct }));
2022-02-23 17:37:38 -08:00
} else {
return ImmutableList();
}
2022-02-23 17:37:38 -08:00
};
2022-02-23 20:41:49 -08:00
const buildPoll = pendingStatus => {
if (pendingStatus.hasIn(['poll', 'options'])) {
return pendingStatus.get('poll').update('options', options => {
2022-02-23 21:07:18 -08:00
return options.map(title => ImmutableMap({ title }));
2022-02-23 20:41:49 -08:00
});
} else {
return null;
}
};
2022-02-23 17:37:38 -08:00
export const buildStatus = (state, pendingStatus, idempotencyKey) => {
const me = state.get('me');
const account = getAccount(state, me);
const inReplyToId = pendingStatus.get('in_reply_to_id');
2022-02-23 19:11:40 -08:00
const status = ImmutableMap({
2021-10-09 14:56:03 -07:00
account,
content: pendingStatus.get('status', '').replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
id: `末pending-${idempotencyKey}`,
2022-02-23 17:37:38 -08:00
in_reply_to_account_id: state.getIn(['statuses', inReplyToId, 'account'], null),
in_reply_to_id: inReplyToId,
media_attachments: pendingStatus.get('media_ids', ImmutableList()).map(id => ImmutableMap({ id })),
2022-02-23 19:11:40 -08:00
mentions: buildMentions(pendingStatus),
2022-02-23 20:41:49 -08:00
poll: buildPoll(pendingStatus),
quote: pendingStatus.get('quote_id', null),
2021-10-09 14:56:03 -07:00
sensitive: pendingStatus.get('sensitive', false),
visibility: pendingStatus.get('visibility', 'public'),
2022-02-23 19:11:40 -08:00
});
2021-10-09 14:56:03 -07:00
2022-02-23 19:11:40 -08:00
return calculateStatus(normalizeStatus(status));
2021-10-09 14:56:03 -07:00
};