闭社主体 forked from https://github.com/tootsuite/mastodon
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.

51 lines
2.6 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import { connect } from 'react-redux';
  4. import Warning from '../components/warning';
  5. import { createSelector } from 'reselect';
  6. import PropTypes from 'prop-types';
  7. import { FormattedMessage } from 'react-intl';
  8. import { OrderedSet } from 'immutable';
  9. const getMentionedUsernames = createSelector(state => state.getIn(['compose', 'text']), text => text.match(/(?:^|[^\/\w])@([a-z0-9_]+@[a-z0-9\.\-]+)/ig));
  10. const getMentionedDomains = createSelector(getMentionedUsernames, mentionedUsernamesWithDomains => {
  11. return OrderedSet(mentionedUsernamesWithDomains !== null ? mentionedUsernamesWithDomains.map(item => item.split('@')[2]) : []);
  12. });
  13. const mapStateToProps = state => {
  14. const mentionedUsernames = getMentionedUsernames(state);
  15. const mentionedUsernamesWithDomains = getMentionedDomains(state);
  16. return {
  17. needsLeakWarning: (state.getIn(['compose', 'privacy']) === 'private' || state.getIn(['compose', 'privacy']) === 'direct') && mentionedUsernames !== null,
  18. mentionedDomains: mentionedUsernamesWithDomains,
  19. needsLockWarning: state.getIn(['compose', 'privacy']) === 'private' && !state.getIn(['accounts', state.getIn(['meta', 'me']), 'locked']),
  20. };
  21. };
  22. const WarningWrapper = ({ needsLeakWarning, needsLockWarning, mentionedDomains }) => {
  23. if (needsLockWarning) {
  24. 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> }} />} />;
  25. } else if (needsLeakWarning) {
  26. return (
  27. <Warning
  28. message={<FormattedMessage
  29. id='compose_form.privacy_disclaimer'
  30. 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.'
  31. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.size }}
  32. />}
  33. />
  34. );
  35. }
  36. return null;
  37. };
  38. WarningWrapper.propTypes = {
  39. needsLeakWarning: PropTypes.bool,
  40. needsLockWarning: PropTypes.bool,
  41. mentionedDomains: ImmutablePropTypes.orderedSet.isRequired,
  42. };
  43. export default connect(mapStateToProps)(WarningWrapper);