split functions for admin api actions

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
environments/review-develop-3zknud/deployments/43^2
marcin mikołajczak 2022-05-21 08:35:33 +02:00
rodzic 88b91bce3e
commit 10bdd664b0
1 zmienionych plików z 170 dodań i 109 usunięć

Wyświetl plik

@ -100,16 +100,8 @@ export function updateConfig(configs) {
};
}
export function fetchReports(params = {}) {
function fetchMastodonReports(params) {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
dispatch({ type: ADMIN_REPORTS_FETCH_REQUEST, params });
if (features.mastodonAdminApi) {
return api(getState)
.get('/api/v1/admin/reports', { params })
.then(({ data: reports }) => {
@ -122,14 +114,13 @@ export function fetchReports(params = {}) {
}).catch(error => {
dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params });
});
}
const { resolved } = params;
};
}
function fetchPleromaReports(params) {
return (dispatch, getState) => {
return api(getState)
.get('/api/pleroma/admin/reports', { params: {
state: resolved === false ? 'open' : (resolved ? 'resolved' : null),
} })
.get('/api/pleroma/admin/reports', { params })
.then(({ data: { reports } }) => {
reports.forEach(report => {
dispatch(importFetchedAccount(report.account));
@ -143,28 +134,42 @@ export function fetchReports(params = {}) {
};
}
function patchReports(ids, state) {
export function fetchReports(params = {}) {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
const reports = ids.map(id => ({ id, state }));
dispatch({ type: ADMIN_REPORTS_PATCH_REQUEST, reports });
dispatch({ type: ADMIN_REPORTS_FETCH_REQUEST, params });
if (features.mastodonAdminApi) {
return Promise.all(ids.map(id => api(getState)
return dispatch(fetchMastodonReports(params));
} else {
const { resolved } = params;
return dispatch(fetchPleromaReports({
state: resolved === false ? 'open' : (resolved ? 'resolved' : null),
}));
}
};
}
function patchMastodonReports(reports) {
return (dispatch, getState) => {
return Promise.all(reports.map(({ id, state }) => api(getState)
.post(`/api/v1/admin/reports/${id}/${state === 'resolved' ? 'reopen' : 'resolve'}`)
.then(({ data }) => {
.then(() => {
dispatch({ type: ADMIN_REPORTS_PATCH_SUCCESS, reports });
}).catch(error => {
dispatch({ type: ADMIN_REPORTS_PATCH_FAIL, error, reports });
}),
));
}
};
}
function patchPleromaReports(reports) {
return (dispatch, getState) => {
return api(getState)
.patch('/api/pleroma/admin/reports', { reports })
.then(() => {
@ -174,20 +179,32 @@ function patchReports(ids, state) {
});
};
}
export function closeReports(ids) {
return patchReports(ids, 'closed');
}
export function fetchUsers(filters = [], page = 1, query, pageSize = 50, next) {
function patchReports(ids, reportState) {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
dispatch({ type: ADMIN_USERS_FETCH_REQUEST, filters, page, pageSize });
const reports = ids.map(id => ({ id, state: reportState }));
dispatch({ type: ADMIN_REPORTS_PATCH_REQUEST, reports });
if (features.mastodonAdminApi) {
return dispatch(patchMastodonReports(reports));
} else {
return dispatch(patchPleromaReports(reports));
}
};
}
export function closeReports(ids) {
return patchReports(ids, 'closed');
}
function fetchMastodonUsers(filters, page, query, pageSize, next) {
return (dispatch, getState) => {
const params = {
username: query,
};
@ -212,8 +229,11 @@ export function fetchUsers(filters = [], page = 1, query, pageSize = 50, next) {
}).catch(error => {
dispatch({ type: ADMIN_USERS_FETCH_FAIL, error, filters, page, pageSize });
});
}
};
}
function fetchPleromaUsers(filters, page, query, pageSize) {
return (dispatch, getState) => {
const params = { filters: filters.join(), page, page_size: pageSize };
if (query) params.query = query;
@ -229,16 +249,25 @@ export function fetchUsers(filters = [], page = 1, query, pageSize = 50, next) {
};
}
export function deactivateUsers(accountIds, reportId) {
export function fetchUsers(filters = [], page = 1, query, pageSize = 50, next) {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
dispatch({ type: ADMIN_USERS_DEACTIVATE_REQUEST, accountIds });
dispatch({ type: ADMIN_USERS_FETCH_REQUEST, filters, page, pageSize });
if (features.mastodonAdminApi) {
return dispatch(fetchMastodonUsers(filters, page, query, pageSize, next));
} else {
return dispatch(fetchPleromaUsers(filters, page, query, pageSize));
}
};
}
function deactivateMastodonUsers(accountIds, reportId) {
return (dispatch, getState) => {
return Promise.all(accountIds.map(accountId => {
api(getState)
.post(`/api/v1/admin/accounts/${accountId}/action`, {
@ -251,8 +280,11 @@ export function deactivateUsers(accountIds, reportId) {
dispatch({ type: ADMIN_USERS_DEACTIVATE_FAIL, error, accountIds: [accountId] });
});
}));
}
};
}
function deactivatePleromaUsers(accountIds) {
return (dispatch, getState) => {
const nicknames = nicknamesFromIds(getState, accountIds);
return api(getState)
.patch('/api/pleroma/admin/users/deactivate', { nicknames })
@ -264,6 +296,23 @@ export function deactivateUsers(accountIds, reportId) {
};
}
export function deactivateUsers(accountIds, reportId) {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
dispatch({ type: ADMIN_USERS_DEACTIVATE_REQUEST, accountIds });
if (features.mastodonAdminApi) {
return dispatch(deactivateMastodonUsers(accountIds, reportId));
} else {
return dispatch(deactivatePleromaUsers(accountIds));
}
};
}
export function deleteUsers(accountIds) {
return (dispatch, getState) => {
const nicknames = nicknamesFromIds(getState, accountIds);
@ -278,6 +327,33 @@ export function deleteUsers(accountIds) {
};
}
function approveMastodonUsers(accountIds) {
return (dispatch, getState) => {
return Promise.all(accountIds.map(accountId => {
api(getState)
.post(`/api/v1/admin/accounts/${accountId}/approve`)
.then(({ data: user }) => {
dispatch({ type: ADMIN_USERS_APPROVE_SUCCESS, users: [user], accountIds: [accountId] });
}).catch(error => {
dispatch({ type: ADMIN_USERS_APPROVE_FAIL, error, accountIds: [accountId] });
});
}));
};
}
function approvePleromaUsers(accountIds) {
return (dispatch, getState) => {
const nicknames = nicknamesFromIds(getState, accountIds);
return api(getState)
.patch('/api/pleroma/admin/users/approve', { nicknames })
.then(({ data: { users } }) => {
dispatch({ type: ADMIN_USERS_APPROVE_SUCCESS, users, accountIds });
}).catch(error => {
dispatch({ type: ADMIN_USERS_APPROVE_FAIL, error, accountIds });
});
};
}
export function approveUsers(accountIds) {
return (dispatch, getState) => {
const state = getState();
@ -288,25 +364,10 @@ export function approveUsers(accountIds) {
dispatch({ type: ADMIN_USERS_APPROVE_REQUEST, accountIds });
if (features.mastodonAdminApi) {
return Promise.all(accountIds.map(accountId => {
api(getState)
.post(`/api/v1/admin/accounts/${accountId}/approve`)
.then(({ data: user }) => {
dispatch({ type: ADMIN_USERS_APPROVE_SUCCESS, users: [user], accountIds: [accountId] });
}).catch(error => {
dispatch({ type: ADMIN_USERS_APPROVE_FAIL, error, accountIds: [accountId] });
});
}));
return dispatch(approveMastodonUsers(accountIds));
} else {
return dispatch(approvePleromaUsers(accountIds));
}
const nicknames = nicknamesFromIds(getState, accountIds);
return api(getState)
.patch('/api/pleroma/admin/users/approve', { nicknames })
.then(({ data: { users } }) => {
dispatch({ type: ADMIN_USERS_APPROVE_SUCCESS, users, accountIds });
}).catch(error => {
dispatch({ type: ADMIN_USERS_APPROVE_FAIL, error, accountIds });
});
};
}