Add accounts_meta reducer to support storing source data for multiple accounts

This commit is contained in:
Alex Gleason 2021-08-23 17:51:32 -05:00
parent d2a6a90757
commit 56462d8a9a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
9 changed files with 85 additions and 47 deletions

View file

@ -55,8 +55,10 @@ export const changeAliasesSuggestions = value => ({
export const addToAliases = (intl, apId) => (dispatch, getState) => { export const addToAliases = (intl, apId) => (dispatch, getState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const state = getState();
const alsoKnownAs = getState().getIn(['meta', 'pleroma', 'also_known_as']); const me = state.get('me');
const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']);
dispatch(addToAliasesRequest(apId)); dispatch(addToAliasesRequest(apId));
@ -92,8 +94,10 @@ export const addToAliasesFail = (apId, error) => ({
export const removeFromAliases = (intl, apId) => (dispatch, getState) => { export const removeFromAliases = (intl, apId) => (dispatch, getState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const state = getState();
const alsoKnownAs = getState().getIn(['meta', 'pleroma', 'also_known_as']); const me = state.get('me');
const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']);
dispatch(removeFromAliasesRequest(apId)); dispatch(removeFromAliasesRequest(apId));

View file

@ -18,14 +18,17 @@ const makeMapStateToProps = () => {
const getAccount = makeGetAccount(); const getAccount = makeGetAccount();
const mapStateToProps = (state, { accountId, added }) => { const mapStateToProps = (state, { accountId, added }) => {
const me = state.get('me');
const ownAccount = getAccount(state, me);
const account = getAccount(state, accountId); const account = getAccount(state, accountId);
const apId = account.getIn(['pleroma', 'ap_id']); const apId = account.getIn(['pleroma', 'ap_id']);
return { return {
account, account,
apId, apId,
added: typeof added === 'undefined' ? state.getIn(['meta', 'pleroma', 'also_known_as']).includes(apId) : added, added: typeof added === 'undefined' ? ownAccount.getIn(['pleroma', 'also_known_as']).includes(apId) : added,
me: state.get('me'), me,
}; };
}; };

View file

@ -9,6 +9,7 @@ import Icon from 'soapbox/components/icon';
import Search from './components/search'; import Search from './components/search';
import Account from './components/account'; import Account from './components/account';
import { removeFromAliases } from '../../actions/aliases'; import { removeFromAliases } from '../../actions/aliases';
import { makeGetAccount } from 'soapbox/selectors';
const messages = defineMessages({ const messages = defineMessages({
heading: { id: 'column.aliases', defaultMessage: 'Account aliases' }, heading: { id: 'column.aliases', defaultMessage: 'Account aliases' },
@ -19,13 +20,24 @@ const messages = defineMessages({
delete: { id: 'column.aliases.delete', defaultMessage: 'Delete' }, delete: { id: 'column.aliases.delete', defaultMessage: 'Delete' },
}); });
const mapStateToProps = state => ({ const makeMapStateToProps = () => {
aliases: state.getIn(['meta', 'pleroma', 'also_known_as']), const getAccount = makeGetAccount();
searchAccountIds: state.getIn(['aliases', 'suggestions', 'items']),
loaded: state.getIn(['aliases', 'suggestions', 'loaded']),
});
export default @connect(mapStateToProps) const mapStateToProps = state => {
const me = state.get('me');
const account = getAccount(state, me);
return {
aliases: account.getIn(['pleroma', 'also_known_as']),
searchAccountIds: state.getIn(['aliases', 'suggestions', 'items']),
loaded: state.getIn(['aliases', 'suggestions', 'loaded']),
};
};
return mapStateToProps;
};
export default @connect(makeMapStateToProps)
@injectIntl @injectIntl
class Aliases extends ImmutablePureComponent { class Aliases extends ImmutablePureComponent {
@ -78,4 +90,4 @@ class Aliases extends ImmutablePureComponent {
); );
} }
} }

View file

@ -26,6 +26,7 @@ import { unescape } from 'lodash';
import { isVerified } from 'soapbox/utils/accounts'; import { isVerified } from 'soapbox/utils/accounts';
import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { getFeatures } from 'soapbox/utils/features'; import { getFeatures } from 'soapbox/utils/features';
import { makeGetAccount } from 'soapbox/selectors';
const hidesNetwork = account => { const hidesNetwork = account => {
const pleroma = account.get('pleroma'); const pleroma = account.get('pleroma');
@ -45,22 +46,23 @@ const messages = defineMessages({
displayNamePlaceholder: { id: 'edit_profile.fields.display_name_placeholder', defaultMessage: 'Name' }, displayNamePlaceholder: { id: 'edit_profile.fields.display_name_placeholder', defaultMessage: 'Name' },
}); });
const mapStateToProps = state => { const makeMapStateToProps = () => {
const me = state.get('me'); const getAccount = makeGetAccount();
const account = state.getIn(['accounts', me]);
const soapbox = getSoapboxConfig(state);
const baseProfile = ImmutableMap({ const mapStateToProps = state => {
pleroma: state.getIn(['meta', 'pleroma', me]), const me = state.get('me');
source: state.getIn(['meta', 'source', me]), const account = getAccount(state, me);
}); const soapbox = getSoapboxConfig(state);
return { return {
account: baseProfile.merge(account), account,
maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4), maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4),
verifiedCanEditName: soapbox.get('verifiedCanEditName'), verifiedCanEditName: soapbox.get('verifiedCanEditName'),
supportsEmailList: getFeatures(state.get('instance')).emailList, supportsEmailList: getFeatures(state.get('instance')).emailList,
};
}; };
return mapStateToProps;
}; };
// Forces fields to be maxFields size, filling empty values // Forces fields to be maxFields size, filling empty values
@ -77,7 +79,7 @@ const unescapeParams = (map, params) => (
), map) ), map)
); );
export default @connect(mapStateToProps) export default @connect(makeMapStateToProps)
@injectIntl @injectIntl
class EditProfile extends ImmutablePureComponent { class EditProfile extends ImmutablePureComponent {

View file

@ -46,6 +46,7 @@ const normalizeAccount = (state, account) => {
'followers_count', 'followers_count',
'following_count', 'following_count',
'statuses_count', 'statuses_count',
'source',
]); ]);
return state.set(account.id, normalized); return state.set(account.id, normalized);

