bigbuffet-rw/app/soapbox/features/ui/util/pending-status-builder.ts

51 lines
1.8 KiB
TypeScript
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-11-15 12:39:43 -08:00
import type { PendingStatus } from 'soapbox/reducers/pending-statuses';
import type { RootState } from 'soapbox/store';
2022-02-23 17:37:38 -08:00
const getAccount = makeGetAccount();
2021-10-09 14:56:03 -07:00
const buildMentions = (pendingStatus: PendingStatus) => {
if (pendingStatus.in_reply_to_id) {
return ImmutableList(pendingStatus.to || []).map(acct => ImmutableMap({ acct }));
2022-02-23 17:37:38 -08:00
} else {
return ImmutableList();
}
2022-02-23 17:37:38 -08:00
};
const buildPoll = (pendingStatus: PendingStatus) => {
2022-02-23 20:41:49 -08:00
if (pendingStatus.hasIn(['poll', 'options'])) {
return pendingStatus.poll!.update('options', (options: ImmutableMap<string, any>) => {
return options.map((title: string) => ImmutableMap({ title }));
2022-02-23 20:41:49 -08:00
});
} else {
return null;
}
};
export const buildStatus = (state: RootState, pendingStatus: PendingStatus, idempotencyKey: string) => {
const me = state.me as string;
2022-02-23 17:37:38 -08:00
const account = getAccount(state, me);
const inReplyToId = pendingStatus.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.status.replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
id: `末pending-${idempotencyKey}`,
in_reply_to_account_id: state.statuses.getIn([inReplyToId, 'account'], null),
2022-02-23 17:37:38 -08:00
in_reply_to_id: inReplyToId,
media_attachments: (pendingStatus.media_ids || ImmutableList()).map((id: string) => 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.quote_id,
sensitive: pendingStatus.sensitive,
visibility: pendingStatus.visibility,
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
};