kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Convert Status Normalizer to TypeScript
rodzic
6e61cb525c
commit
a2adaf2ffd
|
@ -1,5 +1,6 @@
|
||||||
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
|
||||||
|
|
||||||
|
import { IStatus } from 'soapbox/types';
|
||||||
import { accountToMention } from 'soapbox/utils/accounts';
|
import { accountToMention } from 'soapbox/utils/accounts';
|
||||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
|
@ -56,7 +57,7 @@ const basePoll = ImmutableMap({
|
||||||
|
|
||||||
// Ensure attachments have required fields
|
// Ensure attachments have required fields
|
||||||
// https://docs.joinmastodon.org/entities/attachment/
|
// https://docs.joinmastodon.org/entities/attachment/
|
||||||
const normalizeAttachment = attachment => {
|
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||||
const url = [
|
const url = [
|
||||||
attachment.get('url'),
|
attachment.get('url'),
|
||||||
attachment.get('preview_url'),
|
attachment.get('preview_url'),
|
||||||
|
@ -72,14 +73,14 @@ const normalizeAttachment = attachment => {
|
||||||
return attachment.mergeWith(mergeDefined, base);
|
return attachment.mergeWith(mergeDefined, base);
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeAttachments = status => {
|
const normalizeAttachments = (status: ImmutableMap<string, any>) => {
|
||||||
return status.update('media_attachments', ImmutableList(), attachments => {
|
return status.update('media_attachments', ImmutableList(), attachments => {
|
||||||
return attachments.map(normalizeAttachment);
|
return attachments.map(normalizeAttachment);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Normalize mentions
|
// Normalize mentions
|
||||||
const normalizeMention = mention => {
|
const normalizeMention = (mention: ImmutableMap<string, any>) => {
|
||||||
const base = ImmutableMap({
|
const base = ImmutableMap({
|
||||||
acct: '',
|
acct: '',
|
||||||
username: (mention.get('acct') || '').split('@')[0],
|
username: (mention.get('acct') || '').split('@')[0],
|
||||||
|
@ -89,22 +90,22 @@ const normalizeMention = mention => {
|
||||||
return mention.mergeWith(mergeDefined, base);
|
return mention.mergeWith(mergeDefined, base);
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeMentions = status => {
|
const normalizeMentions = (status: ImmutableMap<string, any>) => {
|
||||||
return status.update('mentions', ImmutableList(), mentions => {
|
return status.update('mentions', ImmutableList(), mentions => {
|
||||||
return mentions.map(normalizeMention);
|
return mentions.map(normalizeMention);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Normalize poll option
|
// Normalize poll option
|
||||||
const normalizePollOption = option => {
|
const normalizePollOption = (option: ImmutableMap<string, any>) => {
|
||||||
return option.mergeWith(mergeDefined, basePollOption);
|
return option.mergeWith(mergeDefined, basePollOption);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Normalize poll
|
// Normalize poll
|
||||||
const normalizePoll = status => {
|
const normalizePoll = (status: ImmutableMap<string, any>) => {
|
||||||
if (status.hasIn(['poll', 'options'])) {
|
if (status.hasIn(['poll', 'options'])) {
|
||||||
return status.update('poll', ImmutableMap(), poll => {
|
return status.update('poll', ImmutableMap(), poll => {
|
||||||
return poll.mergeWith(mergeDefined, basePoll).update('options', options => {
|
return poll.mergeWith(mergeDefined, basePoll).update('options', (options: ImmutableList<ImmutableMap<string, any>>) => {
|
||||||
return options.map(normalizePollOption);
|
return options.map(normalizePollOption);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -113,12 +114,12 @@ const normalizePoll = status => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Fix order of mentions
|
// Fix order of mentions
|
||||||
const fixMentionsOrder = status => {
|
const fixMentionsOrder = (status: ImmutableMap<string, any>) => {
|
||||||
const mentions = status.get('mentions', ImmutableList());
|
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
|
||||||
const sorted = mentions.sort((a, b) => {
|
const sorted = mentions.sort((a: ImmutableMap<string, any>, _b: ImmutableMap<string, any>) => {
|
||||||
if (a.get('id') === inReplyToAccountId) {
|
if (a.get('id') === inReplyToAccountId) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -130,7 +131,7 @@ const fixMentionsOrder = status => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add self to mentions if it's a reply to self
|
// Add self to mentions if it's a reply to self
|
||||||
const addSelfMention = status => {
|
const addSelfMention = (status: ImmutableMap<string, any>) => {
|
||||||
const accountId = status.getIn(['account', 'id']);
|
const accountId = status.getIn(['account', 'id']);
|
||||||
|
|
||||||
const isSelfReply = accountId === status.get('in_reply_to_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
|
// Move the quote to the top-level
|
||||||
const fixQuote = status => {
|
const fixQuote = (status: ImmutableMap<string, any>) => {
|
||||||
return status.withMutations(status => {
|
return status.withMutations(status => {
|
||||||
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
|
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
|
||||||
status.deleteIn(['pleroma', 'quote']);
|
status.deleteIn(['pleroma', 'quote']);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const normalizeStatus = status => {
|
export const normalizeStatus = (status: ImmutableMap<any, string>): IStatus => {
|
||||||
return StatusRecord(
|
return StatusRecord(
|
||||||
status.withMutations(status => {
|
status.withMutations(status => {
|
||||||
normalizeAttachments(status);
|
normalizeAttachments(status);
|
|
@ -1,3 +1,4 @@
|
||||||
import { IAccount } from './account';
|
import { IAccount } from './account';
|
||||||
|
import { IStatus } from './status';
|
||||||
|
|
||||||
export { IAccount };
|
export { IAccount, IStatus };
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
* Status entity.
|
||||||
|
* https://docs.joinmastodon.org/entities/status/
|
||||||
|
**/
|
||||||
|
|
||||||
|
interface IStatus {
|
||||||
|
account: Record<any, any>;
|
||||||
|
application: Record<string, any> | null;
|
||||||
|
bookmarked: boolean;
|
||||||
|
card: Record<string, any> | null;
|
||||||
|
content: string;
|
||||||
|
created_at: Date;
|
||||||
|
emojis: Iterable<any>;
|
||||||
|
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<any>;
|
||||||
|
mentions: Iterable<any>;
|
||||||
|
muted: boolean;
|
||||||
|
pinned: boolean;
|
||||||
|
pleroma: Record<string, any>;
|
||||||
|
poll: null;
|
||||||
|
quote: null;
|
||||||
|
reblog: null;
|
||||||
|
reblogged: boolean;
|
||||||
|
reblogs_count: number;
|
||||||
|
replies_count: number;
|
||||||
|
sensitive: boolean;
|
||||||
|
spoiler_text: string;
|
||||||
|
tags: Iterable<any>;
|
||||||
|
uri: string;
|
||||||
|
url: string;
|
||||||
|
visibility: string;
|
||||||
|
|
||||||
|
// Internal fields
|
||||||
|
contentHtml: string;
|
||||||
|
hidden: boolean;
|
||||||
|
search_index: string;
|
||||||
|
spoilerHtml: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { IStatus };
|
Ładowanie…
Reference in New Issue