Don't let status.group be a string

This commit is contained in:
Alex Gleason 2023-06-26 11:50:53 -05:00
parent cb4477185c
commit 98cfb6fae5
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 18 additions and 16 deletions

View file

@ -13,7 +13,7 @@ import {
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
import { normalizeMention } from 'soapbox/normalizers/mention';
import { accountSchema, cardSchema, pollSchema, tombstoneSchema } from 'soapbox/schemas';
import { accountSchema, cardSchema, groupSchema, pollSchema, tombstoneSchema } from 'soapbox/schemas';
import type { Account, Attachment, Card, Emoji, Group, Mention, Poll, EmbeddedEntity } from 'soapbox/types/entities';
@ -55,7 +55,7 @@ export const StatusRecord = ImmutableRecord({
favourited: false,
favourites_count: 0,
filtered: ImmutableList<string>(),
group: null as EmbeddedEntity<Group>,
group: null as Group | null,
in_reply_to_account_id: null as string | null,
in_reply_to_id: null as string | null,
id: '',
@ -252,6 +252,15 @@ const parseAccount = (status: ImmutableMap<string, any>) => {
}
};
const parseGroup = (status: ImmutableMap<string, any>) => {
try {
const group = groupSchema.parse(status.get('group').toJS());
return status.set('group', group);
} catch (_e) {
return status.set('group', null);
}
};
export const normalizeStatus = (status: Record<string, any>) => {
return StatusRecord(
ImmutableMap(fromJS(status)).withMutations(status => {
@ -270,6 +279,7 @@ export const normalizeStatus = (status: Record<string, any>) => {
normalizeDislikes(status);
normalizeTombstone(status);
parseAccount(status);
parseGroup(status);
}),
);
};

View file

@ -59,7 +59,6 @@ export interface ReducerStatus extends StatusRecord {
reblog: string | null
poll: string | null
quote: string | null
group: string | null
}
const minifyStatus = (status: StatusRecord): ReducerStatus => {
@ -67,7 +66,6 @@ const minifyStatus = (status: StatusRecord): ReducerStatus => {
reblog: normalizeId(status.getIn(['reblog', 'id'])),
poll: normalizeId(status.getIn(['poll', 'id'])),
quote: normalizeId(status.getIn(['quote', 'id'])),
group: normalizeId(status.getIn(['group', 'id'])),
}) as ReducerStatus;
};

View file

@ -7,7 +7,6 @@ import {
import { createSelector } from 'reselect';
import { getSettings } from 'soapbox/actions/settings';
import { Entities } from 'soapbox/entity-store/entities';
import { getDomain } from 'soapbox/utils/accounts';
import { validId } from 'soapbox/utils/auth';
import ConfigDB from 'soapbox/utils/config-db';
@ -17,7 +16,7 @@ import { shouldFilter } from 'soapbox/utils/timelines';
import type { ContextType } from 'soapbox/normalizers/filter';
import type { ReducerChat } from 'soapbox/reducers/chats';
import type { RootState } from 'soapbox/store';
import type { Filter as FilterEntity, Notification, Status, Group } from 'soapbox/types/entities';
import type { Filter as FilterEntity, Notification, Status } from 'soapbox/types/entities';
const normalizeId = (id: any): string => typeof id === 'string' ? id : '';
@ -113,31 +112,26 @@ export const makeGetStatus = () => {
[
(state: RootState, { id }: APIStatus) => state.statuses.get(id) as Status | undefined,
(state: RootState, { id }: APIStatus) => state.statuses.get(state.statuses.get(id)?.reblog || '') as Status | undefined,
(state: RootState, { id }: APIStatus) => state.entities[Entities.GROUPS]?.store[state.statuses.get(id)?.group || ''] as Group | undefined,
(_state: RootState, { username }: APIStatus) => username,
getFilters,
(state: RootState) => state.me,
(state: RootState) => getFeatures(state.instance),
],
(statusBase, statusReblog, group, username, filters, me, features) => {
(statusBase, statusReblog, username, filters, me, features) => {
if (!statusBase) return null;
const accountBase = statusBase.account;
const { account } = statusBase;
const accountUsername = account.acct;
const accountUsername = accountBase.acct;
//Must be owner of status if username exists
// Must be owner of status if username exists.
if (accountUsername !== username && username !== undefined) {
return null;
}
return statusBase.withMutations((map: Status) => {
map.set('reblog', statusReblog || null);
// @ts-ignore :(
map.set('account', accountBase || null);
// @ts-ignore
map.set('group', group || null);
if ((features.filters) && accountBase.id !== me) {
if ((features.filters) && account.id !== me) {
const filtered = checkFiltered(statusReblog?.search_index || statusBase.search_index, filters);
map.set('filtered', filtered);