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.

50 lines
2.6 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import Warning from '../components/warning';
  4. import { createSelector } from 'reselect';
  5. import PropTypes from 'prop-types';
  6. import { FormattedMessage } from 'react-intl';
  7. import { OrderedSet } from 'immutable';
  8. const getMentionedUsernames = createSelector(state => state.getIn(['compose', 'text']), text => text.match(/(?:^|[^\/\w])@([a-z0-9_]+@[a-z0-9\.\-]+)/ig));
  9. const getMentionedDomains = createSelector(getMentionedUsernames, mentionedUsernamesWithDomains => {
  10. return OrderedSet(mentionedUsernamesWithDomains !== null ? mentionedUsernamesWithDomains.map(item => item.split('@')[2]) : []);
  11. });
  12. const mapStateToProps = state => {
  13. const mentionedUsernames = getMentionedUsernames(state);
  14. const mentionedUsernamesWithDomains = getMentionedDomains(state);
  15. return {
  16. needsLeakWarning: (state.getIn(['compose', 'privacy']) === 'private' || state.getIn(['compose', 'privacy']) === 'direct') && mentionedUsernames !== null,
  17. mentionedDomains: mentionedUsernamesWithDomains,
  18. needsLockWarning: state.getIn(['compose', 'privacy']) === 'private' && !state.getIn(['accounts', state.getIn(['meta', 'me']), 'locked'])
  19. };
  20. };
  21. const WarningWrapper = ({ needsLeakWarning, needsLockWarning, mentionedDomains }) => {
  22. if (needsLockWarning) {
  23. 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> }} />} />;
  24. } else if (needsLeakWarning) {
  25. return (
  26. <Warning
  27. message={<FormattedMessage
  28. id='compose_form.privacy_disclaimer'
  29. defaultMessage='Your private status will be delivered to mentioned users on {domains}. Do you trust {domainsCount, plural, one {that server} other {those servers}}? Post privacy only works on Mastodon instances. If {domains} {domainsCount, plural, one {is not a Mastodon instance} other {are not Mastodon instances}}, there will be no indication that your post is private, and it may be boosted or otherwise made visible to unintended recipients.'
  30. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.size }}
  31. />}
  32. />
  33. );
  34. }
  35. return null;
  36. };
  37. WarningWrapper.propTypes = {
  38. needsLeakWarning: PropTypes.bool,
  39. needsLockWarning: PropTypes.bool,
  40. mentionedDomains: PropTypes.array.isRequired,
  41. };
  42. export default connect(mapStateToProps)(WarningWrapper);