soapbox/app/soapbox/normalizers/status.ts

156 wiersze
4.7 KiB
TypeScript
Czysty Zwykły widok Historia

2022-03-12 21:01:00 +00:00
/**
* Status normalizer:
* Converts API statuses into our internal format.
* @see {@link https://docs.joinmastodon.org/entities/status/}
*/
2022-03-09 22:00:43 +00:00
import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
2022-03-17 02:33:09 +00:00
fromJS,
2022-03-09 22:00:43 +00:00
} from 'immutable';
2022-02-20 05:21:47 +00:00
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
2022-03-12 21:54:28 +00:00
import { normalizeCard } from 'soapbox/normalizers/card';
2022-03-12 02:48:00 +00:00
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
import { normalizeMention } from 'soapbox/normalizers/mention';
import { normalizePoll } from 'soapbox/normalizers/poll';
2022-02-20 06:19:28 +00:00
import type { Account, Attachment, Card, Emoji, Mention, Poll, EmbeddedEntity } from 'soapbox/types/entities';
type StatusVisibility = 'public' | 'unlisted' | 'private' | 'direct';
2022-03-12 21:01:00 +00:00
// https://docs.joinmastodon.org/entities/status/
2022-03-17 02:15:38 +00:00
export const StatusRecord = ImmutableRecord({
account: null as EmbeddedEntity<Account>,
application: null as ImmutableMap<string, any> | null,
bookmarked: false,
card: null as EmbeddedEntity<Card>,
2022-03-09 05:47:30 +00:00
content: '',
created_at: new Date(),
emojis: ImmutableList<Emoji>(),
favourited: false,
favourites_count: 0,
in_reply_to_account_id: null as string | null,
in_reply_to_id: null as string | null,
2022-03-09 04:02:02 +00:00
id: '',
language: null as string | null,
media_attachments: ImmutableList<Attachment>(),
mentions: ImmutableList<Mention>(),
muted: false,
pinned: false,
pleroma: ImmutableMap<string, any>(),
poll: null as EmbeddedEntity<Poll>,
quote: null as EmbeddedEntity<any>,
reblog: null as EmbeddedEntity<any>,
reblogged: false,
reblogs_count: 0,
replies_count: 0,
2022-03-09 04:02:02 +00:00
sensitive: false,
spoiler_text: '',
tags: ImmutableList<ImmutableMap<string, any>>(),
uri: '',
url: '',
visibility: 'public' as StatusVisibility,
2022-03-09 05:25:30 +00:00
// Internal fields
contentHtml: '',
hidden: false,
search_index: '',
spoilerHtml: '',
2022-02-23 23:02:24 +00:00
});
const normalizeAttachments = (status: ImmutableMap<string, any>) => {
2022-02-20 05:21:47 +00:00
return status.update('media_attachments', ImmutableList(), attachments => {
return attachments.map(normalizeAttachment);
});
};
const normalizeMentions = (status: ImmutableMap<string, any>) => {
return status.update('mentions', ImmutableList(), mentions => {
return mentions.map(normalizeMention);
});
2022-02-24 03:11:40 +00:00
};
2022-03-11 01:55:14 +00:00
// Normalize emojis
2022-03-11 02:40:04 +00:00
const normalizeEmojis = (entity: ImmutableMap<string, any>) => {
return entity.update('emojis', ImmutableList(), emojis => {
2022-03-12 02:48:00 +00:00
return emojis.map(normalizeEmoji);
2022-03-11 01:55:14 +00:00
});
};
2022-03-10 22:25:11 +00:00
// Normalize the poll in the status, if applicable
const normalizeStatusPoll = (status: ImmutableMap<string, any>) => {
2022-02-24 05:07:18 +00:00
if (status.hasIn(['poll', 'options'])) {
2022-03-10 22:25:11 +00:00
return status.update('poll', ImmutableMap(), normalizePoll);
2022-02-24 05:07:18 +00:00
} else {
return status.set('poll', null);
}
};
2022-03-12 21:54:28 +00:00
// Normalize card
const normalizeStatusCard = (status: ImmutableMap<string, any>) => {
if (status.get('card')) {
return status.update('card', ImmutableMap(), normalizeCard);
} else {
return status.set('card', null);
}
};
2022-02-20 05:21:47 +00:00
// Fix order of mentions
const fixMentionsOrder = (status: ImmutableMap<string, any>) => {
2022-02-24 03:11:40 +00:00
const mentions = status.get('mentions', ImmutableList());
2022-02-20 05:21:47 +00: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-20 05:21:47 +00:00
if (a.get('id') === inReplyToAccountId) {
return -1;
} else {
return 0;
}
});
return status.set('mentions', sorted);
};
2022-02-20 06:19:28 +00:00
// Add self to mentions if it's a reply to self
const addSelfMention = (status: ImmutableMap<string, any>) => {
2022-02-20 06:19:28 +00: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 23:57:12 +00:00
if (isSelfReply && !hasSelfMention && accountId) {
const mention = normalizeMention(status.get('account'));
2022-02-20 06:19:28 +00:00
return status.update('mentions', ImmutableList(), mentions => (
ImmutableList([mention]).concat(mentions)
));
} else {
return status;
}
};
2022-02-23 23:25:38 +00:00
// Move the quote to the top-level
const fixQuote = (status: ImmutableMap<string, any>) => {
2022-02-23 23:25:38 +00:00
return status.withMutations(status => {
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
status.deleteIn(['pleroma', 'quote']);
});
};
2022-03-19 19:41:16 +00:00
export const normalizeStatus = (status: Record<string, any>) => {
2022-03-09 04:02:02 +00:00
return StatusRecord(
2022-03-17 02:33:09 +00:00
ImmutableMap(fromJS(status)).withMutations(status => {
2022-03-09 04:02:02 +00:00
normalizeAttachments(status);
normalizeMentions(status);
2022-03-11 01:55:14 +00:00
normalizeEmojis(status);
2022-03-10 22:25:11 +00:00
normalizeStatusPoll(status);
2022-03-12 21:54:28 +00:00
normalizeStatusCard(status);
2022-03-09 04:02:02 +00:00
fixMentionsOrder(status);
addSelfMention(status);
fixQuote(status);
}),
);
2022-02-20 05:21:47 +00:00
};