2022-02-23 19:11:40 -08:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
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';
|
2022-05-28 09:02:04 -07:00
|
|
|
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
|
|
|
|
2022-06-20 10:59:51 -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-01-14 13:35:28 -08:00
|
|
|
}
|
2022-02-23 17:37:38 -08:00
|
|
|
};
|
|
|
|
|
2022-06-20 10:59:51 -07:00
|
|
|
const buildPoll = (pendingStatus: PendingStatus) => {
|
2022-02-23 20:41:49 -08:00
|
|
|
if (pendingStatus.hasIn(['poll', 'options'])) {
|
2022-06-20 10:59:51 -07:00
|
|
|
return pendingStatus.poll!.update('options', (options: ImmutableMap<string, any>) => {
|
2022-05-28 09:02:04 -07:00
|
|
|
return options.map((title: string) => ImmutableMap({ title }));
|
2022-02-23 20:41:49 -08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-20 10:59:51 -07:00
|
|
|
export const buildStatus = (state: RootState, pendingStatus: PendingStatus, idempotencyKey: string) => {
|
2022-05-28 09:02:04 -07:00
|
|
|
const me = state.me as string;
|
2022-02-23 17:37:38 -08:00
|
|
|
const account = getAccount(state, me);
|
2022-06-20 10:59:51 -07:00
|
|
|
const inReplyToId = pendingStatus.in_reply_to_id;
|
2022-01-14 13:35:28 -08:00
|
|
|
|
2022-02-23 19:11:40 -08:00
|
|
|
const status = ImmutableMap({
|
2021-10-09 14:56:03 -07:00
|
|
|
account,
|
2022-06-20 10:59:51 -07:00
|
|
|
content: pendingStatus.status.replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
|
2021-10-09 19:16:37 -07:00
|
|
|
id: `末pending-${idempotencyKey}`,
|
2022-05-28 09:02:04 -07:00
|
|
|
in_reply_to_account_id: state.statuses.getIn([inReplyToId, 'account'], null),
|
2022-02-23 17:37:38 -08:00
|
|
|
in_reply_to_id: inReplyToId,
|
2022-06-20 10:59:51 -07:00
|
|
|
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),
|
2022-06-20 10:59:51 -07:00
|
|
|
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
|
|
|
};
|