Use new API for account aliases

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
improve-ci
marcin mikołajczak 2022-02-14 21:00:41 +01:00
rodzic 6ceecc08b2
commit 23aa11dfe3
5 zmienionych plików z 147 dodań i 59 usunięć

Wyświetl plik

@ -1,14 +1,19 @@
import { defineMessages } from 'react-intl'; import { defineMessages } from 'react-intl';
import { isLoggedIn } from 'soapbox/utils/auth'; import { isLoggedIn } from 'soapbox/utils/auth';
import { getFeatures } from 'soapbox/utils/features';
import api from '../api'; import api from '../api';
import { showAlertForError } from './alerts'; import { showAlertForError } from './alerts';
import { importFetchedAccount, importFetchedAccounts } from './importer'; import { importFetchedAccounts } from './importer';
import { ME_PATCH_SUCCESS } from './me'; import { patchMeSuccess } from './me';
import snackbar from './snackbar'; import snackbar from './snackbar';
export const ALIASES_FETCH_REQUEST = 'ALIASES_FETCH_REQUEST';
export const ALIASES_FETCH_SUCCESS = 'ALIASES_FETCH_SUCCESS';
export const ALIASES_FETCH_FAIL = 'ALIASES_FETCH_FAIL';
export const ALIASES_SUGGESTIONS_CHANGE = 'ALIASES_SUGGESTIONS_CHANGE'; export const ALIASES_SUGGESTIONS_CHANGE = 'ALIASES_SUGGESTIONS_CHANGE';
export const ALIASES_SUGGESTIONS_READY = 'ALIASES_SUGGESTIONS_READY'; export const ALIASES_SUGGESTIONS_READY = 'ALIASES_SUGGESTIONS_READY';
export const ALIASES_SUGGESTIONS_CLEAR = 'ALIASES_SUGGESTIONS_CLEAR'; export const ALIASES_SUGGESTIONS_CLEAR = 'ALIASES_SUGGESTIONS_CLEAR';
@ -26,6 +31,38 @@ const messages = defineMessages({
removeSuccess: { id: 'aliases.success.remove', defaultMessage: 'Account alias removed successfully' }, removeSuccess: { id: 'aliases.success.remove', defaultMessage: 'Account alias removed successfully' },
}); });
export const fetchAliases = (dispatch, getState) => {
if (!isLoggedIn(getState)) return;
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
if (!features.accountMoving) return;
dispatch(fetchAliasesRequest());
api(getState).get('/api/pleroma/aliases')
.then(response => {
dispatch(fetchAliasesSuccess(response.data.aliases));
})
.catch(err => dispatch(fetchAliasesFail(err)));
};
export const fetchAliasesRequest = () => ({
type: ALIASES_FETCH_REQUEST,
});
export const fetchAliasesSuccess = aliases => ({
type: ALIASES_FETCH_SUCCESS,
value: aliases,
});
export const fetchAliasesFail = error => ({
type: ALIASES_FETCH_FAIL,
error,
});
export const fetchAliasesSuggestions = q => (dispatch, getState) => { export const fetchAliasesSuggestions = q => (dispatch, getState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
@ -56,80 +93,103 @@ export const changeAliasesSuggestions = value => ({
value, value,
}); });
export const addToAliases = (intl, apId) => (dispatch, getState) => { export const addToAliases = (intl, account) => (dispatch, getState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const state = getState(); const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
if (!features.accountMoving) {
const me = state.get('me'); const me = state.get('me');
const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']); const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']);
dispatch(addToAliasesRequest(apId)); dispatch(addToAliasesRequest());
api(getState).patch('/api/v1/accounts/update_credentials', { also_known_as: [...alsoKnownAs, apId] }) api(getState).patch('/api/v1/accounts/update_credentials', { also_known_as: [...alsoKnownAs, account.getIn(['pleroma', 'ap_id'])] })
.then((response => { .then((response => {
dispatch(snackbar.success(intl.formatMessage(messages.createSuccess))); dispatch(snackbar.success(intl.formatMessage(messages.createSuccess)));
dispatch(addToAliasesSuccess(response.data)); dispatch(addToAliasesSuccess());
dispatch(patchMeSuccess(response.data));
})) }))
.catch(err => dispatch(addToAliasesFail(err))); .catch(err => dispatch(addToAliasesFail(err)));
return;
}
dispatch(addToAliasesRequest());
api(getState).put('/api/pleroma/aliases', {
alias: account.get('acct'),
})
.then(response => {
dispatch(snackbar.success(intl.formatMessage(messages.createSuccess)));
dispatch(addToAliasesSuccess);
dispatch(fetchAliases);
})
.catch(err => dispatch(fetchAliasesFail(err)));
}; };
export const addToAliasesRequest = (apId) => ({ export const addToAliasesRequest = () => ({
type: ALIASES_ADD_REQUEST, type: ALIASES_ADD_REQUEST,
apId,
}); });
export const addToAliasesSuccess = me => dispatch => { export const addToAliasesSuccess = () => ({
dispatch(importFetchedAccount(me));
dispatch({
type: ME_PATCH_SUCCESS,
me,
});
dispatch({
type: ALIASES_ADD_SUCCESS, type: ALIASES_ADD_SUCCESS,
}); });
};
export const addToAliasesFail = (apId, error) => ({ export const addToAliasesFail = error => ({
type: ALIASES_ADD_FAIL, type: ALIASES_ADD_FAIL,
apId,
error, error,
}); });
export const removeFromAliases = (intl, apId) => (dispatch, getState) => { export const removeFromAliases = (intl, account) => (dispatch, getState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const state = getState(); const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
if (!features.accountMoving) {
const me = state.get('me'); const me = state.get('me');
const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']); const alsoKnownAs = state.getIn(['accounts_meta', me, 'pleroma', 'also_known_as']);
dispatch(removeFromAliasesRequest(apId)); dispatch(removeFromAliasesRequest());
api(getState).patch('/api/v1/accounts/update_credentials', { also_known_as: alsoKnownAs.filter(id => id !== apId) }) api(getState).patch('/api/v1/accounts/update_credentials', { also_known_as: alsoKnownAs.filter(id => id !== account) })
.then(response => { .then(response => {
dispatch(snackbar.success(intl.formatMessage(messages.removeSuccess))); dispatch(snackbar.success(intl.formatMessage(messages.removeSuccess)));
dispatch(removeFromAliasesSuccess(response.data)); dispatch(removeFromAliasesSuccess);
}) })
.catch(err => dispatch(removeFromAliasesFail(apId, err))); .catch(err => dispatch(removeFromAliasesFail(err)));
return;
}
dispatch(addToAliasesRequest());
api(getState).delete('/api/pleroma/aliases', {
data: {
alias: account,
},
})
.then(response => {
dispatch(snackbar.success(intl.formatMessage(messages.removeSuccess)));
dispatch(removeFromAliasesSuccess);
dispatch(fetchAliases);
})
.catch(err => dispatch(fetchAliasesFail(err)));
}; };
export const removeFromAliasesRequest = (apId) => ({ export const removeFromAliasesRequest = () => ({
type: ALIASES_REMOVE_REQUEST, type: ALIASES_REMOVE_REQUEST,
apId,
}); });
export const removeFromAliasesSuccess = me => dispatch => { export const removeFromAliasesSuccess = () => ({
dispatch(importFetchedAccount(me));
dispatch({
type: ME_PATCH_SUCCESS,
me,
});
dispatch({
type: ALIASES_REMOVE_SUCCESS, type: ALIASES_REMOVE_SUCCESS,
}); });
};
export const removeFromAliasesFail = (apId, error) => ({ export const removeFromAliasesFail = error => ({
type: ALIASES_REMOVE_FAIL, type: ALIASES_REMOVE_FAIL,
apId,
error, error,
}); });

