diff --git a/app/soapbox/normalizers/status.js b/app/soapbox/normalizers/status.ts similarity index 81% rename from app/soapbox/normalizers/status.js rename to app/soapbox/normalizers/status.ts index 494a0ced7..a4d8a8d3f 100644 --- a/app/soapbox/normalizers/status.js +++ b/app/soapbox/normalizers/status.ts @@ -1,5 +1,6 @@ import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable'; +import { IStatus } from 'soapbox/types'; import { accountToMention } from 'soapbox/utils/accounts'; import { mergeDefined } from 'soapbox/utils/normalizers'; @@ -56,7 +57,7 @@ const basePoll = ImmutableMap({ // Ensure attachments have required fields // https://docs.joinmastodon.org/entities/attachment/ -const normalizeAttachment = attachment => { +const normalizeAttachment = (attachment: ImmutableMap) => { const url = [ attachment.get('url'), attachment.get('preview_url'), @@ -72,14 +73,14 @@ const normalizeAttachment = attachment => { return attachment.mergeWith(mergeDefined, base); }; -const normalizeAttachments = status => { +const normalizeAttachments = (status: ImmutableMap) => { return status.update('media_attachments', ImmutableList(), attachments => { return attachments.map(normalizeAttachment); }); }; // Normalize mentions -const normalizeMention = mention => { +const normalizeMention = (mention: ImmutableMap) => { const base = ImmutableMap({ acct: '', username: (mention.get('acct') || '').split('@')[0], @@ -89,22 +90,22 @@ const normalizeMention = mention => { return mention.mergeWith(mergeDefined, base); }; -const normalizeMentions = status => { +const normalizeMentions = (status: ImmutableMap) => { return status.update('mentions', ImmutableList(), mentions => { return mentions.map(normalizeMention); }); }; // Normalize poll option -const normalizePollOption = option => { +const normalizePollOption = (option: ImmutableMap) => { return option.mergeWith(mergeDefined, basePollOption); }; // Normalize poll -const normalizePoll = status => { +const normalizePoll = (status: ImmutableMap) => { if (status.hasIn(['poll', 'options'])) { return status.update('poll', ImmutableMap(), poll => { - return poll.mergeWith(mergeDefined, basePoll).update('options', options => { + return poll.mergeWith(mergeDefined, basePoll).update('options', (options: ImmutableList>) => { return options.map(normalizePollOption); }); }); @@ -113,12 +114,12 @@ const normalizePoll = status => { } }; // Fix order of mentions -const fixMentionsOrder = status => { +const fixMentionsOrder = (status: ImmutableMap) => { const mentions = status.get('mentions', ImmutableList()); const inReplyToAccountId = status.get('in_reply_to_account_id'); // Sort the replied-to mention to the top - const sorted = mentions.sort((a, b) => { + const sorted = mentions.sort((a: ImmutableMap, _b: ImmutableMap) => { if (a.get('id') === inReplyToAccountId) { return -1; } else { @@ -130,7 +131,7 @@ const fixMentionsOrder = status => { }; // Add self to mentions if it's a reply to self -const addSelfMention = status => { +const addSelfMention = (status: ImmutableMap) => { const accountId = status.getIn(['account', 'id']); const isSelfReply = accountId === status.get('in_reply_to_account_id'); @@ -147,14 +148,14 @@ const addSelfMention = status => { }; // Move the quote to the top-level -const fixQuote = status => { +const fixQuote = (status: ImmutableMap) => { return status.withMutations(status => { status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null); status.deleteIn(['pleroma', 'quote']); }); }; -export const normalizeStatus = status => { +export const normalizeStatus = (status: ImmutableMap): IStatus => { return StatusRecord( status.withMutations(status => { normalizeAttachments(status); diff --git a/app/soapbox/types/index.ts b/app/soapbox/types/index.ts index bc8cbd94e..df14c2720 100644 --- a/app/soapbox/types/index.ts +++ b/app/soapbox/types/index.ts @@ -1,3 +1,4 @@ import { IAccount } from './account'; +import { IStatus } from './status'; -export { IAccount }; +export { IAccount, IStatus }; diff --git a/app/soapbox/types/status.ts b/app/soapbox/types/status.ts new file mode 100644 index 000000000..a1a1ed061 --- /dev/null +++ b/app/soapbox/types/status.ts @@ -0,0 +1,45 @@ +/** + * Status entity. + * https://docs.joinmastodon.org/entities/status/ + **/ + +interface IStatus { + account: Record; + application: Record | null; + bookmarked: boolean; + card: Record | null; + content: string; + created_at: Date; + emojis: Iterable; + favourited: boolean; + favourites_count: number; + in_reply_to_account_id: string | null; + in_reply_to_id: string | null; + id: string; + language: null; + media_attachments: Iterable; + mentions: Iterable; + muted: boolean; + pinned: boolean; + pleroma: Record; + poll: null; + quote: null; + reblog: null; + reblogged: boolean; + reblogs_count: number; + replies_count: number; + sensitive: boolean; + spoiler_text: string; + tags: Iterable; + uri: string; + url: string; + visibility: string; + + // Internal fields + contentHtml: string; + hidden: boolean; + search_index: string; + spoilerHtml: string; +} + +export { IStatus };