Add option to rewrite mentions to mentioned user's username or acct

Fixes #666
This commit is contained in:
Thibaut Girka 2019-08-28 22:13:41 +02:00 committed by ThibG
parent 284a1562fb
commit 3282bbe454
5 changed files with 31 additions and 2 deletions

View file

@ -702,6 +702,7 @@ class Status extends ImmutablePureComponent {
parseClick={parseClick} parseClick={parseClick}
disabled={!router} disabled={!router}
tagLinks={settings.get('tag_misleading_links')} tagLinks={settings.get('tag_misleading_links')}
rewriteMentions={settings.get('rewrite_mentions')}
/> />
{!isCollapsed || !(muted || !settings.getIn(['collapsed', 'show_action_bar'])) ? ( {!isCollapsed || !(muted || !settings.getIn(['collapsed', 'show_action_bar'])) ? (
<StatusActionBar <StatusActionBar

View file

@ -67,10 +67,12 @@ export default class StatusContent extends React.PureComponent {
disabled: PropTypes.bool, disabled: PropTypes.bool,
onUpdate: PropTypes.func, onUpdate: PropTypes.func,
tagLinks: PropTypes.bool, tagLinks: PropTypes.bool,
rewriteMentions: PropTypes.string,
}; };
static defaultProps = { static defaultProps = {
tagLinks: true, tagLinks: true,
rewriteMentions: 'no',
}; };
state = { state = {
@ -79,7 +81,7 @@ export default class StatusContent extends React.PureComponent {
_updateStatusLinks () { _updateStatusLinks () {
const node = this.contentsNode; const node = this.contentsNode;
const { tagLinks } = this.props; const { tagLinks, rewriteMentions } = this.props;
if (!node) { if (!node) {
return; return;
@ -99,6 +101,13 @@ export default class StatusContent extends React.PureComponent {
if (mention) { if (mention) {
link.addEventListener('click', this.onMentionClick.bind(this, mention), false); link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
link.setAttribute('title', mention.get('acct')); link.setAttribute('title', mention.get('acct'));
if (rewriteMentions !== 'no') {
while (link.firstChild) link.removeChild(link.firstChild);
link.appendChild(document.createTextNode('@'));
const acctSpan = document.createElement('span');
acctSpan.textContent = rewriteMentions === 'acct' ? mention.get('acct') : mention.get('username');
link.appendChild(acctSpan);
}
} else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) { } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false); link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
} else { } else {
@ -242,6 +251,7 @@ export default class StatusContent extends React.PureComponent {
parseClick, parseClick,
disabled, disabled,
tagLinks, tagLinks,
rewriteMentions,
} = this.props; } = this.props;
const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden; const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
@ -340,7 +350,7 @@ export default class StatusContent extends React.PureComponent {
> >
<div <div
ref={this.setContentsRef} ref={this.setContentsRef}
key={`contents-${tagLinks}`} key={`contents-${tagLinks}-${rewriteMentions}`}
dangerouslySetInnerHTML={content} dangerouslySetInnerHTML={content}
lang={status.get('language')} lang={status.get('language')}
className='status__content__text' className='status__content__text'

View file

@ -25,6 +25,9 @@ const messages = defineMessages({
filters_upstream: { id: 'settings.filtering_behavior.upstream', defaultMessage: 'Show "filtered" like vanilla Mastodon' }, filters_upstream: { id: 'settings.filtering_behavior.upstream', defaultMessage: 'Show "filtered" like vanilla Mastodon' },
filters_hide: { id: 'settings.filtering_behavior.hide', defaultMessage: 'Show "filtered" and add a button to display why' }, filters_hide: { id: 'settings.filtering_behavior.hide', defaultMessage: 'Show "filtered" and add a button to display why' },
filters_cw: { id: 'settings.filtering_behavior.cw', defaultMessage: 'Still display the post, and add filtered words to content warning' }, filters_cw: { id: 'settings.filtering_behavior.cw', defaultMessage: 'Still display the post, and add filtered words to content warning' },
rewrite_mentions_no: { id: 'settings.rewrite_mentions_no', defaultMessage: 'Do not rewrite mentions' },
rewrite_mentions_acct: { id: 'settings.rewrite_mentions_acct', defaultMessage: 'Rewrite with username and domain (when the account is remote)' },
rewrite_mentions_username: { id: 'settings.rewrite_mentions_username', defaultMessage: 'Rewrite with username' },
}); });
@injectIntl @injectIntl
@ -75,6 +78,19 @@ export default class LocalSettingsPage extends React.PureComponent {
<FormattedMessage id='settings.tag_misleading_links' defaultMessage='Tag misleading links' /> <FormattedMessage id='settings.tag_misleading_links' defaultMessage='Tag misleading links' />
<span className='hint'><FormattedMessage id='settings.tag_misleading_links.hint' defaultMessage="Add a visual indication with the link target host to every link not mentioning it explicitly" /></span> <span className='hint'><FormattedMessage id='settings.tag_misleading_links.hint' defaultMessage="Add a visual indication with the link target host to every link not mentioning it explicitly" /></span>
</LocalSettingsPageItem> </LocalSettingsPageItem>
<LocalSettingsPageItem
settings={settings}
item={['rewrite_mentions']}
id='mastodon-settings--rewrite_mentions'
options={[
{ value: 'no', message: intl.formatMessage(messages.rewrite_mentions_no) },
{ value: 'acct', message: intl.formatMessage(messages.rewrite_mentions_acct) },
{ value: 'username', message: intl.formatMessage(messages.rewrite_mentions_username) },
]}
onChange={onChange}
>
<FormattedMessage id='settings.rewrite_mentions' defaultMessage='Rewrite mentions in displayed statuses' />
</LocalSettingsPageItem>
<section> <section>
<h2><FormattedMessage id='settings.notifications_opts' defaultMessage='Notifications options' /></h2> <h2><FormattedMessage id='settings.notifications_opts' defaultMessage='Notifications options' /></h2>
<LocalSettingsPageItem <LocalSettingsPageItem

View file

@ -242,6 +242,7 @@ export default class DetailedStatus extends ImmutablePureComponent {
parseClick={this.parseClick} parseClick={this.parseClick}
onUpdate={this.handleChildUpdate} onUpdate={this.handleChildUpdate}
tagLinks={settings.get('tag_misleading_links')} tagLinks={settings.get('tag_misleading_links')}
rewriteMentions={settings.get('rewrite_mentions')}
disabled disabled
/> />

View file

@ -23,6 +23,7 @@ const initialState = ImmutableMap({
show_content_type_choice: false, show_content_type_choice: false,
filtering_behavior: 'hide', filtering_behavior: 'hide',
tag_misleading_links: true, tag_misleading_links: true,
rewrite_mentions: 'no',
content_warnings : ImmutableMap({ content_warnings : ImmutableMap({
auto_unfold : false, auto_unfold : false,
filter : null, filter : null,