Store statuses as StatusRecord

This commit is contained in:
Alex Gleason 2022-03-08 22:02:02 -06:00
parent 2ff3643368
commit 1c36d1b91c
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,10 +1,10 @@
import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
import { accountToMention } from 'soapbox/utils/accounts'; import { accountToMention } from 'soapbox/utils/accounts';
import { mergeDefined } from 'soapbox/utils/normalizers'; import { mergeDefined } from 'soapbox/utils/normalizers';
// Some backends can return null, or omit these required fields const StatusRecord = Record({
const baseStatus = ImmutableMap({ account: ImmutableMap(),
application: null, application: null,
bookmarked: false, bookmarked: false,
card: null, card: null,
@ -14,14 +14,20 @@ const baseStatus = ImmutableMap({
favourites_count: 0, favourites_count: 0,
in_reply_to_account_id: null, in_reply_to_account_id: null,
in_reply_to_id: null, in_reply_to_id: null,
id: '',
language: null, language: null,
media_attachments: ImmutableList(),
mentions: ImmutableList(), mentions: ImmutableList(),
muted: false, muted: false,
pinned: false, pinned: false,
pleroma: ImmutableMap(),
poll: null,
quote: null,
reblog: null, reblog: null,
reblogged: false, reblogged: false,
reblogs_count: 0, reblogs_count: 0,
replies_count: 0, replies_count: 0,
sensitive: false,
spoiler_text: '', spoiler_text: '',
tags: ImmutableList(), tags: ImmutableList(),
uri: '', uri: '',
@ -41,11 +47,6 @@ const basePoll = ImmutableMap({
votes_count: 0, votes_count: 0,
}); });
// Merge base status
const mergeBase = status => {
return status.mergeDeepWith(mergeDefined, baseStatus);
};
// 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 => {
@ -147,13 +148,14 @@ const fixQuote = status => {
}; };
export const normalizeStatus = status => { export const normalizeStatus = status => {
return status.withMutations(status => { return StatusRecord(
mergeBase(status); status.withMutations(status => {
normalizeAttachments(status); normalizeAttachments(status);
normalizeMentions(status); normalizeMentions(status);
normalizePoll(status); normalizePoll(status);
fixMentionsOrder(status); fixMentionsOrder(status);
addSelfMention(status); addSelfMention(status);
fixQuote(status); fixQuote(status);
}); }),
);
}; };