From 1b909c98abeaa206953225cf5d3e46b5db214ec5 Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Mon, 10 Jul 2023 21:38:00 +0200 Subject: [PATCH 1/4] Use TwitterText regex for valid domains in mentions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TwitterText already has a regex to check for valid domains that allows a few domains the current Regex did not allow. Mainly it allows mentions of accounts on domains with emoji in their domain names to be processed correctly. Mentions like `@test@🌈🌈🌈.st` will now work correctly. --- app/models/account.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/account.rb b/app/models/account.rb index 206529301e4..55ef08c5506 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -71,7 +71,7 @@ class Account < ApplicationRecord INSTANCE_ACTOR_ID = -99 USERNAME_RE = /[a-z0-9_]+([.-]+[a-z0-9_]+)*/i - MENTION_RE = %r{(? Date: Tue, 11 Jul 2023 09:19:49 +0200 Subject: [PATCH 2/4] Add a test for unicode domain name mentions --- spec/models/account_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 77341b77864..27e51b0e0ea 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -665,6 +665,10 @@ RSpec.describe Account do expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com' end + it 'matches full usernames with unicode domain names' do + expect(subject.match('@alice@🌈🌈🌈.st')[1]).to eq 'alice@🌈🌈🌈.st' + end + it 'matches full usernames with a dot at the end' do expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com' end From fd4c869f634c8129b11c3c519dac69ca58e191df Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Sun, 3 Sep 2023 21:46:04 +0200 Subject: [PATCH 3/4] Replace the domain part of the mention regex with twitter's validDomain --- .../mastodon/features/compose/util/counter.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/javascript/mastodon/features/compose/util/counter.js b/app/javascript/mastodon/features/compose/util/counter.js index ec2431096b5..8ac2f937ee4 100644 --- a/app/javascript/mastodon/features/compose/util/counter.js +++ b/app/javascript/mastodon/features/compose/util/counter.js @@ -1,9 +1,20 @@ +import regexSupplant from 'twitter-text/dist/lib/regexSupplant'; +import validDomain from 'twitter-text/dist/regexp/validDomain'; + import { urlRegex } from './url_regex'; const urlPlaceholder = '$2xxxxxxxxxxxxxxxxxxxxxxx'; +const validMention = regexSupplant( + '(^|[^/\\w])@(([a-z0-9_]+)@(#{validDomain}))', + { + validDomain, + }, + 'ig' +) + export function countableText(inputText) { return inputText .replace(urlRegex, urlPlaceholder) - .replace(/(^|[^/\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)/ig, '$1@$3'); + .replace(validMention, '$1@$3'); } From efffde02302018363049a42f3e53b41a772a3f3a Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Thu, 9 Nov 2023 23:06:18 +0100 Subject: [PATCH 4/4] Add semicolon --- app/javascript/mastodon/features/compose/util/counter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/mastodon/features/compose/util/counter.js b/app/javascript/mastodon/features/compose/util/counter.js index 8ac2f937ee4..e59f9c16448 100644 --- a/app/javascript/mastodon/features/compose/util/counter.js +++ b/app/javascript/mastodon/features/compose/util/counter.js @@ -11,7 +11,7 @@ const validMention = regexSupplant( validDomain, }, 'ig' -) +); export function countableText(inputText) { return inputText