kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
More defensive status normalization
rodzic
cd6d575ae9
commit
02f05abeaa
|
@ -1,4 +1,4 @@
|
||||||
import { fromJS } from 'immutable';
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
import { normalizeStatus } from 'soapbox/normalizers/status';
|
import { normalizeStatus } from 'soapbox/normalizers/status';
|
||||||
import { calculateStatus } from 'soapbox/reducers/statuses';
|
import { calculateStatus } from 'soapbox/reducers/statuses';
|
||||||
|
@ -11,7 +11,7 @@ export const buildStatus = (state, scheduledStatus) => {
|
||||||
const params = scheduledStatus.get('params');
|
const params = scheduledStatus.get('params');
|
||||||
const account = getAccount(state, me);
|
const account = getAccount(state, me);
|
||||||
|
|
||||||
const status = {
|
const status = ImmutableMap({
|
||||||
account,
|
account,
|
||||||
content: params.get('text', '').replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
|
content: params.get('text', '').replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
|
||||||
created_at: params.get('scheduled_at'),
|
created_at: params.get('scheduled_at'),
|
||||||
|
@ -23,7 +23,7 @@ export const buildStatus = (state, scheduledStatus) => {
|
||||||
uri: `/scheduled_statuses/${scheduledStatus.get('id')}`,
|
uri: `/scheduled_statuses/${scheduledStatus.get('id')}`,
|
||||||
url: `/scheduled_statuses/${scheduledStatus.get('id')}`,
|
url: `/scheduled_statuses/${scheduledStatus.get('id')}`,
|
||||||
visibility: params.get('visibility'),
|
visibility: params.get('visibility'),
|
||||||
};
|
});
|
||||||
|
|
||||||
return calculateStatus(normalizeStatus(fromJS(status)));
|
return calculateStatus(normalizeStatus(status));
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
||||||
|
|
||||||
import { normalizeStatus } from 'soapbox/normalizers/status';
|
import { normalizeStatus } from 'soapbox/normalizers/status';
|
||||||
import { calculateStatus } from 'soapbox/reducers/statuses';
|
import { calculateStatus } from 'soapbox/reducers/statuses';
|
||||||
|
@ -6,11 +6,11 @@ import { makeGetAccount } from 'soapbox/selectors';
|
||||||
|
|
||||||
const getAccount = makeGetAccount();
|
const getAccount = makeGetAccount();
|
||||||
|
|
||||||
const getMentions = pendingStatus => {
|
const buildMentions = pendingStatus => {
|
||||||
if (pendingStatus.get('in_reply_to_id')) {
|
if (pendingStatus.get('in_reply_to_id')) {
|
||||||
return ImmutableList(pendingStatus.get('to') || []).map(mention => ImmutableMap({
|
return ImmutableList(pendingStatus.get('to') || []).map(acct => ImmutableMap({
|
||||||
username: mention.split('@')[0],
|
acct,
|
||||||
acct: mention,
|
username: acct.split('@')[0],
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
return ImmutableList();
|
return ImmutableList();
|
||||||
|
@ -22,19 +22,19 @@ export const buildStatus = (state, pendingStatus, idempotencyKey) => {
|
||||||
const account = getAccount(state, me);
|
const account = getAccount(state, me);
|
||||||
const inReplyToId = pendingStatus.get('in_reply_to_id');
|
const inReplyToId = pendingStatus.get('in_reply_to_id');
|
||||||
|
|
||||||
const status = {
|
const status = ImmutableMap({
|
||||||
account,
|
account,
|
||||||
content: pendingStatus.get('status', '').replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
|
content: pendingStatus.get('status', '').replace(new RegExp('\n', 'g'), '<br>'), /* eslint-disable-line no-control-regex */
|
||||||
id: `末pending-${idempotencyKey}`,
|
id: `末pending-${idempotencyKey}`,
|
||||||
in_reply_to_account_id: state.getIn(['statuses', inReplyToId, 'account'], null),
|
in_reply_to_account_id: state.getIn(['statuses', inReplyToId, 'account'], null),
|
||||||
in_reply_to_id: inReplyToId,
|
in_reply_to_id: inReplyToId,
|
||||||
media_attachments: pendingStatus.get('media_ids', ImmutableList()).map(id => ImmutableMap({ id })),
|
media_attachments: pendingStatus.get('media_ids', ImmutableList()).map(id => ImmutableMap({ id })),
|
||||||
mentions: getMentions(pendingStatus),
|
mentions: buildMentions(pendingStatus),
|
||||||
poll: pendingStatus.get('poll', null),
|
poll: pendingStatus.get('poll', null),
|
||||||
quote: pendingStatus.get('quote_id', null),
|
quote: pendingStatus.get('quote_id', null),
|
||||||
sensitive: pendingStatus.get('sensitive', false),
|
sensitive: pendingStatus.get('sensitive', false),
|
||||||
visibility: pendingStatus.get('visibility', 'public'),
|
visibility: pendingStatus.get('visibility', 'public'),
|
||||||
};
|
});
|
||||||
|
|
||||||
return calculateStatus(normalizeStatus(fromJS(status)));
|
return calculateStatus(normalizeStatus(status));
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,8 +28,10 @@ const baseStatus = ImmutableMap({
|
||||||
visibility: 'public',
|
visibility: 'public',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Merger function for only overriding undefined values
|
||||||
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
||||||
|
|
||||||
|
// Merge base status
|
||||||
const setRequiredFields = status => {
|
const setRequiredFields = status => {
|
||||||
return status.mergeDeepWith(mergeDefined, baseStatus);
|
return status.mergeDeepWith(mergeDefined, baseStatus);
|
||||||
};
|
};
|
||||||
|
@ -49,7 +51,7 @@ const normalizeAttachment = attachment => {
|
||||||
remote_url: url,
|
remote_url: url,
|
||||||
});
|
});
|
||||||
|
|
||||||
return attachment.mergeWith((o, n) => o || n, base);
|
return attachment.mergeWith(mergeDefined, base);
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeAttachments = status => {
|
const normalizeAttachments = status => {
|
||||||
|
@ -58,9 +60,26 @@ const normalizeAttachments = status => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Normalize mentions
|
||||||
|
const normalizeMention = mention => {
|
||||||
|
const base = ImmutableMap({
|
||||||
|
acct: '',
|
||||||
|
username: (mention.get('acct') || '').split('@')[0],
|
||||||
|
url: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
return mention.mergeWith(mergeDefined, base);
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeMentions = status => {
|
||||||
|
return status.update('mentions', ImmutableList(), mentions => {
|
||||||
|
return mentions.map(normalizeMention);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Fix order of mentions
|
// Fix order of mentions
|
||||||
const fixMentions = status => {
|
const fixMentionsOrder = status => {
|
||||||
const mentions = status.get('mentions');
|
const mentions = status.get('mentions', ImmutableList());
|
||||||
const inReplyToAccountId = status.get('in_reply_to_account_id');
|
const inReplyToAccountId = status.get('in_reply_to_account_id');
|
||||||
|
|
||||||
// Sort the replied-to mention to the top
|
// Sort the replied-to mention to the top
|
||||||
|
@ -103,9 +122,10 @@ const fixQuote = status => {
|
||||||
export const normalizeStatus = status => {
|
export const normalizeStatus = status => {
|
||||||
return status.withMutations(status => {
|
return status.withMutations(status => {
|
||||||
setRequiredFields(status);
|
setRequiredFields(status);
|
||||||
fixMentions(status);
|
|
||||||
fixQuote(status);
|
|
||||||
addSelfMention(status);
|
|
||||||
normalizeAttachments(status);
|
normalizeAttachments(status);
|
||||||
|
normalizeMentions(status);
|
||||||
|
fixMentionsOrder(status);
|
||||||
|
addSelfMention(status);
|
||||||
|
fixQuote(status);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Ładowanie…
Reference in New Issue