You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.3 KiB

  1. import { connect } from 'react-redux';
  2. import Warning from '../components/warning';
  3. import { createSelector } from 'reselect';
  4. import PropTypes from 'prop-types';
  5. import { FormattedMessage } from 'react-intl';
  6. const getMentionedUsernames = createSelector(state => state.getIn(['compose', 'text']), text => text.match(/(?:^|[^\/\w])@([a-z0-9_]+@[a-z0-9\.\-]+)/ig));
  7. const getMentionedDomains = createSelector(getMentionedUsernames, mentionedUsernamesWithDomains => {
  8. return mentionedUsernamesWithDomains !== null ? [...new Set(mentionedUsernamesWithDomains.map(item => item.split('@')[2]))] : [];
  9. });
  10. const mapStateToProps = state => {
  11. const mentionedUsernames = getMentionedUsernames(state);
  12. const mentionedUsernamesWithDomains = getMentionedDomains(state);
  13. return {
  14. needsLeakWarning: (state.getIn(['compose', 'privacy']) === 'private' || state.getIn(['compose', 'privacy']) === 'direct') && mentionedUsernames !== null,
  15. mentionedDomains: mentionedUsernamesWithDomains,
  16. needsLockWarning: state.getIn(['compose', 'privacy']) === 'private' && !state.getIn(['accounts', state.getIn(['meta', 'me']), 'locked'])
  17. };
  18. };
  19. const WarningWrapper = ({ needsLeakWarning, needsLockWarning, mentionedDomains }) => {
  20. if (needsLockWarning) {
  21. return <Warning message={<FormattedMessage id='compose_form.lock_disclaimer' defaultMessage='Your account is not {locked}. Anyone can follow you to view your follower-only posts.' values={{ locked: <a href='/settings/profile'><FormattedMessage id='compose_form.lock_disclaimer.lock' defaultMessage='locked' /></a> }} />} />;
  22. } else if (needsLeakWarning) {
  23. return (
  24. <Warning
  25. message={<FormattedMessage
  26. id='compose_form.privacy_disclaimer'
  27. defaultMessage='Your private status will be delivered to mentioned users on {domains}. Do you trust {domainsCount, plural, one {that server} other {those servers}} to not leak your status?'
  28. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.length }}
  29. />}
  30. />
  31. );
  32. }
  33. return null;
  34. };
  35. WarningWrapper.propTypes = {
  36. needsLeakWarning: PropTypes.bool,
  37. needsLockWarning: PropTypes.bool,
  38. mentionedDomains: PropTypes.array.isRequired,
  39. };
  40. export default connect(mapStateToProps)(WarningWrapper);