From ab398758a9d9633f7015be15e9467af798f2faf4 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Sat, 24 Dec 2022 23:04:25 -0500 Subject: [PATCH] Fixed mention linking with mixed case usernames (#265) --- core/html.py | 18 ++++++++++-------- tests/activities/models/test_post.py | 8 ++++---- tests/core/test_html.py | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/core/html.py b/core/html.py index 6cae737..3c48df4 100644 --- a/core/html.py +++ b/core/html.py @@ -139,24 +139,26 @@ class ContentRenderer: url = str(mention.urls.view) else: url = mention.absolute_profile_uri() - possible_matches[mention.username] = url - possible_matches[f"{mention.username}@{mention.domain_id}"] = url + possible_matches[mention.username.lower()] = url + possible_matches[f"{mention.username.lower()}@{mention.domain_id}"] = url collapse_name: dict[str, str] = {} def replacer(match): precursor = match.group(1) - handle = match.group(2).lower() + handle = match.group(2) if "@" in handle: short_handle = handle.split("@", 1)[0] else: short_handle = handle - if handle in possible_matches: - if short_handle not in collapse_name: - collapse_name[short_handle] = handle - elif collapse_name.get(short_handle) != handle: + handle_hash = handle.lower() + short_hash = short_handle.lower() + if handle_hash in possible_matches: + if short_hash not in collapse_name: + collapse_name[short_hash] = handle_hash + elif collapse_name.get(short_hash) != handle_hash: short_handle = handle - return f'{precursor}@{short_handle}' + return f'{precursor}@{short_handle}' else: return match.group() diff --git a/tests/activities/models/test_post.py b/tests/activities/models/test_post.py index 48877fa..c50f8eb 100644 --- a/tests/activities/models/test_post.py +++ b/tests/activities/models/test_post.py @@ -87,7 +87,7 @@ def test_linkify_mentions_remote( post.mentions.add(remote_identity) assert ( post.safe_content_remote() - == '

Hey @test

' + == '

Hey @TeSt

' ) # Test trailing dot (remote) @@ -110,14 +110,14 @@ def test_linkify_mentions_remote( ) post.mentions.set([remote_identity, remote_identity2]) assert post.safe_content_remote() == ( - '

Hey @test ' + '

Hey @TeSt ' 'and @test@remote2.test

' ) post.content = "

Hey @TeSt, @Test@remote.test and @test

" assert post.safe_content_remote() == ( - '

Hey @test, ' - '@test@remote.test ' + '

Hey @TeSt, ' + '@Test@remote.test ' 'and @test

' ) diff --git a/tests/core/test_html.py b/tests/core/test_html.py index 7ebf7eb..5589105 100644 --- a/tests/core/test_html.py +++ b/tests/core/test_html.py @@ -62,3 +62,28 @@ def test_link_preservation(): ) == 'Hello @andrew, I want to link to this #hashtag: here and rewrite #thishashtag' ) + + +@pytest.mark.django_db +def test_link_mixcase_mentions(): + renderer = ContentRenderer(local=True) + fake_mention = Mock() + fake_mention.username = "Manfre" + fake_mention.domain_id = "manfre.net" + fake_mention.urls.view = "/@Manfre@manfre.net/" + fake_mention2 = Mock() + fake_mention2.username = "manfre" + fake_mention2.domain_id = "takahe.social" + fake_mention2.urls.view = "https://takahe.social/@manfre@takahe.social/" + fake_post = Mock() + fake_post.mentions.all.return_value = [fake_mention, fake_mention2] + fake_post.author.domain.uri_domain = "example.com" + fake_post.emojis.all.return_value = [] + + assert ( + renderer.render_post( + "@Manfre@manfre.net @mAnFrE@takahe.social @manfre@manfre.net", + fake_post, + ) + == '@Manfre @mAnFrE@takahe.social @manfre' + )