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

93 lines
3.1 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';
import api, { getLinks } 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';
2022-05-14 11:33:14 -07:00
export const SUGGESTIONS_FETCH_FAIL = 'SUGGESTIONS_FETCH_FAIL';
2020-03-27 13:59:38 -07:00
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';
2022-05-14 11:33:14 -07:00
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 });
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
2022-03-16 14:01:36 -07:00
export function fetchSuggestionsV2(params = {}) {
2021-09-17 13:38:38 -07:00
return (dispatch, getState) => {
const next = getState().getIn(['suggestions', 'next']);
2021-09-17 13:38:38 -07:00
dispatch({ type: SUGGESTIONS_V2_FETCH_REQUEST, skipLoading: true });
return api(getState).get(next ? next.uri : '/api/v2/suggestions', next ? {} : { params }).then((response) => {
const suggestions = response.data;
2021-09-17 13:38:38 -07:00
const accounts = suggestions.map(({ account }) => account);
const next = getLinks(response).refs.find(link => link.rel === 'next');
2021-09-17 13:38:38 -07:00
dispatch(importFetchedAccounts(accounts));
dispatch({ type: SUGGESTIONS_V2_FETCH_SUCCESS, suggestions, next, 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
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();
2022-05-14 11:33:14 -07:00
const me = state.get('me');
2021-09-17 13:38:38 -07:00
const instance = state.get('instance');
const features = getFeatures(instance);
2020-03-27 13:59:38 -07:00
2022-05-14 11:33:14 -07:00
if (!me) return;
2021-09-17 13:38:38 -07:00
if (features.suggestionsV2) {
2022-03-16 14:01:36 -07:00
dispatch(fetchSuggestionsV2(params))
.then(suggestions => {
const accountIds = suggestions.map(({ account }) => account.id);
dispatch(fetchRelationships(accountIds));
})
2022-05-14 11:33:14 -07:00
.catch(() => { });
2021-09-17 13:38:38 -07:00
} else if (features.suggestions) {
2022-03-16 14:01:36 -07:00
dispatch(fetchSuggestionsV1(params))
.then(accounts => {
const accountIds = accounts.map(({ id }) => id);
dispatch(fetchRelationships(accountIds));
})
2022-05-14 11:33:14 -07:00
.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}`);
};