2022-06-15 13:11:36 -07:00
|
|
|
import { defineMessages } from 'react-intl';
|
|
|
|
|
2022-12-20 07:47:46 -08:00
|
|
|
import toast from 'soapbox/toast';
|
2022-06-15 13:11:36 -07:00
|
|
|
import { isLoggedIn } from 'soapbox/utils/auth';
|
2022-07-16 10:59:00 -07:00
|
|
|
import { getFeatures } from 'soapbox/utils/features';
|
2022-06-15 13:11:36 -07:00
|
|
|
|
|
|
|
import api from '../api';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST';
|
|
|
|
const FILTERS_FETCH_SUCCESS = 'FILTERS_FETCH_SUCCESS';
|
|
|
|
const FILTERS_FETCH_FAIL = 'FILTERS_FETCH_FAIL';
|
2022-06-15 13:11:36 -07:00
|
|
|
|
2023-03-04 12:22:59 -08:00
|
|
|
const FILTER_FETCH_REQUEST = 'FILTER_FETCH_REQUEST';
|
|
|
|
const FILTER_FETCH_SUCCESS = 'FILTER_FETCH_SUCCESS';
|
|
|
|
const FILTER_FETCH_FAIL = 'FILTER_FETCH_FAIL';
|
|
|
|
|
2022-06-15 13:11:36 -07:00
|
|
|
const FILTERS_CREATE_REQUEST = 'FILTERS_CREATE_REQUEST';
|
|
|
|
const FILTERS_CREATE_SUCCESS = 'FILTERS_CREATE_SUCCESS';
|
|
|
|
const FILTERS_CREATE_FAIL = 'FILTERS_CREATE_FAIL';
|
|
|
|
|
2023-03-04 12:22:59 -08:00
|
|
|
const FILTERS_UPDATE_REQUEST = 'FILTERS_UPDATE_REQUEST';
|
|
|
|
const FILTERS_UPDATE_SUCCESS = 'FILTERS_UPDATE_SUCCESS';
|
|
|
|
const FILTERS_UPDATE_FAIL = 'FILTERS_UPDATE_FAIL';
|
|
|
|
|
2022-06-15 13:11:36 -07:00
|
|
|
const FILTERS_DELETE_REQUEST = 'FILTERS_DELETE_REQUEST';
|
|
|
|
const FILTERS_DELETE_SUCCESS = 'FILTERS_DELETE_SUCCESS';
|
|
|
|
const FILTERS_DELETE_FAIL = 'FILTERS_DELETE_FAIL';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
added: { id: 'filters.added', defaultMessage: 'Filter added.' },
|
|
|
|
removed: { id: 'filters.removed', defaultMessage: 'Filter deleted.' },
|
|
|
|
});
|
|
|
|
|
2023-10-02 11:54:02 -07:00
|
|
|
type FilterKeywords = { keyword: string; whole_word: boolean }[];
|
2023-03-04 03:43:27 -08:00
|
|
|
|
2023-03-03 13:40:39 -08:00
|
|
|
const fetchFiltersV1 = () =>
|
2022-06-15 13:11:36 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2023-03-03 13:40:39 -08:00
|
|
|
dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_REQUEST,
|
2023-03-03 13:40:39 -08:00
|
|
|
skipLoading: true,
|
|
|
|
});
|
2022-07-16 10:59:00 -07:00
|
|
|
|
2023-03-04 12:22:59 -08:00
|
|
|
return api(getState)
|
2023-03-03 13:40:39 -08:00
|
|
|
.get('/api/v1/filters')
|
|
|
|
.then(({ data }) => dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_SUCCESS,
|
2023-03-03 13:40:39 -08:00
|
|
|
filters: data,
|
|
|
|
skipLoading: true,
|
|
|
|
}))
|
|
|
|
.catch(err => dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_FAIL,
|
2023-03-03 13:40:39 -08:00
|
|
|
err,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
|
|
|
}));
|
|
|
|
};
|
2022-07-16 10:59:00 -07:00
|
|
|
|
2023-03-03 13:40:39 -08:00
|
|
|
const fetchFiltersV2 = () =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2022-06-15 13:11:36 -07:00
|
|
|
dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_REQUEST,
|
2022-06-15 13:11:36 -07:00
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
2023-03-04 12:22:59 -08:00
|
|
|
return api(getState)
|
2023-03-03 13:40:39 -08:00
|
|
|
.get('/api/v2/filters')
|
2022-06-15 13:11:36 -07:00
|
|
|
.then(({ data }) => dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_SUCCESS,
|
2022-06-15 13:11:36 -07:00
|
|
|
filters: data,
|
|
|
|
skipLoading: true,
|
|
|
|
}))
|
|
|
|
.catch(err => dispatch({
|
2023-03-04 03:43:27 -08:00
|
|
|
type: FILTERS_FETCH_FAIL,
|
2022-06-15 13:11:36 -07:00
|
|
|
err,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const fetchFilters = (fromFiltersPage = false) =>
|
2023-03-03 13:40:39 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (!isLoggedIn(getState)) return;
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
const instance = state.instance;
|
|
|
|
const features = getFeatures(instance);
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
if (features.filtersV2 && fromFiltersPage) return dispatch(fetchFiltersV2());
|
2023-03-03 13:40:39 -08:00
|
|
|
|
|
|
|
if (features.filters) return dispatch(fetchFiltersV1());
|
|
|
|
};
|
|
|
|
|
2023-03-04 12:22:59 -08:00
|
|
|
const fetchFilterV1 = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({
|
|
|
|
type: FILTER_FETCH_REQUEST,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return api(getState)
|
|
|
|
.get(`/api/v1/filters/${id}`)
|
|
|
|
.then(({ data }) => dispatch({
|
|
|
|
type: FILTER_FETCH_SUCCESS,
|
|
|
|
filter: data,
|
|
|
|
skipLoading: true,
|
|
|
|
}))
|
|
|
|
.catch(err => dispatch({
|
|
|
|
type: FILTER_FETCH_FAIL,
|
|
|
|
err,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchFilterV2 = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({
|
|
|
|
type: FILTER_FETCH_REQUEST,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return api(getState)
|
|
|
|
.get(`/api/v2/filters/${id}`)
|
|
|
|
.then(({ data }) => dispatch({
|
|
|
|
type: FILTER_FETCH_SUCCESS,
|
|
|
|
filter: data,
|
|
|
|
skipLoading: true,
|
|
|
|
}))
|
|
|
|
.catch(err => dispatch({
|
|
|
|
type: FILTER_FETCH_FAIL,
|
|
|
|
err,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchFilter = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const instance = state.instance;
|
|
|
|
const features = getFeatures(instance);
|
|
|
|
|
|
|
|
if (features.filtersV2) return dispatch(fetchFilterV2(id));
|
|
|
|
|
|
|
|
if (features.filters) return dispatch(fetchFilterV1(id));
|
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const createFilterV1 = (title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords: FilterKeywords) =>
|
2022-06-15 13:11:36 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_REQUEST });
|
|
|
|
return api(getState).post('/api/v1/filters', {
|
2023-03-04 03:43:27 -08:00
|
|
|
phrase: keywords[0].keyword,
|
2022-06-15 13:11:36 -07:00
|
|
|
context,
|
2023-03-04 03:43:27 -08:00
|
|
|
irreversible: hide,
|
|
|
|
whole_word: keywords[0].whole_word,
|
2023-03-05 10:49:40 -08:00
|
|
|
expires_in,
|
2022-06-15 13:11:36 -07:00
|
|
|
}).then(response => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_SUCCESS, filter: response.data });
|
2022-12-20 07:47:46 -08:00
|
|
|
toast.success(messages.added);
|
2022-06-15 13:11:36 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const createFilterV2 = (title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords_attributes: FilterKeywords) =>
|
2023-03-04 03:43:27 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_REQUEST });
|
|
|
|
return api(getState).post('/api/v2/filters', {
|
|
|
|
title,
|
|
|
|
context,
|
|
|
|
filter_action: hide ? 'hide' : 'warn',
|
2023-03-05 10:49:40 -08:00
|
|
|
expires_in,
|
2023-03-04 03:43:27 -08:00
|
|
|
keywords_attributes,
|
|
|
|
}).then(response => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_SUCCESS, filter: response.data });
|
|
|
|
toast.success(messages.added);
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_CREATE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const createFilter = (title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords: FilterKeywords) =>
|
2023-03-04 03:43:27 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const instance = state.instance;
|
|
|
|
const features = getFeatures(instance);
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
if (features.filtersV2) return dispatch(createFilterV2(title, expires_in, context, hide, keywords));
|
2023-03-04 03:43:27 -08:00
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
return dispatch(createFilterV1(title, expires_in, context, hide, keywords));
|
2023-03-04 03:43:27 -08:00
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const updateFilterV1 = (id: string, title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords: FilterKeywords) =>
|
2023-03-04 12:22:59 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_REQUEST });
|
|
|
|
return api(getState).patch(`/api/v1/filters/${id}`, {
|
|
|
|
phrase: keywords[0].keyword,
|
|
|
|
context,
|
|
|
|
irreversible: hide,
|
|
|
|
whole_word: keywords[0].whole_word,
|
2023-03-05 10:49:40 -08:00
|
|
|
expires_in,
|
2023-03-04 12:22:59 -08:00
|
|
|
}).then(response => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_SUCCESS, filter: response.data });
|
|
|
|
toast.success(messages.added);
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const updateFilterV2 = (id: string, title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords_attributes: FilterKeywords) =>
|
2023-03-04 12:22:59 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_REQUEST });
|
|
|
|
return api(getState).patch(`/api/v2/filters/${id}`, {
|
|
|
|
title,
|
|
|
|
context,
|
|
|
|
filter_action: hide ? 'hide' : 'warn',
|
2023-03-05 10:49:40 -08:00
|
|
|
expires_in,
|
2023-03-04 12:22:59 -08:00
|
|
|
keywords_attributes,
|
|
|
|
}).then(response => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_SUCCESS, filter: response.data });
|
|
|
|
toast.success(messages.added);
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_UPDATE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
const updateFilter = (id: string, title: string, expires_in: string | null, context: Array<string>, hide: boolean, keywords: FilterKeywords) =>
|
2023-03-04 12:22:59 -08:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const instance = state.instance;
|
|
|
|
const features = getFeatures(instance);
|
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
if (features.filtersV2) return dispatch(updateFilterV2(id, title, expires_in, context, hide, keywords));
|
2023-03-04 12:22:59 -08:00
|
|
|
|
2023-03-05 10:49:40 -08:00
|
|
|
return dispatch(updateFilterV1(id, title, expires_in, context, hide, keywords));
|
2023-03-04 12:22:59 -08:00
|
|
|
};
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const deleteFilterV1 = (id: string) =>
|
2022-06-15 13:11:36 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_REQUEST });
|
|
|
|
return api(getState).delete(`/api/v1/filters/${id}`).then(response => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_SUCCESS, filter: response.data });
|
2022-12-20 07:47:46 -08:00
|
|
|
toast.success(messages.removed);
|
2022-06-15 13:11:36 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const deleteFilterV2 = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_REQUEST });
|
|
|
|
return api(getState).delete(`/api/v2/filters/${id}`).then(response => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_SUCCESS, filter: response.data });
|
|
|
|
toast.success(messages.removed);
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: FILTERS_DELETE_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteFilter = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const instance = state.instance;
|
|
|
|
const features = getFeatures(instance);
|
|
|
|
|
|
|
|
if (features.filtersV2) return dispatch(deleteFilterV2(id));
|
|
|
|
|
|
|
|
return dispatch(deleteFilterV1(id));
|
|
|
|
};
|
|
|
|
|
2022-06-15 13:11:36 -07:00
|
|
|
export {
|
2023-03-04 03:43:27 -08:00
|
|
|
FILTERS_FETCH_REQUEST,
|
|
|
|
FILTERS_FETCH_SUCCESS,
|
|
|
|
FILTERS_FETCH_FAIL,
|
2023-03-04 12:22:59 -08:00
|
|
|
FILTER_FETCH_REQUEST,
|
|
|
|
FILTER_FETCH_SUCCESS,
|
|
|
|
FILTER_FETCH_FAIL,
|
2022-06-15 13:11:36 -07:00
|
|
|
FILTERS_CREATE_REQUEST,
|
|
|
|
FILTERS_CREATE_SUCCESS,
|
|
|
|
FILTERS_CREATE_FAIL,
|
2023-03-04 12:22:59 -08:00
|
|
|
FILTERS_UPDATE_REQUEST,
|
|
|
|
FILTERS_UPDATE_SUCCESS,
|
|
|
|
FILTERS_UPDATE_FAIL,
|
2022-06-15 13:11:36 -07:00
|
|
|
FILTERS_DELETE_REQUEST,
|
|
|
|
FILTERS_DELETE_SUCCESS,
|
|
|
|
FILTERS_DELETE_FAIL,
|
|
|
|
fetchFilters,
|
2023-03-04 12:22:59 -08:00
|
|
|
fetchFilter,
|
2022-06-15 13:11:36 -07:00
|
|
|
createFilter,
|
2023-03-04 12:22:59 -08:00
|
|
|
updateFilter,
|
2022-06-15 13:11:36 -07:00
|
|
|
deleteFilter,
|
2023-03-03 13:40:39 -08:00
|
|
|
};
|