View file

@ -0,0 +1,28 @@
/**
* Accounts Meta: private user data only the owner should see.
* @module soapbox/reducers/accounts_meta
*/
import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me';
import { Map as ImmutableMap, fromJS } from 'immutable';
const initialState = ImmutableMap();
const importAccount = (state, account) => {
const accountId = account.get('id');
return state.set(accountId, ImmutableMap({
pleroma: account.get('pleroma', ImmutableMap()).delete('settings_store'),
source: account.get('source', ImmutableMap()),
}));
};
export default function accounts_meta(state = initialState, action) {
switch(action.type) {
case ME_FETCH_SUCCESS:
case ME_PATCH_SUCCESS:
return importAccount(state, fromJS(action.me));
default:
return state;
}
}

View file

@ -54,6 +54,7 @@ import admin_log from './admin_log';
import security from './security'; import security from './security';
import scheduled_statuses from './scheduled_statuses'; import scheduled_statuses from './scheduled_statuses';
import aliases from './aliases'; import aliases from './aliases';
import accounts_meta from './accounts_meta';
const appReducer = combineReducers({ const appReducer = combineReducers({
dropdown_menu, dropdown_menu,
@ -109,6 +110,7 @@ const appReducer = combineReducers({
security, security,
scheduled_statuses, scheduled_statuses,
aliases, aliases,
accounts_meta,
}); });
// Clear the state (mostly) when the user logs out // Clear the state (mostly) when the user logs out

View file

@ -1,31 +1,12 @@
'use strict'; 'use strict';
import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me';
import { INSTANCE_FETCH_FAIL } from 'soapbox/actions/instance'; import { INSTANCE_FETCH_FAIL } from 'soapbox/actions/instance';
import { Map as ImmutableMap, fromJS } from 'immutable'; import { Map as ImmutableMap } from 'immutable';
const initialState = ImmutableMap(); const initialState = ImmutableMap();
const importAccount = (state, account) => {
return state.withMutations(state => {
const accountId = account.get('id');
if (account.has('pleroma')) {
const pleroPrefs = account.get('pleroma').delete('settings_store');
state.setIn(['pleroma', accountId], pleroPrefs);
}
if (account.has('source')) {
state.setIn(['source', accountId], account.get('source'));
}
});
};
export default function meta(state = initialState, action) { export default function meta(state = initialState, action) {
switch(action.type) { switch(action.type) {
case ME_FETCH_SUCCESS:
case ME_PATCH_SUCCESS:
return importAccount(state, fromJS(action.me));
case INSTANCE_FETCH_FAIL: case INSTANCE_FETCH_FAIL:
return state.set('instance_fetch_failed', true); return state.set('instance_fetch_failed', true);
default: default:

View file

@ -14,6 +14,7 @@ const getAccountBase = (state, id) => state.getIn(['accounts', id], null
const getAccountCounters = (state, id) => state.getIn(['accounts_counters', id], null); const getAccountCounters = (state, id) => state.getIn(['accounts_counters', id], null);
const getAccountRelationship = (state, id) => state.getIn(['relationships', id], null); const getAccountRelationship = (state, id) => state.getIn(['relationships', id], null);
const getAccountMoved = (state, id) => state.getIn(['accounts', state.getIn(['accounts', id, 'moved'])]); const getAccountMoved = (state, id) => state.getIn(['accounts', state.getIn(['accounts', id, 'moved'])]);
const getAccountMeta = (state, id) => state.getIn(['accounts_meta', id], ImmutableMap());
const getAccountAdminData = (state, id) => state.getIn(['admin', 'users', id]); const getAccountAdminData = (state, id) => state.getIn(['admin', 'users', id]);
const getAccountPatron = (state, id) => { const getAccountPatron = (state, id) => {
const url = state.getIn(['accounts', id, 'url']); const url = state.getIn(['accounts', id, 'url']);
@ -26,14 +27,18 @@ export const makeGetAccount = () => {
getAccountCounters, getAccountCounters,
getAccountRelationship, getAccountRelationship,
getAccountMoved, getAccountMoved,
getAccountMeta,
getAccountAdminData, getAccountAdminData,
getAccountPatron, getAccountPatron,
], (base, counters, relationship, moved, admin, patron) => { ], (base, counters, relationship, moved, meta, admin, patron) => {
if (base === null) { if (base === null) {
return null; return null;
} }
return base.merge(counters).withMutations(map => { return base.withMutations(map => {
map.merge(counters);
map.merge(meta);
map.set('pleroma', meta.get('pleroma', ImmutableMap()).merge(base.get('pleroma', ImmutableMap()))); // Lol, thanks Pleroma
map.set('relationship', relationship); map.set('relationship', relationship);
map.set('moved', moved); map.set('moved', moved);
map.set('patron', patron); map.set('patron', patron);