2022-11-29 15:32:21 -08:00
|
|
|
import api, { getLinks } from '../api';
|
|
|
|
|
|
|
|
import { importFetchedStatuses } from './importer';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const STATUS_QUOTES_FETCH_REQUEST = 'STATUS_QUOTES_FETCH_REQUEST';
|
|
|
|
const STATUS_QUOTES_FETCH_SUCCESS = 'STATUS_QUOTES_FETCH_SUCCESS';
|
|
|
|
const STATUS_QUOTES_FETCH_FAIL = 'STATUS_QUOTES_FETCH_FAIL';
|
2022-11-29 15:32:21 -08:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const STATUS_QUOTES_EXPAND_REQUEST = 'STATUS_QUOTES_EXPAND_REQUEST';
|
|
|
|
const STATUS_QUOTES_EXPAND_SUCCESS = 'STATUS_QUOTES_EXPAND_SUCCESS';
|
|
|
|
const STATUS_QUOTES_EXPAND_FAIL = 'STATUS_QUOTES_EXPAND_FAIL';
|
2022-11-29 15:32:21 -08:00
|
|
|
|
|
|
|
const noOp = () => new Promise(f => f(null));
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const fetchStatusQuotes = (statusId: string) =>
|
2022-11-29 15:32:21 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (getState().status_lists.getIn([`quotes:${statusId}`, 'isLoading'])) {
|
|
|
|
return dispatch(noOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
statusId,
|
|
|
|
type: STATUS_QUOTES_FETCH_REQUEST,
|
|
|
|
});
|
|
|
|
|
2024-05-11 14:37:37 -07:00
|
|
|
return api(getState)(`/api/v1/pleroma/statuses/${statusId}/quotes`).then(response => {
|
2022-11-29 15:32:21 -08:00
|
|
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
2024-05-11 14:37:37 -07:00
|
|
|
dispatch(importFetchedStatuses(response.json));
|
2022-11-29 15:32:21 -08:00
|
|
|
return dispatch({
|
|
|
|
type: STATUS_QUOTES_FETCH_SUCCESS,
|
|
|
|
statusId,
|
2024-05-11 14:37:37 -07:00
|
|
|
statuses: response.json,
|
2022-11-29 15:32:21 -08:00
|
|
|
next: next ? next.uri : null,
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: STATUS_QUOTES_FETCH_FAIL,
|
|
|
|
statusId,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const expandStatusQuotes = (statusId: string) =>
|
2022-11-29 15:32:21 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const url = getState().status_lists.getIn([`quotes:${statusId}`, 'next'], null) as string | null;
|
|
|
|
|
|
|
|
if (url === null || getState().status_lists.getIn([`quotes:${statusId}`, 'isLoading'])) {
|
|
|
|
return dispatch(noOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: STATUS_QUOTES_EXPAND_REQUEST,
|
|
|
|
statusId,
|
|
|
|
});
|
|
|
|
|
2024-05-11 14:37:37 -07:00
|
|
|
return api(getState)(url).then(response => {
|
2022-11-29 15:32:21 -08:00
|
|
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
2024-05-11 14:37:37 -07:00
|
|
|
dispatch(importFetchedStatuses(response.json));
|
2022-11-29 15:32:21 -08:00
|
|
|
dispatch({
|
|
|
|
type: STATUS_QUOTES_EXPAND_SUCCESS,
|
|
|
|
statusId,
|
2024-05-11 14:37:37 -07:00
|
|
|
statuses: response.json,
|
2022-11-29 15:32:21 -08:00
|
|
|
next: next ? next.uri : null,
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: STATUS_QUOTES_EXPAND_FAIL,
|
|
|
|
statusId,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2024-05-12 16:18:04 -07:00
|
|
|
|
|
|
|
export {
|
|
|
|
STATUS_QUOTES_FETCH_REQUEST,
|
|
|
|
STATUS_QUOTES_FETCH_SUCCESS,
|
|
|
|
STATUS_QUOTES_FETCH_FAIL,
|
|
|
|
STATUS_QUOTES_EXPAND_REQUEST,
|
|
|
|
STATUS_QUOTES_EXPAND_SUCCESS,
|
|
|
|
STATUS_QUOTES_EXPAND_FAIL,
|
|
|
|
fetchStatusQuotes,
|
|
|
|
expandStatusQuotes,
|
|
|
|
};
|