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:25:06 -08:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import api from '../api';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-10-02 14:09:28 -07:00
|
|
|
import { fetchRelationships } from './accounts';
|
2022-01-10 14:17:52 -08:00
|
|
|
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
|
|
|
|
2022-03-16 14:01:36 -07:00
|
|
|
export function fetchSuggestionsV1(params = {}) {
|
2021-09-17 13:38:38 -07:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: SUGGESTIONS_FETCH_REQUEST, skipLoading: true });
|
2022-03-16 14:01:36 -07:00
|
|
|
return api(getState).get('/api/v1/suggestions', { params }).then(({ data: accounts }) => {
|
2021-09-17 13:38:38 -07:00
|
|
|
dispatch(importFetchedAccounts(accounts));
|
|
|
|
dispatch({ type: SUGGESTIONS_FETCH_SUCCESS, accounts, skipLoading: true });
|
2021-10-02 14:09:28 -07:00
|
|
|
return accounts;
|
2021-09-17 13:38:38 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: SUGGESTIONS_FETCH_FAIL, error, skipLoading: true, skipAlert: true });
|
2021-10-02 14:09:28 -07:00
|
|
|
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
|
|
|
|
2022-03-16 14:01:36 -07:00
|
|
|
export function fetchSuggestionsV2(params = {}) {
|
2021-09-17 13:38:38 -07:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: SUGGESTIONS_V2_FETCH_REQUEST, skipLoading: true });
|
2022-03-16 14:01:36 -07:00
|
|
|
return api(getState).get('/api/v2/suggestions', { params }).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 });
|
2021-10-02 14:09:28 -07:00
|
|
|
return suggestions;
|
2021-09-17 13:38:38 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: SUGGESTIONS_V2_FETCH_FAIL, error, skipLoading: true, skipAlert: true });
|
2021-10-02 14:09:28 -07:00
|
|
|
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
|
|
|
|
2022-03-16 14:01:36 -07:00
|
|
|
export function fetchSuggestions(params = { limit: 50 }) {
|
2021-09-17 13:38:38 -07:00
|
|
|
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) {
|
2022-03-16 14:01:36 -07:00
|
|
|
dispatch(fetchSuggestionsV2(params))
|
2021-10-02 14:09:28 -07:00
|
|
|
.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) {
|
2022-03-16 14:01:36 -07:00
|
|
|
dispatch(fetchSuggestionsV1(params))
|
2021-10-02 14:09:28 -07:00
|
|
|
.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}`);
|
|
|
|
};
|