pleroma/src/actions/reports.ts

130 lines
3 KiB
TypeScript
Raw Normal View History

import { getClient } from '../api';
import { openModal } from './modals';
2023-06-20 12:24:39 -07:00
import type { Account } from 'soapbox/schemas';
import type { AppDispatch, RootState } from 'soapbox/store';
2023-06-20 12:24:39 -07:00
import type { ChatMessage, Group, Status } from 'soapbox/types/entities';
const REPORT_INIT = 'REPORT_INIT';
const REPORT_CANCEL = 'REPORT_CANCEL';
const REPORT_SUBMIT_REQUEST = 'REPORT_SUBMIT_REQUEST';
const REPORT_SUBMIT_SUCCESS = 'REPORT_SUBMIT_SUCCESS';
const REPORT_SUBMIT_FAIL = 'REPORT_SUBMIT_FAIL';
const REPORT_STATUS_TOGGLE = 'REPORT_STATUS_TOGGLE';
const REPORT_COMMENT_CHANGE = 'REPORT_COMMENT_CHANGE';
const REPORT_FORWARD_CHANGE = 'REPORT_FORWARD_CHANGE';
const REPORT_BLOCK_CHANGE = 'REPORT_BLOCK_CHANGE';
const REPORT_RULE_CHANGE = 'REPORT_RULE_CHANGE';
2023-03-22 10:56:32 -07:00
enum ReportableEntities {
ACCOUNT = 'ACCOUNT',
STATUS = 'STATUS'
}
2022-09-29 06:45:57 -07:00
type ReportedEntity = {
status?: Status;
chatMessage?: ChatMessage;
group?: Group;
2022-09-29 06:45:57 -07:00
}
2023-03-22 10:56:32 -07:00
const initReport = (entityType: ReportableEntities, account: Account, entities?: ReportedEntity) => (dispatch: AppDispatch) => {
const { status, chatMessage, group } = entities || {};
2022-09-29 06:45:57 -07:00
dispatch({
type: REPORT_INIT,
2023-03-22 10:56:32 -07:00
entityType,
2022-09-29 06:45:57 -07:00
account,
status,
chatMessage,
2023-03-22 10:56:32 -07:00
group,
2022-09-29 06:45:57 -07:00
});
return dispatch(openModal('REPORT'));
};
const cancelReport = () => ({
type: REPORT_CANCEL,
});
const toggleStatusReport = (statusId: string, checked: boolean) => ({
type: REPORT_STATUS_TOGGLE,
statusId,
checked,
});
const submitReport = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch(submitReportRequest());
const { reports } = getState();
return getClient(getState()).accounts.reportAccount(reports.new.account_id!, {
status_ids: reports.new.status_ids.toArray(),
// group_id: reports.getIn(['new', 'group', 'id']),
rule_ids: reports.new.rule_ids.toArray(),
comment: reports.new.comment,
forward: reports.new.forward,
})
};
const submitReportRequest = () => ({
type: REPORT_SUBMIT_REQUEST,
});
const submitReportSuccess = () => ({
type: REPORT_SUBMIT_SUCCESS,
});
2023-10-23 15:22:10 -07:00
const submitReportFail = (error: unknown) => ({
type: REPORT_SUBMIT_FAIL,
error,
});
const changeReportComment = (comment: string) => ({
type: REPORT_COMMENT_CHANGE,
comment,
});
const changeReportForward = (forward: boolean) => ({
type: REPORT_FORWARD_CHANGE,
forward,
});
const changeReportBlock = (block: boolean) => ({
type: REPORT_BLOCK_CHANGE,
block,
});
const changeReportRule = (ruleId: string) => ({
type: REPORT_RULE_CHANGE,
rule_id: ruleId,
});
export {
2023-03-22 10:56:32 -07:00
ReportableEntities,
REPORT_INIT,
REPORT_CANCEL,
REPORT_SUBMIT_REQUEST,
REPORT_SUBMIT_SUCCESS,
REPORT_SUBMIT_FAIL,
REPORT_STATUS_TOGGLE,
REPORT_COMMENT_CHANGE,
REPORT_FORWARD_CHANGE,
REPORT_BLOCK_CHANGE,
REPORT_RULE_CHANGE,
initReport,
cancelReport,
toggleStatusReport,
submitReport,
submitReportRequest,
submitReportSuccess,
submitReportFail,
changeReportComment,
changeReportForward,
changeReportBlock,
changeReportRule,
2022-08-11 09:57:09 -07:00
};