bigbuffet-rw/app/soapbox/actions/suggestions.js

82 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-03-25 10:25:45 -07:00
import { isLoggedIn } from 'soapbox/utils/auth';
2021-09-17 13:38:38 -07:00
import { getFeatures } from 'soapbox/utils/features';
2022-01-10 14:01:24 -08:00
import api from '../api';
import { fetchRelationships } from './accounts';
import { importFetchedAccounts } from './importer';
2020-03-27 13:59:38 -07:00
export const SUGGESTIONS_FETCH_REQUEST = 'SUGGESTIONS_FETCH_REQUEST';
export const SUGGESTIONS_FETCH_SUCCESS = 'SUGGESTIONS_FETCH_SUCCESS';
export const SUGGESTIONS_FETCH_FAIL = 'SUGGESTIONS_FETCH_FAIL';
export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS';
2021-09-17 13:38:38 -07:00
export const SUGGESTIONS_V2_FETCH_REQUEST = 'SUGGESTIONS_V2_FETCH_REQUEST';
export const SUGGESTIONS_V2_FETCH_SUCCESS = 'SUGGESTIONS_V2_FETCH_SUCCESS';
export const SUGGESTIONS_V2_FETCH_FAIL = 'SUGGESTIONS_V2_FETCH_FAIL';
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
export function fetchSuggestionsV1() {
return (dispatch, getState) => {
dispatch({ type: SUGGESTIONS_FETCH_REQUEST, skipLoading: true });
return api(getState).get('/api/v1/suggestions').then(({ data: accounts }) => {
2021-09-17 13:38:38 -07:00
dispatch(importFetchedAccounts(accounts));
dispatch({ type: SUGGESTIONS_FETCH_SUCCESS, accounts, skipLoading: true });
return accounts;
2021-09-17 13:38:38 -07:00
}).catch(error => {
dispatch({ type: SUGGESTIONS_FETCH_FAIL, error, skipLoading: true, skipAlert: true });
throw error;
2021-09-17 13:38:38 -07:00
});
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
export function fetchSuggestionsV2() {
return (dispatch, getState) => {
dispatch({ type: SUGGESTIONS_V2_FETCH_REQUEST, skipLoading: true });
return api(getState).get('/api/v2/suggestions').then(({ data: suggestions }) => {
2021-09-17 13:38:38 -07:00
const accounts = suggestions.map(({ account }) => account);
dispatch(importFetchedAccounts(accounts));
dispatch({ type: SUGGESTIONS_V2_FETCH_SUCCESS, suggestions, skipLoading: true });
return suggestions;
2021-09-17 13:38:38 -07:00
}).catch(error => {
dispatch({ type: SUGGESTIONS_V2_FETCH_FAIL, error, skipLoading: true, skipAlert: true });
throw error;
2021-09-17 13:38:38 -07:00
});
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
export function fetchSuggestions() {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
if (features.suggestionsV2) {
dispatch(fetchSuggestionsV2())
.then(suggestions => {
const accountIds = suggestions.map(({ account }) => account.id);
dispatch(fetchRelationships(accountIds));
})
.catch(() => {});
2021-09-17 13:38:38 -07:00
} else if (features.suggestions) {
dispatch(fetchSuggestionsV1())
.then(accounts => {
const accountIds = accounts.map(({ id }) => id);
dispatch(fetchRelationships(accountIds));
})
.catch(() => {});
2021-09-17 13:38:38 -07:00
} else {
// Do nothing
}
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
export const dismissSuggestion = accountId => (dispatch, getState) => {
2021-03-24 09:44:51 -07:00
if (!isLoggedIn(getState)) return;
2020-03-27 13:59:38 -07:00
dispatch({
type: SUGGESTIONS_DISMISS,
id: accountId,
});
api(getState).delete(`/api/v1/suggestions/${accountId}`);
};