Skip to content

Commit

Permalink
#38 Show alert when mention is added to limited post
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Sep 28, 2023
1 parent 62b7b7a commit d75c850
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { connect } from 'react-redux';

import { me } from 'mastodon/initial_state';
import { HASHTAG_PATTERN_REGEX } from 'mastodon/utils/hashtags';
import { MENTION_PATTERN_REGEX } from 'mastodon/utils/mentions';

import Warning from '../components/warning';

Expand All @@ -14,10 +15,11 @@ const mapStateToProps = state => ({
hashtagWarning: !['public', 'public_unlisted', 'login'].includes(state.getIn(['compose', 'privacy'])) && state.getIn(['compose', 'searchability']) !== 'public' && HASHTAG_PATTERN_REGEX.test(state.getIn(['compose', 'text'])),
directMessageWarning: state.getIn(['compose', 'privacy']) === 'direct',
searchabilityWarning: state.getIn(['compose', 'searchability']) === 'limited',
limitedPostWarning: ['mutual', 'circle'].includes(state.getIn(['compose', 'privacy'])),
mentionWarning: ['mutual', 'circle', 'limited'].includes(state.getIn(['compose', 'privacy'])) && MENTION_PATTERN_REGEX.test(state.getIn(['compose', 'text'])),
limitedPostWarning: ['mutual', 'circle'].includes(state.getIn(['compose', 'privacy'])) && !state.getIn(['compose', 'limited_scope']),
});

const WarningWrapper = ({ needsLockWarning, hashtagWarning, directMessageWarning, searchabilityWarning, limitedPostWarning }) => {
const WarningWrapper = ({ needsLockWarning, hashtagWarning, directMessageWarning, searchabilityWarning, mentionWarning, limitedPostWarning }) => {
if (needsLockWarning) {
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> }} />} />;
}
Expand All @@ -40,6 +42,10 @@ const WarningWrapper = ({ needsLockWarning, hashtagWarning, directMessageWarning
return <Warning message={<FormattedMessage id='compose_form.searchability_warning' defaultMessage='Self only searchability is not available other mastodon servers. Others can search your post.' />} />;
}

if (mentionWarning) {
return <Warning message={<FormattedMessage id='compose_form.mention_warning' defaultMessage='When you add a Mention to a limited post, the person you are mentioning can also see this post.' />} />;
}

if (limitedPostWarning) {
return <Warning message={<FormattedMessage id='compose_form.limited_post_warning' defaultMessage='Limited posts are NOT reached Misskey, normal Mastodon or so on.' />} />;
}
Expand All @@ -52,6 +58,7 @@ WarningWrapper.propTypes = {
hashtagWarning: PropTypes.bool,
directMessageWarning: PropTypes.bool,
searchabilityWarning: PropTypes.bool,
mentionWarning: PropTypes.bool,
limitedPostWarning: PropTypes.bool,
};

Expand Down
4 changes: 4 additions & 0 deletions app/javascript/mastodon/reducers/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ function clearAll(state) {
if (!state.get('in_reply_to')) {
map.set('posted_on_this_session', true);
}
map.set('limited_scope', null);
map.set('id', null);
map.set('in_reply_to', null);
map.set('searchability', state.get('default_searchability'));
Expand Down Expand Up @@ -411,6 +412,7 @@ export default function compose(state = initialState, action) {
map.set('in_reply_to', action.status.get('id'));
map.set('text', statusToTextMentions(state, action.status));
map.set('privacy', privacyPreference(action.status.get('visibility_ex'), state.get('default_privacy')));
map.set('limited_scope', null);
map.set('searchability', privacyPreference(action.status.get('searchability'), state.get('default_searchability')));
map.set('focusDate', new Date());
map.set('caretPosition', null);
Expand Down Expand Up @@ -547,6 +549,7 @@ export default function compose(state = initialState, action) {
map.set('text', action.raw_text || unescapeHTML(expandMentions(action.status)));
map.set('in_reply_to', action.status.get('in_reply_to_id'));
map.set('privacy', action.status.get('visibility_ex'));
map.set('limited_scope', null);
map.set('media_attachments', action.status.get('media_attachments').map((media) => media.set('unattached', true)));
map.set('focusDate', new Date());
map.set('caretPosition', null);
Expand Down Expand Up @@ -582,6 +585,7 @@ export default function compose(state = initialState, action) {
} else {
map.set('privacy', action.status.get('limited_scope') === 'mutual' ? 'mutual' : 'circle');
}
map.set('limited_scope', action.status.get('limited_scope'));
map.set('media_attachments', action.status.get('media_attachments'));
map.set('focusDate', new Date());
map.set('caretPosition', null);
Expand Down
29 changes: 29 additions & 0 deletions app/javascript/mastodon/utils/mentions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const MENTION_SEPARATORS = '_\\u00b7\\u200c';
const ALPHA = '\\p{L}\\p{M}';
const WORD = '\\p{L}\\p{M}\\p{N}\\p{Pc}';

const buildMentionPatternRegex = () => {
try {
return new RegExp(
`(?:^|[^\\/\\)\\w])@(([${WORD}_][${WORD}${MENTION_SEPARATORS}]*[${ALPHA}${MENTION_SEPARATORS}][${WORD}${MENTION_SEPARATORS}]*[${WORD}_])|([${WORD}_]*[${ALPHA}][${WORD}_]*))`,
'iu',
);
} catch {
return /(?:^|[^/)\w])#(\w*[a-zA-Z·]\w*)/i;
}
};

const buildMentionRegex = () => {
try {
return new RegExp(
`^(([${WORD}_][${WORD}${MENTION_SEPARATORS}]*[${ALPHA}${MENTION_SEPARATORS}][${WORD}${MENTION_SEPARATORS}]*[${WORD}_])|([${WORD}_]*[${ALPHA}][${WORD}_]*))$`,
'iu',
);
} catch {
return /^(\w*[a-zA-Z·]\w*)$/i;
}
};

export const MENTION_PATTERN_REGEX = buildMentionPatternRegex();

export const MENTION_REGEX = buildMentionRegex();

0 comments on commit d75c850

Please sign in to comment.