[Glitch] Add `forward_to_domains` parameter to `POST /api/v1/reports`
Port c27b82a437
to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
075887e1d6
commit
c699dc0908
|
@ -1,53 +1,75 @@
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { PureComponent } from 'react';
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
|
|
||||||
import { injectIntl, defineMessages, FormattedMessage } from 'react-intl';
|
import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import { OrderedSet, List as ImmutableList } from 'immutable';
|
||||||
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
|
import { shallowEqual } from 'react-redux';
|
||||||
|
import { createSelector } from 'reselect';
|
||||||
|
|
||||||
import Toggle from 'react-toggle';
|
import Toggle from 'react-toggle';
|
||||||
|
|
||||||
|
import { fetchAccount } from 'flavours/glitch/actions/accounts';
|
||||||
import Button from 'flavours/glitch/components/button';
|
import Button from 'flavours/glitch/components/button';
|
||||||
|
import { useAppDispatch, useAppSelector } from 'flavours/glitch/store';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
placeholder: { id: 'report.placeholder', defaultMessage: 'Type or paste additional comments' },
|
placeholder: { id: 'report.placeholder', defaultMessage: 'Type or paste additional comments' },
|
||||||
});
|
});
|
||||||
|
|
||||||
class Comment extends PureComponent {
|
const selectRepliedToAccountIds = createSelector(
|
||||||
|
[
|
||||||
static propTypes = {
|
(state) => state.get('statuses'),
|
||||||
onSubmit: PropTypes.func.isRequired,
|
(_, statusIds) => statusIds,
|
||||||
comment: PropTypes.string.isRequired,
|
],
|
||||||
onChangeComment: PropTypes.func.isRequired,
|
(statusesMap, statusIds) => statusIds.map((statusId) => statusesMap.getIn([statusId, 'in_reply_to_account_id'])),
|
||||||
intl: PropTypes.object.isRequired,
|
{
|
||||||
isSubmitting: PropTypes.bool,
|
resultEqualityCheck: shallowEqual,
|
||||||
forward: PropTypes.bool,
|
|
||||||
isRemote: PropTypes.bool,
|
|
||||||
domain: PropTypes.string,
|
|
||||||
onChangeForward: PropTypes.func.isRequired,
|
|
||||||
};
|
|
||||||
|
|
||||||
handleClick = () => {
|
|
||||||
const { onSubmit } = this.props;
|
|
||||||
onSubmit();
|
|
||||||
};
|
|
||||||
|
|
||||||
handleChange = e => {
|
|
||||||
const { onChangeComment } = this.props;
|
|
||||||
onChangeComment(e.target.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
handleKeyDown = e => {
|
|
||||||
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
||||||
this.handleClick();
|
|
||||||
}
|
}
|
||||||
};
|
);
|
||||||
|
|
||||||
handleForwardChange = e => {
|
const Comment = ({ comment, domain, statusIds, isRemote, isSubmitting, selectedDomains, onSubmit, onChangeComment, onToggleDomain }) => {
|
||||||
const { onChangeForward } = this.props;
|
const intl = useIntl();
|
||||||
onChangeForward(e.target.checked);
|
|
||||||
};
|
|
||||||
|
|
||||||
render () {
|
const dispatch = useAppDispatch();
|
||||||
const { comment, isRemote, forward, domain, isSubmitting, intl } = this.props;
|
const loadedRef = useRef(false);
|
||||||
|
|
||||||
|
const handleClick = useCallback(() => onSubmit(), [onSubmit]);
|
||||||
|
const handleChange = useCallback((e) => onChangeComment(e.target.value), [onChangeComment]);
|
||||||
|
const handleToggleDomain = useCallback(e => onToggleDomain(e.target.value, e.target.checked), [onToggleDomain]);
|
||||||
|
|
||||||
|
const handleKeyDown = useCallback((e) => {
|
||||||
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
||||||
|
handleClick();
|
||||||
|
}
|
||||||
|
}, [handleClick]);
|
||||||
|
|
||||||
|
// Memoize accountIds since we don't want it to trigger `useEffect` on each render
|
||||||
|
const accountIds = useAppSelector((state) => domain ? selectRepliedToAccountIds(state, statusIds) : ImmutableList());
|
||||||
|
|
||||||
|
// While we could memoize `availableDomains`, it is pretty inexpensive to recompute
|
||||||
|
const accountsMap = useAppSelector((state) => state.get('accounts'));
|
||||||
|
const availableDomains = domain ? OrderedSet([domain]).union(accountIds.map((accountId) => accountsMap.getIn([accountId, 'acct'], '').split('@')[1]).filter(domain => !!domain)) : OrderedSet();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loadedRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedRef.current = true;
|
||||||
|
|
||||||
|
// First, pre-select known domains
|
||||||
|
availableDomains.forEach((domain) => {
|
||||||
|
onToggleDomain(domain, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then, fetch missing replied-to accounts
|
||||||
|
const unknownAccounts = OrderedSet(accountIds.filter(accountId => accountId && !accountsMap.has(accountId)));
|
||||||
|
unknownAccounts.forEach((accountId) => {
|
||||||
|
dispatch(fetchAccount(accountId));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -57,8 +79,8 @@ class Comment extends PureComponent {
|
||||||
className='report-dialog-modal__textarea'
|
className='report-dialog-modal__textarea'
|
||||||
placeholder={intl.formatMessage(messages.placeholder)}
|
placeholder={intl.formatMessage(messages.placeholder)}
|
||||||
value={comment}
|
value={comment}
|
||||||
onChange={this.handleChange}
|
onChange={handleChange}
|
||||||
onKeyDown={this.handleKeyDown}
|
onKeyDown={handleKeyDown}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
@ -66,22 +88,34 @@ class Comment extends PureComponent {
|
||||||
<>
|
<>
|
||||||
<p className='report-dialog-modal__lead'><FormattedMessage id='report.forward_hint' defaultMessage='The account is from another server. Send an anonymized copy of the report there as well?' /></p>
|
<p className='report-dialog-modal__lead'><FormattedMessage id='report.forward_hint' defaultMessage='The account is from another server. Send an anonymized copy of the report there as well?' /></p>
|
||||||
|
|
||||||
<label className='report-dialog-modal__toggle'>
|
{ availableDomains.map((domain) => (
|
||||||
<Toggle checked={forward} disabled={isSubmitting} onChange={this.handleForwardChange} />
|
<label className='report-dialog-modal__toggle' key={`toggle-${domain}`}>
|
||||||
|
<Toggle checked={selectedDomains.includes(domain)} disabled={isSubmitting} onChange={handleToggleDomain} value={domain} />
|
||||||
<FormattedMessage id='report.forward' defaultMessage='Forward to {target}' values={{ target: domain }} />
|
<FormattedMessage id='report.forward' defaultMessage='Forward to {target}' values={{ target: domain }} />
|
||||||
</label>
|
</label>
|
||||||
|
))}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className='flex-spacer' />
|
<div className='flex-spacer' />
|
||||||
|
|
||||||
<div className='report-dialog-modal__actions'>
|
<div className='report-dialog-modal__actions'>
|
||||||
<Button onClick={this.handleClick} disabled={isSubmitting}><FormattedMessage id='report.submit' defaultMessage='Submit report' /></Button>
|
<Button onClick={handleClick} disabled={isSubmitting}><FormattedMessage id='report.submit' defaultMessage='Submit report' /></Button>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default injectIntl(Comment);
|
Comment.propTypes = {
|
||||||
|
comment: PropTypes.string.isRequired,
|
||||||
|
domain: PropTypes.string,
|
||||||
|
statusIds: ImmutablePropTypes.list.isRequired,
|
||||||
|
isRemote: PropTypes.bool,
|
||||||
|
isSubmitting: PropTypes.bool,
|
||||||
|
selectedDomains: ImmutablePropTypes.set.isRequired,
|
||||||
|
onSubmit: PropTypes.func.isRequired,
|
||||||
|
onChangeComment: PropTypes.func.isRequired,
|
||||||
|
onToggleDomain: PropTypes.func.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Comment;
|
||||||
|
|
|
@ -46,25 +46,26 @@ class ReportModal extends ImmutablePureComponent {
|
||||||
state = {
|
state = {
|
||||||
step: 'category',
|
step: 'category',
|
||||||
selectedStatusIds: OrderedSet(this.props.statusId ? [this.props.statusId] : []),
|
selectedStatusIds: OrderedSet(this.props.statusId ? [this.props.statusId] : []),
|
||||||
|
selectedDomains: OrderedSet(),
|
||||||
comment: '',
|
comment: '',
|
||||||
category: null,
|
category: null,
|
||||||
selectedRuleIds: OrderedSet(),
|
selectedRuleIds: OrderedSet(),
|
||||||
forward: true,
|
|
||||||
isSubmitting: false,
|
isSubmitting: false,
|
||||||
isSubmitted: false,
|
isSubmitted: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
handleSubmit = () => {
|
handleSubmit = () => {
|
||||||
const { dispatch, accountId } = this.props;
|
const { dispatch, accountId } = this.props;
|
||||||
const { selectedStatusIds, comment, category, selectedRuleIds, forward } = this.state;
|
const { selectedStatusIds, selectedDomains, comment, category, selectedRuleIds } = this.state;
|
||||||
|
|
||||||
this.setState({ isSubmitting: true });
|
this.setState({ isSubmitting: true });
|
||||||
|
|
||||||
dispatch(submitReport({
|
dispatch(submitReport({
|
||||||
account_id: accountId,
|
account_id: accountId,
|
||||||
status_ids: selectedStatusIds.toArray(),
|
status_ids: selectedStatusIds.toArray(),
|
||||||
|
selected_domains: selectedDomains.toArray(),
|
||||||
comment,
|
comment,
|
||||||
forward,
|
forward: selectedDomains.size > 0,
|
||||||
category,
|
category,
|
||||||
rule_ids: selectedRuleIds.toArray(),
|
rule_ids: selectedRuleIds.toArray(),
|
||||||
}, this.handleSuccess, this.handleFail));
|
}, this.handleSuccess, this.handleFail));
|
||||||
|
@ -88,13 +89,19 @@ class ReportModal extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
handleRuleToggle = (ruleId, checked) => {
|
handleDomainToggle = (domain, checked) => {
|
||||||
const { selectedRuleIds } = this.state;
|
|
||||||
|
|
||||||
if (checked) {
|
if (checked) {
|
||||||
this.setState({ selectedRuleIds: selectedRuleIds.add(ruleId) });
|
this.setState((state) => ({ selectedDomains: state.selectedDomains.add(domain) }));
|
||||||
} else {
|
} else {
|
||||||
this.setState({ selectedRuleIds: selectedRuleIds.remove(ruleId) });
|
this.setState((state) => ({ selectedDomains: state.selectedDomains.remove(domain) }));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
handleRuleToggle = (ruleId, checked) => {
|
||||||
|
if (checked) {
|
||||||
|
this.setState((state) => ({ selectedRuleIds: state.selectedRuleIds.add(ruleId) }));
|
||||||
|
} else {
|
||||||
|
this.setState((state) => ({ selectedRuleIds: state.selectedRuleIds.remove(ruleId) }));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -106,10 +113,6 @@ class ReportModal extends ImmutablePureComponent {
|
||||||
this.setState({ comment });
|
this.setState({ comment });
|
||||||
};
|
};
|
||||||
|
|
||||||
handleChangeForward = forward => {
|
|
||||||
this.setState({ forward });
|
|
||||||
};
|
|
||||||
|
|
||||||
handleNextStep = step => {
|
handleNextStep = step => {
|
||||||
this.setState({ step });
|
this.setState({ step });
|
||||||
};
|
};
|
||||||
|
@ -138,8 +141,8 @@ class ReportModal extends ImmutablePureComponent {
|
||||||
step,
|
step,
|
||||||
selectedStatusIds,
|
selectedStatusIds,
|
||||||
selectedRuleIds,
|
selectedRuleIds,
|
||||||
|
selectedDomains,
|
||||||
comment,
|
comment,
|
||||||
forward,
|
|
||||||
category,
|
category,
|
||||||
isSubmitting,
|
isSubmitting,
|
||||||
isSubmitted,
|
isSubmitted,
|
||||||
|
@ -187,10 +190,11 @@ class ReportModal extends ImmutablePureComponent {
|
||||||
isSubmitting={isSubmitting}
|
isSubmitting={isSubmitting}
|
||||||
isRemote={isRemote}
|
isRemote={isRemote}
|
||||||
comment={comment}
|
comment={comment}
|
||||||
forward={forward}
|
|
||||||
domain={domain}
|
domain={domain}
|
||||||
onChangeComment={this.handleChangeComment}
|
onChangeComment={this.handleChangeComment}
|
||||||
onChangeForward={this.handleChangeForward}
|
statusIds={selectedStatusIds}
|
||||||
|
selectedDomains={selectedDomains}
|
||||||
|
onToggleDomain={this.handleDomainToggle}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -709,6 +709,7 @@
|
||||||
&__toggle {
|
&__toggle {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
|
||||||
& > span {
|
& > span {
|
||||||
font-size: 17px;
|
font-size: 17px;
|
||||||
|
|
Loading…
Reference in New Issue