pleroma/app/soapbox/normalizers/status.ts

253 lines
6.7 KiB
TypeScript
Raw Normal View History

2022-03-10 17:55:14 -08:00
import escapeTextContentForBrowser from 'escape-html';
2022-03-09 14:00:43 -08:00
import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} from 'immutable';
2022-02-19 21:21:47 -08:00
2022-03-10 17:55:14 -08:00
import emojify from 'soapbox/features/emoji/emoji';
2022-03-10 15:57:12 -08:00
import { normalizeAccount } from 'soapbox/normalizers/account';
2022-03-11 18:48:00 -08:00
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
import { IStatus } from 'soapbox/types';
2022-03-10 17:55:14 -08:00
import { mergeDefined, makeEmojiMap } from 'soapbox/utils/normalizers';
2022-02-19 22:19:28 -08:00
2022-03-09 14:00:43 -08:00
const StatusRecord = ImmutableRecord({
account: null,
application: null,
bookmarked: false,
card: null,
2022-03-08 21:47:30 -08:00
content: '',
created_at: new Date(),
2022-02-23 15:02:24 -08:00
emojis: ImmutableList(),
favourited: false,
favourites_count: 0,
in_reply_to_account_id: null,
in_reply_to_id: null,
2022-03-08 20:02:02 -08:00
id: '',
language: null,
2022-03-08 20:02:02 -08:00
media_attachments: ImmutableList(),
2022-02-23 15:02:24 -08:00
mentions: ImmutableList(),
muted: false,
pinned: false,
2022-03-08 20:02:02 -08:00
pleroma: ImmutableMap(),
poll: null,
quote: null,
reblog: null,
reblogged: false,
reblogs_count: 0,
replies_count: 0,
2022-03-08 20:02:02 -08:00
sensitive: false,
spoiler_text: '',
tags: ImmutableList(),
uri: '',
url: '',
visibility: 'public',
2022-03-08 21:25:30 -08:00
// Internal fields
contentHtml: '',
hidden: false,
search_index: '',
spoilerHtml: '',
2022-02-23 15:02:24 -08:00
});
// https://docs.joinmastodon.org/entities/attachment/
const AttachmentRecord = ImmutableRecord({
blurhash: undefined,
description: '',
id: '',
meta: ImmutableMap(),
pleroma: ImmutableMap(),
preview_url: '',
remote_url: null,
type: 'unknown',
url: '',
// Internal fields
account: null,
status: null,
});
2022-03-10 15:57:12 -08:00
// https://docs.joinmastodon.org/entities/mention/
const MentionRecord = ImmutableRecord({
id: '',
acct: '',
username: '',
url: '',
});
2022-03-10 17:55:14 -08:00
// https://docs.joinmastodon.org/entities/poll/
const PollRecord = ImmutableRecord({
emojis: ImmutableList(),
expired: false,
expires_at: new Date(),
id: '',
multiple: false,
options: ImmutableList(),
voters_count: 0,
votes_count: 0,
own_votes: null,
voted: false,
});
// Sub-entity of Poll
const PollOptionRecord = ImmutableRecord({
title: '',
votes_count: 0,
// Internal fields
title_emojified: '',
});
2022-02-19 21:21:47 -08:00
// Ensure attachments have required fields
// https://docs.joinmastodon.org/entities/attachment/
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
2022-02-19 21:21:47 -08:00
const url = [
attachment.get('url'),
attachment.get('preview_url'),
attachment.get('remote_url'),
].find(url => url) || '';
const base = ImmutableMap({
url,
preview_url: url,
});
return AttachmentRecord(attachment.mergeWith(mergeDefined, base));
2022-02-19 21:21:47 -08:00
};
const normalizeAttachments = (status: ImmutableMap<string, any>) => {
2022-02-19 21:21:47 -08:00
return status.update('media_attachments', ImmutableList(), attachments => {
return attachments.map(normalizeAttachment);
});
};
2022-02-23 19:11:40 -08:00
// Normalize mentions
const normalizeMention = (mention: ImmutableMap<string, any>) => {
2022-03-10 15:57:12 -08:00
return MentionRecord(normalizeAccount(mention));
2022-02-23 19:11:40 -08:00
};
const normalizeMentions = (status: ImmutableMap<string, any>) => {
2022-03-11 18:48:00 -08:00
let mentions;
mentions = status.get('mentions', ImmutableList());
mentions = mentions.map(normalizeMention);
return status.set('mentions', mentions);
2022-02-23 19:11:40 -08:00
};
2022-03-10 17:55:14 -08:00
// Normalize emojis
2022-03-10 18:40:04 -08:00
const normalizeEmojis = (entity: ImmutableMap<string, any>) => {
return entity.update('emojis', ImmutableList(), emojis => {
2022-03-11 18:48:00 -08:00
return emojis.map(normalizeEmoji);
2022-03-10 17:55:14 -08:00
});
};
const normalizePollOption = (option: ImmutableMap<string, any>, emojis: ImmutableList<ImmutableMap<string, string>> = ImmutableList()) => {
const emojiMap = makeEmojiMap(emojis);
const titleEmojified = emojify(escapeTextContentForBrowser(option.get('title')), emojiMap);
return PollOptionRecord(
option.set('title_emojified', titleEmojified),
);
};
2022-03-10 14:25:11 -08:00
// Normalize poll options
const normalizePollOptions = (poll: ImmutableMap<string, any>) => {
2022-03-10 17:55:14 -08:00
const emojis = poll.get('emojis');
2022-03-10 14:25:11 -08:00
return poll.update('options', (options: ImmutableList<ImmutableMap<string, any>>) => {
2022-03-10 17:55:14 -08:00
return options.map(option => normalizePollOption(option, emojis));
2022-03-10 14:25:11 -08:00
});
};
// Normalize own_votes to `null` if empty (like Mastodon)
const normalizePollOwnVotes = (poll: ImmutableMap<string, any>) => {
return poll.update('own_votes', ownVotes => {
return ownVotes?.size > 0 ? ownVotes : null;
});
};
// Whether the user voted in the poll
const normalizePollVoted = (poll: ImmutableMap<string, any>) => {
return poll.update('voted', voted => {
return typeof voted === 'boolean' ? voted : poll.get('own_votes')?.size > 0;
});
};
// Normalize the actual poll
const normalizePoll = (poll: ImmutableMap<string, any>) => {
return PollRecord(
poll.withMutations((poll: ImmutableMap<string, any>) => {
2022-03-10 17:55:14 -08:00
normalizeEmojis(poll);
2022-03-10 14:25:11 -08:00
normalizePollOptions(poll);
normalizePollOwnVotes(poll);
normalizePollVoted(poll);
}),
);
};
// Normalize the poll in the status, if applicable
const normalizeStatusPoll = (status: ImmutableMap<string, any>) => {
2022-02-23 21:07:18 -08:00
if (status.hasIn(['poll', 'options'])) {
2022-03-10 14:25:11 -08:00
return status.update('poll', ImmutableMap(), normalizePoll);
2022-02-23 21:07:18 -08:00
} else {
return status.set('poll', null);
}
};
2022-02-19 21:21:47 -08:00
// Fix order of mentions
const fixMentionsOrder = (status: ImmutableMap<string, any>) => {
2022-02-23 19:11:40 -08:00
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: ImmutableMap<string, any>, _b: ImmutableMap<string, any>) => {
2022-02-19 21:21:47 -08:00
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: ImmutableMap<string, any>) => {
2022-02-19 22:19:28 -08:00
const accountId = status.getIn(['account', 'id']);
const isSelfReply = accountId === status.get('in_reply_to_account_id');
const hasSelfMention = accountId === status.getIn(['mentions', 0, 'id']);
2022-03-10 15:57:12 -08:00
if (isSelfReply && !hasSelfMention && accountId) {
const mention = normalizeMention(status.get('account'));
2022-02-19 22:19:28 -08:00
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: ImmutableMap<string, any>) => {
2022-02-23 15:25:38 -08:00
return status.withMutations(status => {
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
status.deleteIn(['pleroma', 'quote']);
});
};
2022-03-10 14:25:11 -08:00
export const normalizeStatus = (status: ImmutableMap<string, any>): IStatus => {
2022-03-08 20:02:02 -08:00
return StatusRecord(
status.withMutations(status => {
normalizeAttachments(status);
normalizeMentions(status);
2022-03-10 17:55:14 -08:00
normalizeEmojis(status);
2022-03-10 14:25:11 -08:00
normalizeStatusPoll(status);
2022-03-08 20:02:02 -08:00
fixMentionsOrder(status);
addSelfMention(status);
fixQuote(status);
}),
);
2022-02-19 21:21:47 -08:00
};