2022-02-19 21:21:47 -08:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
|
|
|
|
2022-02-19 22:19:28 -08:00
|
|
|
import { accountToMention } from 'soapbox/utils/accounts';
|
|
|
|
|
2022-02-19 23:27:29 -08:00
|
|
|
// Some backends can return null, or omit these required fields
|
2022-02-23 15:02:24 -08:00
|
|
|
const baseStatus = ImmutableMap({
|
2022-02-23 17:00:49 -08:00
|
|
|
application: null,
|
|
|
|
bookmarked: false,
|
|
|
|
card: null,
|
|
|
|
created_at: new Date(),
|
2022-02-23 15:02:24 -08:00
|
|
|
emojis: ImmutableList(),
|
2022-02-23 17:00:49 -08:00
|
|
|
favourited: false,
|
|
|
|
favourites_count: 0,
|
|
|
|
in_reply_to_account_id: null,
|
|
|
|
in_reply_to_id: null,
|
|
|
|
language: null,
|
2022-02-23 15:02:24 -08:00
|
|
|
mentions: ImmutableList(),
|
2022-02-23 17:00:49 -08:00
|
|
|
muted: false,
|
|
|
|
pinned: false,
|
|
|
|
reblog: null,
|
|
|
|
reblogged: false,
|
|
|
|
reblogs_count: 0,
|
|
|
|
replies_count: 0,
|
|
|
|
spoiler_text: '',
|
|
|
|
tags: ImmutableList(),
|
|
|
|
uri: '',
|
|
|
|
url: '',
|
|
|
|
visibility: 'public',
|
2022-02-23 15:02:24 -08:00
|
|
|
});
|
|
|
|
|
2022-02-23 21:07:18 -08:00
|
|
|
const basePollOption = ImmutableMap({ title: '', votes_count: 0 });
|
|
|
|
|
|
|
|
const basePoll = ImmutableMap({
|
|
|
|
emojis: ImmutableList(),
|
|
|
|
expired: false,
|
|
|
|
expires_at: new Date(Date.now() + 1000 * (60 * 5)), // 5 minutes
|
|
|
|
multiple: false,
|
|
|
|
options: ImmutableList(),
|
|
|
|
voters_count: 0,
|
|
|
|
votes_count: 0,
|
|
|
|
});
|
|
|
|
|
2022-02-23 19:11:40 -08:00
|
|
|
// Merger function for only overriding undefined values
|
2022-02-23 15:02:24 -08:00
|
|
|
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
|
|
|
|
|
2022-02-23 19:11:40 -08:00
|
|
|
// Merge base status
|
2022-02-23 19:31:35 -08:00
|
|
|
const mergeBase = status => {
|
2022-02-23 15:02:24 -08:00
|
|
|
return status.mergeDeepWith(mergeDefined, baseStatus);
|
2022-02-19 23:27:29 -08:00
|
|
|
};
|
|
|
|
|
2022-02-19 21:21:47 -08:00
|
|
|
// Ensure attachments have required fields
|
|
|
|
// https://docs.joinmastodon.org/entities/attachment/
|
|
|
|
const normalizeAttachment = attachment => {
|
|
|
|
const url = [
|
|
|
|
attachment.get('url'),
|
|
|
|
attachment.get('preview_url'),
|
|
|
|
attachment.get('remote_url'),
|
|
|
|
].find(url => url) || '';
|
|
|
|
|
|
|
|
const base = ImmutableMap({
|
|
|
|
url,
|
|
|
|
preview_url: url,
|
|
|
|
remote_url: url,
|
|
|
|
});
|
|
|
|
|
2022-02-23 19:11:40 -08:00
|
|
|
return attachment.mergeWith(mergeDefined, base);
|
2022-02-19 21:21:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const normalizeAttachments = status => {
|
|
|
|
return status.update('media_attachments', ImmutableList(), attachments => {
|
|
|
|
return attachments.map(normalizeAttachment);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-23 19:11:40 -08:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-23 21:07:18 -08:00
|
|
|
// Normalize poll option
|
|
|
|
const normalizePollOption = option => {
|
|
|
|
return option.mergeWith(mergeDefined, basePollOption);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Normalize poll
|
|
|
|
const normalizePoll = status => {
|
|
|
|
if (status.hasIn(['poll', 'options'])) {
|
|
|
|
return status.update('poll', ImmutableMap(), poll => {
|
|
|
|
return poll.mergeWith(mergeDefined, basePoll).update('options', options => {
|
|
|
|
return options.map(normalizePollOption);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return status.set('poll', null);
|
|
|
|
}
|
|
|
|
};
|
2022-02-19 21:21:47 -08:00
|
|
|
// Fix order of mentions
|
2022-02-23 19:11:40 -08:00
|
|
|
const fixMentionsOrder = status => {
|
|
|
|
const mentions = status.get('mentions', ImmutableList());
|
2022-02-19 21:21:47 -08:00
|
|
|
const inReplyToAccountId = status.get('in_reply_to_account_id');
|
|
|
|
|
|
|
|
// Sort the replied-to mention to the top
|
|
|
|
const sorted = mentions.sort((a, b) => {
|
|
|
|
if (a.get('id') === inReplyToAccountId) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return status.set('mentions', sorted);
|
|
|
|
};
|
|
|
|
|
2022-02-19 22:19:28 -08:00
|
|
|
// Add self to mentions if it's a reply to self
|
|
|
|
const addSelfMention = status => {
|
|
|
|
const accountId = status.getIn(['account', 'id']);
|
|
|
|
|
|
|
|
const isSelfReply = accountId === status.get('in_reply_to_account_id');
|
|
|
|
const hasSelfMention = accountId === status.getIn(['mentions', 0, 'id']);
|
|
|
|
|
|
|
|
if (isSelfReply && !hasSelfMention) {
|
|
|
|
const mention = accountToMention(status.get('account'));
|
|
|
|
return status.update('mentions', ImmutableList(), mentions => (
|
|
|
|
ImmutableList([mention]).concat(mentions)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-23 15:25:38 -08:00
|
|
|
// Move the quote to the top-level
|
|
|
|
const fixQuote = status => {
|
|
|
|
return status.withMutations(status => {
|
|
|
|
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
|
|
|
|
status.deleteIn(['pleroma', 'quote']);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-19 21:21:47 -08:00
|
|
|
export const normalizeStatus = status => {
|
|
|
|
return status.withMutations(status => {
|
2022-02-23 19:31:35 -08:00
|
|
|
mergeBase(status);
|
2022-02-19 21:21:47 -08:00
|
|
|
normalizeAttachments(status);
|
2022-02-23 19:11:40 -08:00
|
|
|
normalizeMentions(status);
|
2022-02-23 21:07:18 -08:00
|
|
|
normalizePoll(status);
|
2022-02-23 19:11:40 -08:00
|
|
|
fixMentionsOrder(status);
|
|
|
|
addSelfMention(status);
|
|
|
|
fixQuote(status);
|
2022-02-19 21:21:47 -08:00
|
|
|
});
|
|
|
|
};
|