Wyświetl plik

@ -5,11 +5,12 @@ import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages, injectIntl } from 'react-intl'; import { defineMessages, injectIntl } from 'react-intl';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { addToAliases } from '../../../actions/aliases'; import { addToAliases } from 'soapbox/actions/aliases';
import Avatar from '../../../components/avatar'; import Avatar from 'soapbox/components/avatar';
import DisplayName from '../../../components/display_name'; import DisplayName from 'soapbox/components/display_name';
import IconButton from '../../../components/icon_button'; import IconButton from 'soapbox/components/icon_button';
import { makeGetAccount } from '../../../selectors'; import { makeGetAccount } from 'soapbox/selectors';
import { getFeatures } from 'soapbox/utils/features';
const messages = defineMessages({ const messages = defineMessages({
add: { id: 'aliases.account.add', defaultMessage: 'Create alias' }, add: { id: 'aliases.account.add', defaultMessage: 'Create alias' },
@ -18,17 +19,20 @@ const messages = defineMessages({
const makeMapStateToProps = () => { const makeMapStateToProps = () => {
const getAccount = makeGetAccount(); const getAccount = makeGetAccount();
const mapStateToProps = (state, { accountId, added }) => { const mapStateToProps = (state, { accountId, added, aliases }) => {
const me = state.get('me'); const me = state.get('me');
const ownAccount = getAccount(state, me);
const instance = state.get('instance');
const features = getFeatures(instance);
const account = getAccount(state, accountId); const account = getAccount(state, accountId);
const apId = account.getIn(['pleroma', 'ap_id']); const apId = account.getIn(['pleroma', 'ap_id']);
const name = features.accountMoving ? account.get('acct') : apId;
return { return {
account, account,
apId, apId,
added: typeof added === 'undefined' ? ownAccount.getIn(['pleroma', 'also_known_as']).includes(apId) : added, added: typeof added === 'undefined' ? aliases.includes(name) : added,
me, me,
}; };
}; };
@ -56,7 +60,7 @@ class Account extends ImmutablePureComponent {
added: false, added: false,
}; };
handleOnAdd = () => this.props.onAdd(this.props.intl, this.props.apId); handleOnAdd = () => this.props.onAdd(this.props.intl, this.props.account);
render() { render() {
const { account, accountId, intl, added, me } = this.props; const { account, accountId, intl, added, me } = this.props;

Wyświetl plik

@ -1,13 +1,15 @@
import { List as ImmutableList } from 'immutable';
import React from 'react'; import React from 'react';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { fetchAliases, removeFromAliases } from 'soapbox/actions/aliases';
import Icon from 'soapbox/components/icon'; import Icon from 'soapbox/components/icon';
import ScrollableList from 'soapbox/components/scrollable_list';
import { makeGetAccount } from 'soapbox/selectors'; import { makeGetAccount } from 'soapbox/selectors';
import { getFeatures } from 'soapbox/utils/features';
import { removeFromAliases } from '../../actions/aliases';
import ScrollableList from '../../components/scrollable_list';
import Column from '../ui/components/column'; import Column from '../ui/components/column';
import ColumnSubheading from '../ui/components/column_subheading'; import ColumnSubheading from '../ui/components/column_subheading';
@ -30,8 +32,16 @@ const makeMapStateToProps = () => {
const me = state.get('me'); const me = state.get('me');
const account = getAccount(state, me); const account = getAccount(state, me);
const instance = state.get('instance');
const features = getFeatures(instance);
let aliases;
if (features.accountMoving) aliases = state.getIn(['aliases', 'aliases', 'items'], ImmutableList());
else aliases = account.getIn(['pleroma', 'also_known_as']);
return { return {
aliases: account.getIn(['pleroma', 'also_known_as']), aliases,
searchAccountIds: state.getIn(['aliases', 'suggestions', 'items']), searchAccountIds: state.getIn(['aliases', 'suggestions', 'items']),
loaded: state.getIn(['aliases', 'suggestions', 'loaded']), loaded: state.getIn(['aliases', 'suggestions', 'loaded']),
}; };
@ -44,6 +54,11 @@ export default @connect(makeMapStateToProps)
@injectIntl @injectIntl
class Aliases extends ImmutablePureComponent { class Aliases extends ImmutablePureComponent {
componentDidMount = e => {
const { dispatch } = this.props;
dispatch(fetchAliases);
}
handleFilterDelete = e => { handleFilterDelete = e => {
const { dispatch, intl } = this.props; const { dispatch, intl } = this.props;
dispatch(removeFromAliases(intl, e.currentTarget.dataset.value)); dispatch(removeFromAliases(intl, e.currentTarget.dataset.value));
@ -65,7 +80,7 @@ class Aliases extends ImmutablePureComponent {
</div> </div>
) : ( ) : (
<div className='aliases__accounts'> <div className='aliases__accounts'>
{searchAccountIds.map(accountId => <Account key={accountId} accountId={accountId} />)} {searchAccountIds.map(accountId => <Account key={accountId} accountId={accountId} aliases={aliases} />)}
</div> </div>
) )
} }

Wyświetl plik

@ -4,9 +4,14 @@ import {
ALIASES_SUGGESTIONS_READY, ALIASES_SUGGESTIONS_READY,
ALIASES_SUGGESTIONS_CLEAR, ALIASES_SUGGESTIONS_CLEAR,
ALIASES_SUGGESTIONS_CHANGE, ALIASES_SUGGESTIONS_CHANGE,
ALIASES_FETCH_SUCCESS,
} from '../actions/aliases'; } from '../actions/aliases';
const initialState = ImmutableMap({ const initialState = ImmutableMap({
aliases: ImmutableMap({
loaded: false,
items: ImmutableList(),
}),
suggestions: ImmutableMap({ suggestions: ImmutableMap({
value: '', value: '',
loaded: false, loaded: false,
@ -16,6 +21,9 @@ const initialState = ImmutableMap({
export default function aliasesReducer(state = initialState, action) { export default function aliasesReducer(state = initialState, action) {
switch(action.type) { switch(action.type) {
case ALIASES_FETCH_SUCCESS:
return state
.setIn(['aliases', 'items'], action.value);
case ALIASES_SUGGESTIONS_CHANGE: case ALIASES_SUGGESTIONS_CHANGE:
return state return state
.setIn(['suggestions', 'value'], action.value) .setIn(['suggestions', 'value'], action.value)

Wyświetl plik

@ -88,6 +88,7 @@ export const getFeatures = createSelector([instance => instance], instance => {
]), ]),
birthdays: v.software === PLEROMA && gte(v.version, '2.4.50'), birthdays: v.software === PLEROMA && gte(v.version, '2.4.50'),
ethereumLogin: v.software === MITRA, ethereumLogin: v.software === MITRA,
accountMoving: v.software === PLEROMA && gte(v.version, '2.4.50'),
}; };
}); });