Fix reducer and selector, import accounts into entity store
This commit is contained in:
parent
412fe84d13
commit
4258d4b27f
3 changed files with 33 additions and 23 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { importEntities } from 'soapbox/entity-store/actions';
|
import { importEntities } from 'soapbox/entity-store/actions';
|
||||||
import { Entities } from 'soapbox/entity-store/entities';
|
import { Entities } from 'soapbox/entity-store/entities';
|
||||||
import { Group, groupSchema } from 'soapbox/schemas';
|
import { Group, accountSchema, groupSchema } from 'soapbox/schemas';
|
||||||
import { filteredArray } from 'soapbox/schemas/utils';
|
import { filteredArray } from 'soapbox/schemas/utils';
|
||||||
|
|
||||||
import { getSettings } from '../settings';
|
import { getSettings } from '../settings';
|
||||||
|
@ -17,11 +17,27 @@ const STATUSES_IMPORT = 'STATUSES_IMPORT';
|
||||||
const POLLS_IMPORT = 'POLLS_IMPORT';
|
const POLLS_IMPORT = 'POLLS_IMPORT';
|
||||||
const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP';
|
const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP';
|
||||||
|
|
||||||
const importAccount = (account: APIEntity) =>
|
const importAccount = (data: APIEntity) =>
|
||||||
({ type: ACCOUNT_IMPORT, account });
|
(dispatch: AppDispatch, _getState: () => RootState) => {
|
||||||
|
dispatch({ type: ACCOUNT_IMPORT, account: data });
|
||||||
|
try {
|
||||||
|
const account = accountSchema.parse(data);
|
||||||
|
dispatch(importEntities([account], Entities.ACCOUNTS));
|
||||||
|
} catch (e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const importAccounts = (accounts: APIEntity[]) =>
|
const importAccounts = (data: APIEntity[]) =>
|
||||||
({ type: ACCOUNTS_IMPORT, accounts });
|
(dispatch: AppDispatch, _getState: () => RootState) => {
|
||||||
|
dispatch({ type: ACCOUNTS_IMPORT, accounts: data });
|
||||||
|
try {
|
||||||
|
const accounts = filteredArray(accountSchema).parse(data);
|
||||||
|
dispatch(importEntities(accounts, Entities.ACCOUNTS));
|
||||||
|
} catch (e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const importGroup = (group: Group) =>
|
const importGroup = (group: Group) =>
|
||||||
importEntities([group], Entities.GROUPS);
|
importEntities([group], Entities.GROUPS);
|
||||||
|
|
|
@ -183,12 +183,15 @@ const accountsSelector = createSelector(
|
||||||
(accounts) => immutableizeStore<Account, EntityStore<Account>>(accounts),
|
(accounts) => immutableizeStore<Account, EntityStore<Account>>(accounts),
|
||||||
);
|
);
|
||||||
|
|
||||||
const extendedRootReducer = (state: InferState<typeof appReducer>, action: AnyAction) => {
|
const extendedRootReducer = (
|
||||||
|
state: InferState<typeof appReducer>,
|
||||||
|
action: AnyAction,
|
||||||
|
): ReturnType<typeof rootReducer> & { accounts: ReturnType<typeof accountsSelector> } => {
|
||||||
const extendedState = rootReducer(state, action);
|
const extendedState = rootReducer(state, action);
|
||||||
return {
|
// @ts-ignore
|
||||||
...extendedState,
|
extendedState.accounts = accountsSelector(extendedState);
|
||||||
accounts: accountsSelector(extendedState),
|
// @ts-ignore
|
||||||
};
|
return extendedState;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default extendedRootReducer as Reducer<ReturnType<typeof extendedRootReducer>>;
|
export default extendedRootReducer as Reducer<ReturnType<typeof extendedRootReducer>>;
|
||||||
|
|
|
@ -15,7 +15,6 @@ import { getFeatures } from 'soapbox/utils/features';
|
||||||
import { shouldFilter } from 'soapbox/utils/timelines';
|
import { shouldFilter } from 'soapbox/utils/timelines';
|
||||||
|
|
||||||
import type { ContextType } from 'soapbox/normalizers/filter';
|
import type { ContextType } from 'soapbox/normalizers/filter';
|
||||||
import type { ReducerAccount } from 'soapbox/reducers/accounts';
|
|
||||||
import type { ReducerChat } from 'soapbox/reducers/chats';
|
import type { ReducerChat } from 'soapbox/reducers/chats';
|
||||||
import type { RootState } from 'soapbox/store';
|
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, Group } from 'soapbox/types/entities';
|
||||||
|
@ -168,8 +167,6 @@ export const makeGetStatus = () => {
|
||||||
[
|
[
|
||||||
(state: RootState, { id }: APIStatus) => state.statuses.get(id) as Status | undefined,
|
(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.statuses.get(state.statuses.get(id)?.reblog || '') as Status | undefined,
|
||||||
(state: RootState, { id }: APIStatus) => state.accounts.get(state.statuses.get(id)?.account || '') as ReducerAccount | undefined,
|
|
||||||
(state: RootState, { id }: APIStatus) => state.accounts.get(state.statuses.get(state.statuses.get(id)?.reblog || '')?.account || '') as ReducerAccount | undefined,
|
|
||||||
(state: RootState, { id }: APIStatus) => state.entities[Entities.GROUPS]?.store[state.statuses.get(id)?.group || ''] as Group | undefined,
|
(state: RootState, { id }: APIStatus) => state.entities[Entities.GROUPS]?.store[state.statuses.get(id)?.group || ''] as Group | undefined,
|
||||||
(_state: RootState, { username }: APIStatus) => username,
|
(_state: RootState, { username }: APIStatus) => username,
|
||||||
getFilters,
|
getFilters,
|
||||||
|
@ -177,8 +174,9 @@ export const makeGetStatus = () => {
|
||||||
(state: RootState) => getFeatures(state.instance),
|
(state: RootState) => getFeatures(state.instance),
|
||||||
],
|
],
|
||||||
|
|
||||||
(statusBase, statusReblog, accountBase, accountReblog, group, username, filters, me, features) => {
|
(statusBase, statusReblog, group, username, filters, me, features) => {
|
||||||
if (!statusBase || !accountBase) return null;
|
if (!statusBase) return null;
|
||||||
|
const accountBase = statusBase.account;
|
||||||
|
|
||||||
const accountUsername = accountBase.acct;
|
const accountUsername = accountBase.acct;
|
||||||
//Must be owner of status if username exists
|
//Must be owner of status if username exists
|
||||||
|
@ -186,13 +184,6 @@ export const makeGetStatus = () => {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusReblog && accountReblog) {
|
|
||||||
// @ts-ignore AAHHHHH
|
|
||||||
statusReblog = statusReblog.set('account', accountReblog);
|
|
||||||
} else {
|
|
||||||
statusReblog = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return statusBase.withMutations((map: Status) => {
|
return statusBase.withMutations((map: Status) => {
|
||||||
map.set('reblog', statusReblog || null);
|
map.set('reblog', statusReblog || null);
|
||||||
// @ts-ignore :(
|
// @ts-ignore :(
|
||||||
|
@ -200,7 +191,7 @@ export const makeGetStatus = () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
map.set('group', group || null);
|
map.set('group', group || null);
|
||||||
|
|
||||||
if ((features.filters) && (accountReblog || accountBase).id !== me) {
|
if ((features.filters) && accountBase.id !== me) {
|
||||||
const filtered = checkFiltered(statusReblog?.search_index || statusBase.search_index, filters);
|
const filtered = checkFiltered(statusReblog?.search_index || statusBase.search_index, filters);
|
||||||
|
|
||||||
map.set('filtered', filtered);
|
map.set('filtered', filtered);
|
||||||
|
|
Loading…
Reference in a new issue