WIP TypeScript convertions

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
renovate/emoji-datasource-14.x
marcin mikołajczak 2022-12-26 00:31:07 +01:00
rodzic c692265249
commit 4b3f03353d
21 zmienionych plików z 413 dodań i 318 usunięć

Wyświetl plik

@ -3,6 +3,7 @@ import { Map as ImmutableMap } from 'immutable';
import { __stub } from 'soapbox/api'; import { __stub } from 'soapbox/api';
import { mockStore, rootState } from 'soapbox/jest/test-helpers'; import { mockStore, rootState } from 'soapbox/jest/test-helpers';
import { AuthUserRecord, ReducerRecord } from '../../reducers/auth';
import { import {
fetchMe, patchMe, fetchMe, patchMe,
} from '../me'; } from '../me';
@ -38,10 +39,10 @@ describe('fetchMe()', () => {
beforeEach(() => { beforeEach(() => {
const state = rootState const state = rootState
.set('auth', ImmutableMap({ .set('auth', ReducerRecord({
me: accountUrl, me: accountUrl,
users: ImmutableMap({ users: ImmutableMap({
[accountUrl]: ImmutableMap({ [accountUrl]: AuthUserRecord({
'access_token': token, 'access_token': token,
}), }),
}), }),

Wyświetl plik

@ -77,6 +77,16 @@ const ADMIN_USERS_UNSUGGEST_REQUEST = 'ADMIN_USERS_UNSUGGEST_REQUEST';
const ADMIN_USERS_UNSUGGEST_SUCCESS = 'ADMIN_USERS_UNSUGGEST_SUCCESS'; const ADMIN_USERS_UNSUGGEST_SUCCESS = 'ADMIN_USERS_UNSUGGEST_SUCCESS';
const ADMIN_USERS_UNSUGGEST_FAIL = 'ADMIN_USERS_UNSUGGEST_FAIL'; const ADMIN_USERS_UNSUGGEST_FAIL = 'ADMIN_USERS_UNSUGGEST_FAIL';
const ADMIN_USER_INDEX_EXPAND_FAIL = 'ADMIN_USER_INDEX_EXPAND_FAIL';
const ADMIN_USER_INDEX_EXPAND_REQUEST = 'ADMIN_USER_INDEX_EXPAND_REQUEST';
const ADMIN_USER_INDEX_EXPAND_SUCCESS = 'ADMIN_USER_INDEX_EXPAND_SUCCESS';
const ADMIN_USER_INDEX_FETCH_FAIL = 'ADMIN_USER_INDEX_FETCH_FAIL';
const ADMIN_USER_INDEX_FETCH_REQUEST = 'ADMIN_USER_INDEX_FETCH_REQUEST';
const ADMIN_USER_INDEX_FETCH_SUCCESS = 'ADMIN_USER_INDEX_FETCH_SUCCESS';
const ADMIN_USER_INDEX_QUERY_SET = 'ADMIN_USER_INDEX_QUERY_SET';
const nicknamesFromIds = (getState: () => RootState, ids: string[]) => ids.map(id => getState().accounts.get(id)!.acct); const nicknamesFromIds = (getState: () => RootState, ids: string[]) => ids.map(id => getState().accounts.get(id)!.acct);
const fetchConfig = () => const fetchConfig = () =>
@ -544,6 +554,50 @@ const unsuggestUsers = (accountIds: string[]) =>
}); });
}; };
const setUserIndexQuery = (query: string) => ({ type: ADMIN_USER_INDEX_QUERY_SET, query });
const fetchUserIndex = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
const { filters, page, query, pageSize, isLoading } = getState().admin_user_index;
if (isLoading) return;
dispatch({ type: ADMIN_USER_INDEX_FETCH_REQUEST });
dispatch(fetchUsers(filters.toJS() as string[], page + 1, query, pageSize))
.then((data: any) => {
if (data.error) {
dispatch({ type: ADMIN_USER_INDEX_FETCH_FAIL });
} else {
const { users, count, next } = (data);
dispatch({ type: ADMIN_USER_INDEX_FETCH_SUCCESS, users, count, next });
}
}).catch(() => {
dispatch({ type: ADMIN_USER_INDEX_FETCH_FAIL });
});
};
const expandUserIndex = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
const { filters, page, query, pageSize, isLoading, next, loaded } = getState().admin_user_index;
if (!loaded || isLoading) return;
dispatch({ type: ADMIN_USER_INDEX_EXPAND_REQUEST });
dispatch(fetchUsers(filters.toJS() as string[], page + 1, query, pageSize, next))
.then((data: any) => {
if (data.error) {
dispatch({ type: ADMIN_USER_INDEX_EXPAND_FAIL });
} else {
const { users, count, next } = (data);
dispatch({ type: ADMIN_USER_INDEX_EXPAND_SUCCESS, users, count, next });
}
}).catch(() => {
dispatch({ type: ADMIN_USER_INDEX_EXPAND_FAIL });
});
};
export { export {
ADMIN_CONFIG_FETCH_REQUEST, ADMIN_CONFIG_FETCH_REQUEST,
ADMIN_CONFIG_FETCH_SUCCESS, ADMIN_CONFIG_FETCH_SUCCESS,
@ -596,6 +650,13 @@ export {
ADMIN_USERS_UNSUGGEST_REQUEST, ADMIN_USERS_UNSUGGEST_REQUEST,
ADMIN_USERS_UNSUGGEST_SUCCESS, ADMIN_USERS_UNSUGGEST_SUCCESS,
ADMIN_USERS_UNSUGGEST_FAIL, ADMIN_USERS_UNSUGGEST_FAIL,
ADMIN_USER_INDEX_EXPAND_FAIL,
ADMIN_USER_INDEX_EXPAND_REQUEST,
ADMIN_USER_INDEX_EXPAND_SUCCESS,
ADMIN_USER_INDEX_FETCH_FAIL,
ADMIN_USER_INDEX_FETCH_REQUEST,
ADMIN_USER_INDEX_FETCH_SUCCESS,
ADMIN_USER_INDEX_QUERY_SET,
fetchConfig, fetchConfig,
updateConfig, updateConfig,
updateSoapboxConfig, updateSoapboxConfig,
@ -622,4 +683,7 @@ export {
setRole, setRole,
suggestUsers, suggestUsers,
unsuggestUsers, unsuggestUsers,
setUserIndexQuery,
fetchUserIndex,
expandUserIndex,
}; };

Wyświetl plik

@ -29,7 +29,6 @@ import api, { baseClient } from '../api';
import { importFetchedAccount } from './importer'; import { importFetchedAccount } from './importer';
import type { AxiosError } from 'axios'; import type { AxiosError } from 'axios';
import type { Map as ImmutableMap } from 'immutable';
import type { AppDispatch, RootState } from 'soapbox/store'; import type { AppDispatch, RootState } from 'soapbox/store';
export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT'; export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT';
@ -94,11 +93,11 @@ const createAuthApp = () =>
const createAppToken = () => const createAppToken = () =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const app = getState().auth.get('app'); const app = getState().auth.app;
const params = { const params = {
client_id: app.get('client_id'), client_id: app.client_id,
client_secret: app.get('client_secret'), client_secret: app.client_secret,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'client_credentials', grant_type: 'client_credentials',
scope: getScopes(getState()), scope: getScopes(getState()),
@ -111,11 +110,11 @@ const createAppToken = () =>
const createUserToken = (username: string, password: string) => const createUserToken = (username: string, password: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const app = getState().auth.get('app'); const app = getState().auth.app;
const params = { const params = {
client_id: app.get('client_id'), client_id: app.client_id,
client_secret: app.get('client_secret'), client_secret: app.client_secret,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'password', grant_type: 'password',
username: username, username: username,
@ -127,32 +126,12 @@ const createUserToken = (username: string, password: string) =>
.then((token: Record<string, string | number>) => dispatch(authLoggedIn(token))); .then((token: Record<string, string | number>) => dispatch(authLoggedIn(token)));
}; };
export const refreshUserToken = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
const refreshToken = getState().auth.getIn(['user', 'refresh_token']);
const app = getState().auth.get('app');
if (!refreshToken) return dispatch(noOp);
const params = {
client_id: app.get('client_id'),
client_secret: app.get('client_secret'),
refresh_token: refreshToken,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'refresh_token',
scope: getScopes(getState()),
};
return dispatch(obtainOAuthToken(params))
.then((token: Record<string, string | number>) => dispatch(authLoggedIn(token)));
};
export const otpVerify = (code: string, mfa_token: string) => export const otpVerify = (code: string, mfa_token: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const app = getState().auth.get('app'); const app = getState().auth.app;
return api(getState, 'app').post('/oauth/mfa/challenge', { return api(getState, 'app').post('/oauth/mfa/challenge', {
client_id: app.get('client_id'), client_id: app.client_id,
client_secret: app.get('client_secret'), client_secret: app.client_secret,
mfa_token: mfa_token, mfa_token: mfa_token,
code: code, code: code,
challenge_type: 'totp', challenge_type: 'totp',
@ -233,9 +212,9 @@ export const logOut = () =>
if (!account) return dispatch(noOp); if (!account) return dispatch(noOp);
const params = { const params = {
client_id: state.auth.getIn(['app', 'client_id']), client_id: state.auth.app.client_id,
client_secret: state.auth.getIn(['app', 'client_secret']), client_secret: state.auth.app.client_secret,
token: state.auth.getIn(['users', account.url, 'access_token']), token: state.auth.users.get(account.url)?.access_token!,
}; };
return dispatch(revokeOAuthToken(params)) return dispatch(revokeOAuthToken(params))
@ -263,10 +242,10 @@ export const switchAccount = (accountId: string, background = false) =>
export const fetchOwnAccounts = () => export const fetchOwnAccounts = () =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const state = getState(); const state = getState();
return state.auth.get('users').forEach((user: ImmutableMap<string, string>) => { return state.auth.users.forEach((user) => {
const account = state.accounts.get(user.get('id')); const account = state.accounts.get(user.id);
if (!account) { if (!account) {
dispatch(verifyCredentials(user.get('access_token')!, user.get('url'))); dispatch(verifyCredentials(user.access_token!, user.url));
} }
}); });
}; };

Wyświetl plik

@ -10,12 +10,12 @@ import api from '../api';
const getMeUrl = (state: RootState) => { const getMeUrl = (state: RootState) => {
const me = state.me; const me = state.me;
return state.accounts.getIn([me, 'url']); return state.accounts.get(me)?.url;
}; };
/** Figure out the appropriate instance to fetch depending on the state */ /** Figure out the appropriate instance to fetch depending on the state */
export const getHost = (state: RootState) => { export const getHost = (state: RootState) => {
const accountUrl = getMeUrl(state) || getAuthUserUrl(state); const accountUrl = getMeUrl(state) || getAuthUserUrl(state) as string;
try { try {
return new URL(accountUrl).host; return new URL(accountUrl).host;

Wyświetl plik

@ -30,8 +30,8 @@ const getMeUrl = (state: RootState) => {
const getMeToken = (state: RootState) => { const getMeToken = (state: RootState) => {
// Fallback for upgrading IDs to URLs // Fallback for upgrading IDs to URLs
const accountUrl = getMeUrl(state) || state.auth.get('me'); const accountUrl = getMeUrl(state) || state.auth.me;
return state.auth.getIn(['users', accountUrl, 'access_token']); return state.auth.users.get(accountUrl!)?.access_token;
}; };
const fetchMe = () => const fetchMe = () =>
@ -46,7 +46,7 @@ const fetchMe = () =>
} }
dispatch(fetchMeRequest()); dispatch(fetchMeRequest());
return dispatch(loadCredentials(token, accountUrl)) return dispatch(loadCredentials(token, accountUrl!))
.catch(error => dispatch(fetchMeFail(error))); .catch(error => dispatch(fetchMeFail(error)));
}; };

Wyświetl plik

@ -43,7 +43,7 @@ const maybeParseJSON = (data: string) => {
const getAuthBaseURL = createSelector([ const getAuthBaseURL = createSelector([
(state: RootState, me: string | false | null) => state.accounts.getIn([me, 'url']), (state: RootState, me: string | false | null) => state.accounts.getIn([me, 'url']),
(state: RootState, _me: string | false | null) => state.auth.get('me'), (state: RootState, _me: string | false | null) => state.auth.me,
], (accountUrl, authUserUrl) => { ], (accountUrl, authUserUrl) => {
const baseURL = parseBaseURL(accountUrl) || parseBaseURL(authUserUrl); const baseURL = parseBaseURL(accountUrl) || parseBaseURL(authUserUrl);
return baseURL !== window.location.origin ? baseURL : ''; return baseURL !== window.location.origin ? baseURL : '';

Wyświetl plik

@ -10,7 +10,7 @@ interface IIconButton extends Pick<React.ButtonHTMLAttributes<HTMLButtonElement>
pressed?: boolean pressed?: boolean
size?: number size?: number
src: string src: string
text: React.ReactNode text?: React.ReactNode
} }
const IconButton: React.FC<IIconButton> = ({ const IconButton: React.FC<IIconButton> = ({

Wyświetl plik

@ -1,132 +0,0 @@
import { Set as ImmutableSet, OrderedSet as ImmutableOrderedSet, is } from 'immutable';
import debounce from 'lodash/debounce';
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { injectIntl, defineMessages } from 'react-intl';
import { connect } from 'react-redux';
import { fetchUsers } from 'soapbox/actions/admin';
import ScrollableList from 'soapbox/components/scrollable-list';
import { Column } from 'soapbox/components/ui';
import AccountContainer from 'soapbox/containers/account-container';
import { SimpleForm, TextInput } from 'soapbox/features/forms';
const messages = defineMessages({
heading: { id: 'column.admin.users', defaultMessage: 'Users' },
empty: { id: 'admin.user_index.empty', defaultMessage: 'No users found.' },
searchPlaceholder: { id: 'admin.user_index.search_input_placeholder', defaultMessage: 'Who are you looking for?' },
});
class UserIndex extends ImmutablePureComponent {
static propTypes = {
dispatch: PropTypes.func.isRequired,
};
state = {
isLoading: true,
filters: ImmutableSet(['local', 'active']),
accountIds: ImmutableOrderedSet(),
total: Infinity,
pageSize: 50,
page: 0,
query: '',
nextLink: undefined,
}
clearState = callback => {
this.setState({
isLoading: true,
accountIds: ImmutableOrderedSet(),
page: 0,
}, callback);
}
fetchNextPage = () => {
const { filters, page, query, pageSize, nextLink } = this.state;
const nextPage = page + 1;
this.props.dispatch(fetchUsers(filters, nextPage, query, pageSize, nextLink))
.then(({ users, count, next }) => {
const newIds = users.map(user => user.id);
this.setState({
isLoading: false,
accountIds: this.state.accountIds.union(newIds),
total: count,
page: nextPage,
nextLink: next,
});
})
.catch(() => { });
}
componentDidMount() {
this.fetchNextPage();
}
refresh = () => {
this.clearState(() => {
this.fetchNextPage();
});
}
componentDidUpdate(prevProps, prevState) {
const { filters, query } = this.state;
const filtersChanged = !is(filters, prevState.filters);
const queryChanged = query !== prevState.query;
if (filtersChanged || queryChanged) {
this.refresh();
}
}
handleLoadMore = debounce(() => {
this.fetchNextPage();
}, 2000, { leading: true });
updateQuery = debounce(query => {
this.setState({ query });
}, 900)
handleQueryChange = e => {
this.updateQuery(e.target.value);
};
render() {
const { intl } = this.props;
const { accountIds, isLoading } = this.state;
const hasMore = accountIds.count() < this.state.total && this.state.nextLink !== false;
const showLoading = isLoading && accountIds.isEmpty();
return (
<Column label={intl.formatMessage(messages.heading)}>
<SimpleForm style={{ paddingBottom: 0 }}>
<TextInput
onChange={this.handleQueryChange}
placeholder={intl.formatMessage(messages.searchPlaceholder)}
/>
</SimpleForm>
<ScrollableList
scrollKey='user-index'
hasMore={hasMore}
isLoading={isLoading}
showLoading={showLoading}
onLoadMore={this.handleLoadMore}
emptyMessage={intl.formatMessage(messages.empty)}
className='mt-4'
itemClassName='pb-4'
>
{accountIds.map(id =>
<AccountContainer key={id} id={id} withDate />,
)}
</ScrollableList>
</Column>
);
}
}
export default injectIntl(connect()(UserIndex));

Wyświetl plik

@ -0,0 +1,71 @@
import debounce from 'lodash/debounce';
import React, { useCallback, useEffect } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { expandUserIndex, fetchUserIndex, setUserIndexQuery } from 'soapbox/actions/admin';
import ScrollableList from 'soapbox/components/scrollable-list';
import { Column } from 'soapbox/components/ui';
import AccountContainer from 'soapbox/containers/account-container';
import { SimpleForm, TextInput } from 'soapbox/features/forms';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
const messages = defineMessages({
heading: { id: 'column.admin.users', defaultMessage: 'Users' },
empty: { id: 'admin.user_index.empty', defaultMessage: 'No users found.' },
searchPlaceholder: { id: 'admin.user_index.search_input_placeholder', defaultMessage: 'Who are you looking for?' },
});
const UserIndex: React.FC = () => {
const dispatch = useAppDispatch();
const intl = useIntl();
const { isLoading, items, total, query, next } = useAppSelector((state) => state.admin_user_index);
const handleLoadMore = () => {
dispatch(expandUserIndex());
};
const updateQuery = useCallback(debounce(() => {
dispatch(fetchUserIndex());
}, 900, { leading: true }), []);
const handleQueryChange: React.ChangeEventHandler<HTMLInputElement> = e => {
dispatch(setUserIndexQuery(e.target.value));
};
useEffect(() => {
updateQuery();
}, [query]);
const hasMore = items.count() < total && next !== null;
const showLoading = isLoading && items.isEmpty();
return (
<Column label={intl.formatMessage(messages.heading)}>
<SimpleForm style={{ paddingBottom: 0 }}>
<TextInput
value={query}
onChange={handleQueryChange}
placeholder={intl.formatMessage(messages.searchPlaceholder)}
/>
</SimpleForm>
<ScrollableList
scrollKey='user-index'
hasMore={hasMore}
isLoading={isLoading}
showLoading={showLoading}
onLoadMore={handleLoadMore}
emptyMessage={intl.formatMessage(messages.empty)}
className='mt-4'
itemClassName='pb-4'
>
{items.map(id =>
<AccountContainer key={id} id={id} withDate />,
)}
</ScrollableList>
</Column>
);
};
export default UserIndex;

Wyświetl plik

@ -75,7 +75,7 @@ const AuthTokenList: React.FC = () => {
const intl = useIntl(); const intl = useIntl();
const tokens = useAppSelector(state => state.security.get('tokens').reverse()); const tokens = useAppSelector(state => state.security.get('tokens').reverse());
const currentTokenId = useAppSelector(state => { const currentTokenId = useAppSelector(state => {
const currentToken = state.auth.get('tokens').valueSeq().find((token: ImmutableMap<string, any>) => token.get('me') === state.auth.get('me')); const currentToken = state.auth.tokens.valueSeq().find((token) => token.me === state.auth.me);
return currentToken?.get('id'); return currentToken?.get('id');
}); });

Wyświetl plik

@ -75,7 +75,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
const showSearch = useAppSelector((state) => state.search.submitted && !state.search.hidden); const showSearch = useAppSelector((state) => state.search.submitted && !state.search.hidden);
const isModalOpen = useAppSelector((state) => !!(state.modals.size && state.modals.last()!.modalType === 'COMPOSE')); const isModalOpen = useAppSelector((state) => !!(state.modals.size && state.modals.last()!.modalType === 'COMPOSE'));
const maxTootChars = configuration.getIn(['statuses', 'max_characters']) as number; const maxTootChars = configuration.getIn(['statuses', 'max_characters']) as number;
const scheduledStatusCount = useAppSelector((state) => state.get('scheduled_statuses').size); const scheduledStatusCount = useAppSelector((state) => state.scheduled_statuses.size);
const features = useFeatures(); const features = useFeatures();
const { text, suggestions, spoiler, spoiler_text: spoilerText, privacy, focusDate, caretPosition, is_submitting: isSubmitting, is_changing_upload: isChangingUpload, is_uploading: isUploading, schedule: scheduledAt } = compose; const { text, suggestions, spoiler, spoiler_text: spoilerText, privacy, focusDate, caretPosition, is_submitting: isSubmitting, is_changing_upload: isChangingUpload, is_uploading: isUploading, schedule: scheduledAt } = compose;

Wyświetl plik

@ -37,7 +37,7 @@ const SettingsStore: React.FC = () => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const settings = useSettings(); const settings = useSettings();
const settingsStore = useAppSelector(state => state.get('settings')); const settingsStore = useAppSelector(state => state.settings);
const [rawJSON, setRawJSON] = useState<string>(JSON.stringify(settingsStore, null, 2)); const [rawJSON, setRawJSON] = useState<string>(JSON.stringify(settingsStore, null, 2));
const [jsonValid, setJsonValid] = useState(true); const [jsonValid, setJsonValid] = useState(true);

Wyświetl plik

@ -39,8 +39,8 @@ const ProfileDropdown: React.FC<IProfileDropdown> = ({ account, children }) => {
const features = useFeatures(); const features = useFeatures();
const intl = useIntl(); const intl = useIntl();
const authUsers = useAppSelector((state) => state.auth.get('users')); const authUsers = useAppSelector((state) => state.auth.users);
const otherAccounts = useAppSelector((state) => authUsers.map((authUser: any) => getAccount(state, authUser.get('id')))); const otherAccounts = useAppSelector((state) => authUsers.map((authUser: any) => getAccount(state, authUser.id)!));
const handleLogOut = () => { const handleLogOut = () => {
dispatch(logOut()); dispatch(logOut());

Wyświetl plik

@ -16,9 +16,9 @@ const AdminPage: React.FC = ({ children }) => {
</Layout.Main> </Layout.Main>
<Layout.Aside> <Layout.Aside>
<BundleContainer fetchComponent={LatestAccountsPanel}> {/* <BundleContainer fetchComponent={LatestAccountsPanel}>
{Component => <Component limit={5} />} {Component => <Component limit={5} />}
</BundleContainer> </BundleContainer> */}
<LinkFooter /> <LinkFooter />
</Layout.Aside> </Layout.Aside>

Wyświetl plik

@ -1,4 +1,4 @@
import { Map as ImmutableMap, fromJS } from 'immutable'; import { Map as ImmutableMap, Record as ImmutableRecord, fromJS } from 'immutable';
import { import {
AUTH_APP_CREATED, AUTH_APP_CREATED,
@ -10,12 +10,13 @@ import {
} from 'soapbox/actions/auth'; } from 'soapbox/actions/auth';
import { ME_FETCH_SKIP } from 'soapbox/actions/me'; import { ME_FETCH_SKIP } from 'soapbox/actions/me';
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload'; import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
import { ReducerRecord } from 'soapbox/reducers/auth';
import reducer from '../auth'; import reducer from '../auth';
describe('auth reducer', () => { describe('auth reducer', () => {
it('should return the initial state', () => { it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(ImmutableMap({ expect(reducer(undefined, {} as any)).toEqual(ImmutableMap({
app: ImmutableMap(), app: ImmutableMap(),
users: ImmutableMap(), users: ImmutableMap(),
tokens: ImmutableMap(), tokens: ImmutableMap(),
@ -47,9 +48,9 @@ describe('auth reducer', () => {
}); });
it('should merge the token with existing state', () => { it('should merge the token with existing state', () => {
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } }, tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } },
}); })));
const expected = fromJS({ const expected = fromJS({
'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' }, 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' },
@ -73,12 +74,12 @@ describe('auth reducer', () => {
account: fromJS({ url: 'https://gleasonator.com/users/alex' }), account: fromJS({ url: 'https://gleasonator.com/users/alex' }),
}; };
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
users: { users: {
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
}, },
}); })));
const expected = fromJS({ const expected = fromJS({
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
@ -89,13 +90,13 @@ describe('auth reducer', () => {
}); });
it('sets `me` to the next available user', () => { it('sets `me` to the next available user', () => {
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
me: 'https://gleasonator.com/users/alex', me: 'https://gleasonator.com/users/alex',
users: { users: {
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
}, },
}); })));
const action = { const action = {
type: AUTH_LOGGED_OUT, type: AUTH_LOGGED_OUT,
@ -130,9 +131,9 @@ describe('auth reducer', () => {
account: { id: '1234', url: 'https://gleasonator.com/users/alex' }, account: { id: '1234', url: 'https://gleasonator.com/users/alex' },
}; };
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } }, tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } },
}); })));
const expected = fromJS({ const expected = fromJS({
'ABCDEFG': { 'ABCDEFG': {
@ -165,7 +166,7 @@ describe('auth reducer', () => {
account: { id: '1234', url: 'https://gleasonator.com/users/alex' }, account: { id: '1234', url: 'https://gleasonator.com/users/alex' },
}; };
const state = fromJS({ me: 'https://gleasonator.com/users/benis' }); const state = ReducerRecord(ImmutableMap(fromJS({ me: 'https://gleasonator.com/users/benis' })));
const result = reducer(state, action); const result = reducer(state, action);
expect(result.get('me')).toEqual('https://gleasonator.com/users/benis'); expect(result.get('me')).toEqual('https://gleasonator.com/users/benis');
@ -178,13 +179,13 @@ describe('auth reducer', () => {
account: { id: '1234', url: 'https://gleasonator.com/users/alex' }, account: { id: '1234', url: 'https://gleasonator.com/users/alex' },
}; };
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
users: { users: {
'https://gleasonator.com/users/mk': { id: '4567', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/mk' }, 'https://gleasonator.com/users/mk': { id: '4567', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/mk' },
'https://gleasonator.com/users/curtis': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/curtis' }, 'https://gleasonator.com/users/curtis': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/curtis' },
'https://gleasonator.com/users/benis': { id: '5432', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5432', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
}, },
}); })));
const expected = fromJS({ const expected = fromJS({
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
@ -202,7 +203,7 @@ describe('auth reducer', () => {
account: { id: '1234', url: 'https://gleasonator.com/users/alex' }, account: { id: '1234', url: 'https://gleasonator.com/users/alex' },
}; };
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
me: '1234', me: '1234',
users: { users: {
'1234': { id: '1234', access_token: 'ABCDEFG' }, '1234': { id: '1234', access_token: 'ABCDEFG' },
@ -211,9 +212,9 @@ describe('auth reducer', () => {
tokens: { tokens: {
'ABCDEFG': { access_token: 'ABCDEFG', account: '1234' }, 'ABCDEFG': { access_token: 'ABCDEFG', account: '1234' },
}, },
}); })));
const expected = fromJS({ const expected = ImmutableRecord(fromJS({
me: 'https://gleasonator.com/users/alex', me: 'https://gleasonator.com/users/alex',
users: { users: {
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
@ -222,7 +223,7 @@ describe('auth reducer', () => {
tokens: { tokens: {
'ABCDEFG': { access_token: 'ABCDEFG', account: '1234', me: 'https://gleasonator.com/users/alex' }, 'ABCDEFG': { access_token: 'ABCDEFG', account: '1234', me: 'https://gleasonator.com/users/alex' },
}, },
}); }));
const result = reducer(state, action); const result = reducer(state, action);
expect(result).toEqual(expected); expect(result).toEqual(expected);
@ -231,12 +232,12 @@ describe('auth reducer', () => {
describe('VERIFY_CREDENTIALS_FAIL', () => { describe('VERIFY_CREDENTIALS_FAIL', () => {
it('should delete the failed token if it 403\'d', () => { it('should delete the failed token if it 403\'d', () => {
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
tokens: { tokens: {
'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' }, 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' },
'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' },
}, },
}); })));
const expected = fromJS({ const expected = fromJS({
'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' },
@ -253,12 +254,12 @@ describe('auth reducer', () => {
}); });
it('should delete any users associated with the failed token', () => { it('should delete any users associated with the failed token', () => {
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
users: { users: {
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
}, },
}); })));
const expected = fromJS({ const expected = fromJS({
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
@ -275,13 +276,13 @@ describe('auth reducer', () => {
}); });
it('should reassign `me` to the next in line', () => { it('should reassign `me` to the next in line', () => {
const state = fromJS({ const state = ReducerRecord(ImmutableMap(fromJS({
me: 'https://gleasonator.com/users/alex', me: 'https://gleasonator.com/users/alex',
users: { users: {
'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' }, 'https://gleasonator.com/users/alex': { id: '1234', access_token: 'ABCDEFG', url: 'https://gleasonator.com/users/alex' },
'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' }, 'https://gleasonator.com/users/benis': { id: '5678', access_token: 'HIJKLMN', url: 'https://gleasonator.com/users/benis' },
}, },
}); })));
const action = { const action = {
type: VERIFY_CREDENTIALS_FAIL, type: VERIFY_CREDENTIALS_FAIL,
@ -308,7 +309,7 @@ describe('auth reducer', () => {
describe('ME_FETCH_SKIP', () => { describe('ME_FETCH_SKIP', () => {
it('sets `me` to null', () => { it('sets `me` to null', () => {
const state = fromJS({ me: 'https://gleasonator.com/users/alex' }); const state = ReducerRecord(ImmutableMap(fromJS({ me: 'https://gleasonator.com/users/alex' })));
const action = { type: ME_FETCH_SKIP }; const action = { type: ME_FETCH_SKIP };
const result = reducer(state, action); const result = reducer(state, action);
expect(result.get('me')).toEqual(null); expect(result.get('me')).toEqual(null);

Wyświetl plik

@ -0,0 +1,68 @@
import { Set as ImmutableSet, OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord } from 'immutable';
import {
ADMIN_USER_INDEX_EXPAND_FAIL,
ADMIN_USER_INDEX_EXPAND_REQUEST,
ADMIN_USER_INDEX_EXPAND_SUCCESS,
ADMIN_USER_INDEX_FETCH_FAIL,
ADMIN_USER_INDEX_FETCH_REQUEST,
ADMIN_USER_INDEX_FETCH_SUCCESS,
ADMIN_USER_INDEX_QUERY_SET,
} from 'soapbox/actions/admin';
import type { AnyAction } from 'redux';
import type { APIEntity } from 'soapbox/types/entities';
const ReducerRecord = ImmutableRecord({
isLoading: false,
loaded: false,
items: ImmutableOrderedSet<string>(),
filters: ImmutableSet(['local', 'active']),
total: Infinity,
pageSize: 50,
page: -1,
query: '',
next: null as string | null,
});
type State = ReturnType<typeof ReducerRecord>;
export default function admin_user_index(state: State = ReducerRecord(), action: AnyAction): State {
switch (action.type) {
case ADMIN_USER_INDEX_QUERY_SET:
return state.set('query', action.query);
case ADMIN_USER_INDEX_FETCH_REQUEST:
return state
.set('isLoading', true)
.set('loaded', true)
.set('items', ImmutableOrderedSet())
.set('total', action.count)
.set('page', 0)
.set('next', null);
case ADMIN_USER_INDEX_FETCH_SUCCESS:
return state
.set('isLoading', false)
.set('loaded', true)
.set('items', ImmutableOrderedSet(action.users.map((user: APIEntity) => user.id)))
.set('total', action.count)
.set('page', 1)
.set('next', action.next);
case ADMIN_USER_INDEX_FETCH_FAIL:
case ADMIN_USER_INDEX_EXPAND_FAIL:
return state
.set('isLoading', false);
case ADMIN_USER_INDEX_EXPAND_REQUEST:
return state
.set('isLoading', true);
case ADMIN_USER_INDEX_EXPAND_SUCCESS:
return state
.set('isLoading', false)
.set('loaded', true)
.set('items', state.items.union(action.users.map((user: APIEntity) => user.id)))
.set('total', action.count)
.set('page', 1)
.set('next', action.next);
default:
return state;
}
}

Wyświetl plik

@ -1,8 +1,8 @@
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'; import { List as ImmutableList, Map as ImmutableMap, Record as ImmutableRecord, fromJS } from 'immutable';
import trim from 'lodash/trim'; import trim from 'lodash/trim';
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload'; import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
import { FE_SUBDIRECTORY } from 'soapbox/build-config'; import BuildConfig from 'soapbox/build-config';
import KVStore from 'soapbox/storage/kv-store'; import KVStore from 'soapbox/storage/kv-store';
import { validId, isURL } from 'soapbox/utils/auth'; import { validId, isURL } from 'soapbox/utils/auth';
@ -17,17 +17,54 @@ import {
} from '../actions/auth'; } from '../actions/auth';
import { ME_FETCH_SKIP } from '../actions/me'; import { ME_FETCH_SKIP } from '../actions/me';
const defaultState = ImmutableMap({ import type { AxiosError } from 'axios';
app: ImmutableMap(), import type { AnyAction } from 'redux';
users: ImmutableMap(), import type { APIEntity, Account as AccountEntity } from 'soapbox/types/entities';
tokens: ImmutableMap(),
me: null, export const AuthAppRecord = ImmutableRecord({
access_token: '',
client_id: '',
client_secret: '',
id: '',
name: '',
redirect_uri: '',
vapid_key: '',
website: '',
}); });
const buildKey = parts => parts.join(':'); export const AuthUserRecord = ImmutableRecord({
access_token: '',
id: '',
url: '',
});
export const AuthTokenRecord = ImmutableRecord({
access_token: '',
account: '',
created_at: null as number | null,
expires_in: null as number | null,
id: null as number | null,
me: '',
refresh_token: null as string | null,
scope: '',
token_type: '',
});
export const ReducerRecord = ImmutableRecord({
app: AuthAppRecord(),
tokens: ImmutableMap<string, AuthToken>(),
users: ImmutableMap<string, AuthUser>(),
me: null as string | null,
});
type AuthToken = ReturnType<typeof AuthTokenRecord>;
type AuthUser = ReturnType<typeof AuthUserRecord>;
type State = ReturnType<typeof ReducerRecord>;
const buildKey = (parts: string[]) => parts.join(':');
// For subdirectory support // For subdirectory support
const NAMESPACE = trim(FE_SUBDIRECTORY, '/') ? `soapbox@${FE_SUBDIRECTORY}` : 'soapbox'; const NAMESPACE = trim(BuildConfig.FE_SUBDIRECTORY, '/') ? `soapbox@${BuildConfig.FE_SUBDIRECTORY}` : 'soapbox';
const STORAGE_KEY = buildKey([NAMESPACE, 'auth']); const STORAGE_KEY = buildKey([NAMESPACE, 'auth']);
const SESSION_KEY = buildKey([NAMESPACE, 'auth', 'me']); const SESSION_KEY = buildKey([NAMESPACE, 'auth', 'me']);
@ -38,34 +75,34 @@ const getSessionUser = () => {
}; };
const sessionUser = getSessionUser(); const sessionUser = getSessionUser();
export const localState = fromJS(JSON.parse(localStorage.getItem(STORAGE_KEY))); export const localState = fromJS(JSON.parse(localStorage.getItem(STORAGE_KEY)!));
// Checks if the user has an ID and access token // Checks if the user has an ID and access token
const validUser = user => { const validUser = (user?: AuthUser) => {
try { try {
return validId(user.get('id')) && validId(user.get('access_token')); return !!(user && validId(user.id) && validId(user.access_token));
} catch (e) { } catch (e) {
return false; return false;
} }
}; };
// Finds the first valid user in the state // Finds the first valid user in the state
const firstValidUser = state => state.get('users', ImmutableMap()).find(validUser); const firstValidUser = (state: State) => state.users.find(validUser);
// For legacy purposes. IDs get upgraded to URLs further down. // For legacy purposes. IDs get upgraded to URLs further down.
const getUrlOrId = user => { const getUrlOrId = (user?: AuthUser): string | null => {
try { try {
const { id, url } = user.toJS(); const { id, url } = user!.toJS();
return url || id; return (url || id) as string;
} catch { } catch {
return null; return null;
} }
}; };
// If `me` doesn't match an existing user, attempt to shift it. // If `me` doesn't match an existing user, attempt to shift it.
const maybeShiftMe = state => { const maybeShiftMe = (state: State) => {
const me = state.get('me'); const me = state.me!;
const user = state.getIn(['users', me]); const user = state.users.get(me);
if (!validUser(user)) { if (!validUser(user)) {
const nextUser = firstValidUser(state); const nextUser = firstValidUser(state);
@ -76,29 +113,30 @@ const maybeShiftMe = state => {
}; };
// Set the user from the session or localStorage, whichever is valid first // Set the user from the session or localStorage, whichever is valid first
const setSessionUser = state => state.update('me', null, me => { const setSessionUser = (state: State) => state.update('me', me => {
const user = ImmutableList([ const user = ImmutableList<AuthUser>([
state.getIn(['users', sessionUser]), state.users.get(sessionUser!)!,
state.getIn(['users', me]), state.users.get(me!)!,
]).find(validUser); ]).find(validUser);
return getUrlOrId(user); return getUrlOrId(user);
}); });
// Upgrade the initial state // Upgrade the initial state
const migrateLegacy = state => { const migrateLegacy = (state: State) => {
if (localState) return state; if (localState) return state;
return state.withMutations(state => { return state.withMutations(state => {
const app = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:app'))); console.log(localStorage.getItem('soapbox:auth:app'));
const user = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:user'))); const app = AuthAppRecord(JSON.parse(localStorage.getItem('soapbox:auth:app')!));
const user = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:user')!)) as ImmutableMap<string, any>;
if (!user) return; if (!user) return;
state.set('me', '_legacy'); // Placeholder account ID state.set('me', '_legacy'); // Placeholder account ID
state.set('app', app); state.set('app', app);
state.set('tokens', ImmutableMap({ state.set('tokens', ImmutableMap({
[user.get('access_token')]: user.set('account', '_legacy'), [user.get('access_token')]: AuthTokenRecord(user.set('account', '_legacy')),
})); }));
state.set('users', ImmutableMap({ state.set('users', ImmutableMap({
'_legacy': ImmutableMap({ '_legacy': AuthUserRecord({
id: '_legacy', id: '_legacy',
access_token: user.get('access_token'), access_token: user.get('access_token'),
}), }),
@ -106,26 +144,26 @@ const migrateLegacy = state => {
}); });
}; };
const isUpgradingUrlId = state => { const isUpgradingUrlId = (state: State) => {
const me = state.get('me'); const me = state.me;
const user = state.getIn(['users', me]); const user = state.users.get(me!);
return validId(me) && user && !isURL(me); return validId(me) && user && !isURL(me);
}; };
// Checks the state and makes it valid // Checks the state and makes it valid
const sanitizeState = state => { const sanitizeState = (state: State) => {
// Skip sanitation during ID to URL upgrade // Skip sanitation during ID to URL upgrade
if (isUpgradingUrlId(state)) return state; if (isUpgradingUrlId(state)) return state;
return state.withMutations(state => { return state.withMutations(state => {
// Remove invalid users, ensure ID match // Remove invalid users, ensure ID match
state.update('users', ImmutableMap(), users => ( state.update('users', users => (
users.filter((user, url) => ( users.filter((user, url) => (
validUser(user) && user.get('url') === url validUser(user) && user.get('url') === url
)) ))
)); ));
// Remove mismatched tokens // Remove mismatched tokens
state.update('tokens', ImmutableMap(), tokens => ( state.update('tokens', tokens => (
tokens.filter((token, id) => ( tokens.filter((token, id) => (
validId(id) && token.get('access_token') === id validId(id) && token.get('access_token') === id
)) ))
@ -133,21 +171,22 @@ const sanitizeState = state => {
}); });
}; };
const persistAuth = state => localStorage.setItem(STORAGE_KEY, JSON.stringify(state.toJS())); const persistAuth = (state: State) => localStorage.setItem(STORAGE_KEY, JSON.stringify(state.toJS()));
const persistSession = state => { const persistSession = (state: State) => {
const me = state.get('me'); const me = state.me;
if (me && typeof me === 'string') { if (me && typeof me === 'string') {
sessionStorage.setItem(SESSION_KEY, me); sessionStorage.setItem(SESSION_KEY, me);
} }
}; };
const persistState = state => { const persistState = (state: State) => {
persistAuth(state); persistAuth(state);
persistSession(state); persistSession(state);
}; };
const initialize = state => { const initialize = (state: State) => {
console.log(JSON.stringify(state.toJS()), JSON.stringify(localState?.toJS()));
return state.withMutations(state => { return state.withMutations(state => {
maybeShiftMe(state); maybeShiftMe(state);
setSessionUser(state); setSessionUser(state);
@ -157,17 +196,17 @@ const initialize = state => {
}); });
}; };
const initialState = initialize(defaultState.merge(localState)); const initialState = initialize(ReducerRecord().merge(localState as any));
const importToken = (state, token) => { const importToken = (state: State, token: APIEntity) => {
return state.setIn(['tokens', token.access_token], fromJS(token)); return state.setIn(['tokens', token.access_token], AuthTokenRecord(ImmutableMap(fromJS(token))));
}; };
// Upgrade the `_legacy` placeholder ID with a real account // Upgrade the `_legacy` placeholder ID with a real account
const upgradeLegacyId = (state, account) => { const upgradeLegacyId = (state: State, account: APIEntity) => {
if (localState) return state; if (localState) return state;
return state.withMutations(state => { return state.withMutations(state => {
state.update('me', null, me => me === '_legacy' ? account.url : me); state.update('me', me => me === '_legacy' ? account.url : me);
state.deleteIn(['users', '_legacy']); state.deleteIn(['users', '_legacy']);
}); });
// TODO: Delete `soapbox:auth:app` and `soapbox:auth:user` localStorage? // TODO: Delete `soapbox:auth:app` and `soapbox:auth:user` localStorage?
@ -176,19 +215,19 @@ const upgradeLegacyId = (state, account) => {
// Users are now stored by their ActivityPub ID instead of their // Users are now stored by their ActivityPub ID instead of their
// primary key to support auth against multiple hosts. // primary key to support auth against multiple hosts.
const upgradeNonUrlId = (state, account) => { const upgradeNonUrlId = (state: State, account: APIEntity) => {
const me = state.get('me'); const me = state.me;
if (isURL(me)) return state; if (isURL(me)) return state;
return state.withMutations(state => { return state.withMutations(state => {
state.update('me', null, me => me === account.id ? account.url : me); state.update('me', me => me === account.id ? account.url : me);
state.deleteIn(['users', account.id]); state.deleteIn(['users', account.id]);
}); });
}; };
// Returns a predicate function for filtering a mismatched user/token // Returns a predicate function for filtering a mismatched user/token
const userMismatch = (token, account) => { const userMismatch = (token: string, account: APIEntity) => {
return (user, url) => { return (user: AuthUser, url: string) => {
const sameToken = user.get('access_token') === token; const sameToken = user.get('access_token') === token;
const differentUrl = url !== account.url || user.get('url') !== account.url; const differentUrl = url !== account.url || user.get('url') !== account.url;
const differentId = user.get('id') !== account.id; const differentId = user.get('id') !== account.id;
@ -197,7 +236,7 @@ const userMismatch = (token, account) => {
}; };
}; };
const importCredentials = (state, token, account) => { const importCredentials = (state: State, token: string, account: APIEntity) => {
return state.withMutations(state => { return state.withMutations(state => {
state.setIn(['users', account.url], ImmutableMap({ state.setIn(['users', account.url], ImmutableMap({
id: account.id, id: account.id,
@ -206,62 +245,62 @@ const importCredentials = (state, token, account) => {
})); }));
state.setIn(['tokens', token, 'account'], account.id); state.setIn(['tokens', token, 'account'], account.id);
state.setIn(['tokens', token, 'me'], account.url); state.setIn(['tokens', token, 'me'], account.url);
state.update('users', ImmutableMap(), users => users.filterNot(userMismatch(token, account))); state.update('users', users => users.filterNot(userMismatch(token, account)));
state.update('me', null, me => me || account.url); state.update('me', me => me || account.url);
upgradeLegacyId(state, account); upgradeLegacyId(state, account);
upgradeNonUrlId(state, account); upgradeNonUrlId(state, account);
}); });
}; };
const deleteToken = (state, token) => { const deleteToken = (state: State, token: string) => {
return state.withMutations(state => { return state.withMutations(state => {
state.update('tokens', ImmutableMap(), tokens => tokens.delete(token)); state.update('tokens', tokens => tokens.delete(token));
state.update('users', ImmutableMap(), users => users.filterNot(user => user.get('access_token') === token)); state.update('users', users => users.filterNot(user => user.get('access_token') === token));
maybeShiftMe(state); maybeShiftMe(state);
}); });
}; };
const deleteUser = (state, account) => { const deleteUser = (state: State, account: AccountEntity) => {
const accountUrl = account.get('url'); const accountUrl = account.url;
return state.withMutations(state => { return state.withMutations(state => {
state.update('users', ImmutableMap(), users => users.delete(accountUrl)); state.update('users', users => users.delete(accountUrl));
state.update('tokens', ImmutableMap(), tokens => tokens.filterNot(token => token.get('me') === accountUrl)); state.update('tokens', tokens => tokens.filterNot(token => token.get('me') === accountUrl));
maybeShiftMe(state); maybeShiftMe(state);
}); });
}; };
const importMastodonPreload = (state, data) => { const importMastodonPreload = (state: State, data: ImmutableMap<string, any>) => {
return state.withMutations(state => { return state.withMutations(state => {
const accountId = data.getIn(['meta', 'me']); const accountId = data.getIn(['meta', 'me']);
const accountUrl = data.getIn(['accounts', accountId, 'url']); const accountUrl = data.getIn(['accounts', accountId, 'url']) as string;
const accessToken = data.getIn(['meta', 'access_token']); const accessToken = data.getIn(['meta', 'access_token']);
if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) { if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) {
state.setIn(['tokens', accessToken], fromJS({ state.setIn(['tokens', accessToken], AuthTokenRecord(ImmutableMap(fromJS({
access_token: accessToken, access_token: accessToken,
account: accountId, account: accountId,
me: accountUrl, me: accountUrl,
scope: 'read write follow push', scope: 'read write follow push',
token_type: 'Bearer', token_type: 'Bearer',
})); }))));
state.setIn(['users', accountUrl], fromJS({ state.setIn(['users', accountUrl], AuthUserRecord(ImmutableMap(fromJS({
id: accountId, id: accountId,
access_token: accessToken, access_token: accessToken,
url: accountUrl, url: accountUrl,
})); }))));
} }
maybeShiftMe(state); maybeShiftMe(state);
}); });
}; };
const persistAuthAccount = account => { const persistAuthAccount = (account: APIEntity) => {
if (account && account.url) { if (account && account.url) {
const key = `authAccount:${account.url}`; const key = `authAccount:${account.url}`;
if (!account.pleroma) account.pleroma = {}; if (!account.pleroma) account.pleroma = {};
KVStore.getItem(key).then(oldAccount => { KVStore.getItem(key).then((oldAccount: any) => {
const settings = oldAccount?.pleroma?.settings_store || {}; const settings = oldAccount?.pleroma?.settings_store || {};
if (!account.pleroma.settings_store) { if (!account.pleroma.settings_store) {
account.pleroma.settings_store = settings; account.pleroma.settings_store = settings;
@ -272,20 +311,22 @@ const persistAuthAccount = account => {
} }
}; };
const deleteForbiddenToken = (state, error, token) => { const deleteForbiddenToken = (state: State, error: AxiosError, token: string) => {
if ([401, 403].includes(error.response?.status)) { if ([401, 403].includes(error.response?.status!)) {
return deleteToken(state, token); return deleteToken(state, token);
} else { } else {
return state; return state;
} }
}; };
const reducer = (state, action) => { const reducer = (state: State, action: AnyAction) => {
switch (action.type) { switch (action.type) {
case AUTH_APP_CREATED: case AUTH_APP_CREATED:
return state.set('app', fromJS(action.app)); console.log(action.app, AuthAppRecord(action.app));
return state.set('app', AuthAppRecord(action.app));
case AUTH_APP_AUTHORIZED: case AUTH_APP_AUTHORIZED:
return state.update('app', ImmutableMap(), app => app.merge(fromJS(action.token))); console.log(state.app, state.app.merge(action.token));
return state.update('app', app => app.merge(action.token));
case AUTH_LOGGED_IN: case AUTH_LOGGED_IN:
return importToken(state, action.token); return importToken(state, action.token);
case AUTH_LOGGED_OUT: case AUTH_LOGGED_OUT:
@ -300,7 +341,7 @@ const reducer = (state, action) => {
case ME_FETCH_SKIP: case ME_FETCH_SKIP:
return state.set('me', null); return state.set('me', null);
case MASTODON_PRELOAD_IMPORT: case MASTODON_PRELOAD_IMPORT:
return importMastodonPreload(state, fromJS(action.data)); return importMastodonPreload(state, fromJS(action.data) as ImmutableMap<string, any>);
default: default:
return state; return state;
} }
@ -309,33 +350,33 @@ const reducer = (state, action) => {
const reload = () => location.replace('/'); const reload = () => location.replace('/');
// `me` is a user ID string // `me` is a user ID string
const validMe = state => { const validMe = (state: State) => {
const me = state.get('me'); const me = state.me;
return typeof me === 'string' && me !== '_legacy'; return typeof me === 'string' && me !== '_legacy';
}; };
// `me` has changed from one valid ID to another // `me` has changed from one valid ID to another
const userSwitched = (oldState, state) => { const userSwitched = (oldState: State, state: State) => {
const me = state.get('me'); const me = state.me;
const oldMe = oldState.get('me'); const oldMe = oldState.me;
const stillValid = validMe(oldState) && validMe(state); const stillValid = validMe(oldState) && validMe(state);
const didChange = oldMe !== me; const didChange = oldMe !== me;
const userUpgradedUrl = state.getIn(['users', me, 'id']) === oldMe; const userUpgradedUrl = state.users.get(me!)?.id === oldMe;
return stillValid && didChange && !userUpgradedUrl; return stillValid && didChange && !userUpgradedUrl;
}; };
const maybeReload = (oldState, state, action) => { const maybeReload = (oldState: State, state: State, action: AnyAction) => {
const loggedOutStandalone = action.type === AUTH_LOGGED_OUT && action.standalone; const loggedOutStandalone = action.type === AUTH_LOGGED_OUT && action.standalone;
const switched = userSwitched(oldState, state); const switched = userSwitched(oldState, state);
if (switched || loggedOutStandalone) { if (switched || loggedOutStandalone) {
reload(state); reload();
} }
}; };
export default function auth(oldState = initialState, action) { export default function auth(oldState: State = initialState, action: AnyAction) {
const state = reducer(oldState, action); const state = reducer(oldState, action);
if (!state.equals(oldState)) { if (!state.equals(oldState)) {

Wyświetl plik

@ -10,6 +10,7 @@ import accounts_counters from './accounts-counters';
import accounts_meta from './accounts-meta'; import accounts_meta from './accounts-meta';
import admin from './admin'; import admin from './admin';
import admin_log from './admin-log'; import admin_log from './admin-log';
import admin_user_index from './admin-user-index';
import aliases from './aliases'; import aliases from './aliases';
import announcements from './announcements'; import announcements from './announcements';
import auth from './auth'; import auth from './auth';
@ -118,6 +119,7 @@ const reducers = {
history, history,
announcements, announcements,
compose_event, compose_event,
admin_user_index,
}; };
// Build a default state from all reducers: it has the key and `undefined` // Build a default state from all reducers: it has the key and `undefined`

Wyświetl plik

@ -1,4 +1,4 @@
import { Map as ImmutableMap, fromJS } from 'immutable'; import { List as ImmutableList, Map as ImmutableMap, fromJS } from 'immutable';
import { PLEROMA_PRELOAD_IMPORT } from 'soapbox/actions/preload'; import { PLEROMA_PRELOAD_IMPORT } from 'soapbox/actions/preload';
import KVStore from 'soapbox/storage/kv-store'; import KVStore from 'soapbox/storage/kv-store';
@ -11,24 +11,24 @@ import {
SOAPBOX_CONFIG_REQUEST_FAIL, SOAPBOX_CONFIG_REQUEST_FAIL,
} from '../actions/soapbox'; } from '../actions/soapbox';
const initialState = ImmutableMap(); const initialState = ImmutableMap<string, any>();
const fallbackState = ImmutableMap({ const fallbackState = ImmutableMap<string, any>({
brandColor: '#0482d8', // Azure brandColor: '#0482d8', // Azure
}); });
const updateFromAdmin = (state, configs) => { const updateFromAdmin = (state: ImmutableMap<string, any>, configs: ImmutableList<ImmutableMap<string, any>>) => {
try { try {
return ConfigDB.find(configs, ':pleroma', ':frontend_configurations') return ConfigDB.find(configs, ':pleroma', ':frontend_configurations')!
.get('value') .get('value')
.find(value => value.getIn(['tuple', 0]) === ':soapbox_fe') .find((value: ImmutableMap<string, any>) => value.getIn(['tuple', 0]) === ':soapbox_fe')
.getIn(['tuple', 1]); .getIn(['tuple', 1]);
} catch { } catch {
return state; return state;
} }
}; };
const preloadImport = (state, action) => { const preloadImport = (state: ImmutableMap<string, any>, action: Record<string, any>) => {
const path = '/api/pleroma/frontend_configurations'; const path = '/api/pleroma/frontend_configurations';
const feData = action.data[path]; const feData = action.data[path];
@ -40,29 +40,29 @@ const preloadImport = (state, action) => {
} }
}; };
const persistSoapboxConfig = (soapboxConfig, host) => { const persistSoapboxConfig = (soapboxConfig: ImmutableMap<string, any>, host: string) => {
if (host) { if (host) {
KVStore.setItem(`soapbox_config:${host}`, soapboxConfig.toJS()).catch(console.error); KVStore.setItem(`soapbox_config:${host}`, soapboxConfig.toJS()).catch(console.error);
} }
}; };
const importSoapboxConfig = (state, soapboxConfig, host) => { const importSoapboxConfig = (state: ImmutableMap<string, any>, soapboxConfig: ImmutableMap<string, any>, host: string) => {
persistSoapboxConfig(soapboxConfig, host); persistSoapboxConfig(soapboxConfig, host);
return soapboxConfig; return soapboxConfig;
}; };
export default function soapbox(state = initialState, action) { export default function soapbox(state = initialState, action: Record<string, any>) {
switch (action.type) { switch (action.type) {
case PLEROMA_PRELOAD_IMPORT: case PLEROMA_PRELOAD_IMPORT:
return preloadImport(state, action); return preloadImport(state, action);
case SOAPBOX_CONFIG_REMEMBER_SUCCESS: case SOAPBOX_CONFIG_REMEMBER_SUCCESS:
return fromJS(action.soapboxConfig); return fromJS(action.soapboxConfig);
case SOAPBOX_CONFIG_REQUEST_SUCCESS: case SOAPBOX_CONFIG_REQUEST_SUCCESS:
return importSoapboxConfig(state, fromJS(action.soapboxConfig), action.host); return importSoapboxConfig(state, fromJS(action.soapboxConfig) as ImmutableMap<string, any>, action.host);
case SOAPBOX_CONFIG_REQUEST_FAIL: case SOAPBOX_CONFIG_REQUEST_FAIL:
return fallbackState.mergeDeep(state); return fallbackState.mergeDeep(state);
case ADMIN_CONFIG_UPDATE_SUCCESS: case ADMIN_CONFIG_UPDATE_SUCCESS:
return updateFromAdmin(state, fromJS(action.configs)); return updateFromAdmin(state, fromJS(action.configs) as ImmutableList<ImmutableMap<string, any>>);
default: default:
return state; return state;
} }

Wyświetl plik

@ -269,16 +269,16 @@ export const makeGetReport = () => {
}; };
const getAuthUserIds = createSelector([ const getAuthUserIds = createSelector([
(state: RootState) => state.auth.get('users', ImmutableMap()), (state: RootState) => state.auth.users,
], authUsers => { ], authUsers => {
return authUsers.reduce((ids: ImmutableOrderedSet<string>, authUser: ImmutableMap<string, any>) => { return authUsers.reduce((ids: ImmutableOrderedSet<string>, authUser) => {
try { try {
const id = authUser.get('id'); const id = authUser.get('id');
return validId(id) ? ids.add(id) : ids; return validId(id) ? ids.add(id) : ids;
} catch { } catch {
return ids; return ids;
} }
}, ImmutableOrderedSet()); }, ImmutableOrderedSet<string>());
}); });
export const makeGetOtherAccounts = () => { export const makeGetOtherAccounts = () => {

Wyświetl plik

@ -4,7 +4,8 @@ import type { RootState } from 'soapbox/store';
export const validId = (id: any) => typeof id === 'string' && id !== 'null' && id !== 'undefined'; export const validId = (id: any) => typeof id === 'string' && id !== 'null' && id !== 'undefined';
export const isURL = (url: string) => { export const isURL = (url?: string | null) => {
if (typeof url !== 'string') return false;
try { try {
new URL(url); new URL(url);
return true; return true;
@ -30,11 +31,11 @@ export const isLoggedIn = (getState: () => RootState) => {
return validId(getState().me); return validId(getState().me);
}; };
export const getAppToken = (state: RootState) => state.auth.getIn(['app', 'access_token']) as string; export const getAppToken = (state: RootState) => state.auth.app.access_token as string;
export const getUserToken = (state: RootState, accountId?: string | false | null) => { export const getUserToken = (state: RootState, accountId?: string | false | null) => {
const accountUrl = state.accounts.getIn([accountId, 'url']); const accountUrl = state.accounts.getIn([accountId, 'url']) as string;
return state.auth.getIn(['users', accountUrl, 'access_token']) as string; return state.auth.users.get(accountUrl)?.access_token as string;
}; };
export const getAccessToken = (state: RootState) => { export const getAccessToken = (state: RootState) => {
@ -43,24 +44,23 @@ export const getAccessToken = (state: RootState) => {
}; };
export const getAuthUserId = (state: RootState) => { export const getAuthUserId = (state: RootState) => {
const me = state.auth.get('me'); const me = state.auth.me;
return ImmutableList([ return ImmutableList([
state.auth.getIn(['users', me, 'id']), state.auth.users.get(me!)?.id,
me, me,
]).find(validId); ]).find(validId);
}; };
export const getAuthUserUrl = (state: RootState) => { export const getAuthUserUrl = (state: RootState) => {
const me = state.auth.get('me'); const me = state.auth.me;
return ImmutableList([ return ImmutableList([
state.auth.getIn(['users', me, 'url']), state.auth.users.get(me!)?.url,
me, me,
]).find(isURL); ].filter(url => url)).find(isURL);
}; };
/** Get the VAPID public key. */ /** Get the VAPID public key. */
export const getVapidKey = (state: RootState) => { export const getVapidKey = (state: RootState) =>
return state.auth.getIn(['app', 'vapid_key']) || state.instance.getIn(['pleroma', 'vapid_public_key']); (state.auth.app.vapid_key || state.instance.pleroma.get('vapid_public_key')) as string;
};