From 42dd19908460557c1f5c362e09ed86c1d026681c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 08:28:45 -0700 Subject: [PATCH 1/6] Unit test that identifies clashes between links and mentions --- bookwyrm/tests/views/test_status.py | 10 ++++++++++ bookwyrm/views/status.py | 23 ++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index d02c71374..6863c9625 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -456,6 +456,16 @@ http://www.fish.com/""" views.status.format_links(url), f'{url[8:]}' ) + def test_format_links_with_at_symbol(self, *_): + """A link with an @username shouldn't treat the username as a mention""" + content = "a link to https://www.example.com/user/@mouse" + mentions = views.status.find_mentions(self.local_user, content) + # pylint: disable=line-too-long + self.assertEqual( + views.status.format_mentions(content, mentions), + 'a link to www.example.com/user/@mouse' + ) + def test_to_markdown(self, *_): """this is mostly handled in other places, but nonetheless""" text = "_hi_ and http://fish.com is rad" diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index 82d033d7c..866657a3b 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -96,18 +96,12 @@ class CreateStatus(View): # inspect the text for user tags content = status.content - for (mention_text, mention_user) in find_mentions( - request.user, content - ).items(): + mentions = find_mentions(request.user, content) + for (_, mention_user) in mentions: # add them to status mentions fk status.mention_users.add(mention_user) + content = format_mentions(content, mentions) - # turn the mention into a link - content = re.sub( - rf"{mention_text}\b(?!@)", - rf'{mention_text}', - content, - ) # add reply parent to mentions if status.reply_parent: status.mention_users.add(status.reply_parent.user) @@ -149,6 +143,17 @@ class CreateStatus(View): return HttpResponse() return redirect_to_referer(request) +def format_mentions(content, mentions): + """Detect @mentions and make them links""" + for (mention_text, mention_user) in mentions.items(): + # turn the mention into a link + content = re.sub( + rf"{mention_text}\b(?!@)", + rf'{mention_text}', + content, + ) + return content + @method_decorator(login_required, name="dispatch") class DeleteStatus(View): From 8a0e88db8343341e5161cf0437407134b5182bc3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 08:39:41 -0700 Subject: [PATCH 2/6] Adds test for links with # symbols --- bookwyrm/tests/views/test_status.py | 16 +++++++++++++--- bookwyrm/views/status.py | 26 +++++++++++++++++--------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 6863c9625..1ec148991 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -456,14 +456,24 @@ http://www.fish.com/""" views.status.format_links(url), f'{url[8:]}' ) - def test_format_links_with_at_symbol(self, *_): + def test_format_mentions_with_at_symbol_links(self, *_): """A link with an @username shouldn't treat the username as a mention""" - content = "a link to https://www.example.com/user/@mouse" + content = "a link to https://example.com/user/@mouse" mentions = views.status.find_mentions(self.local_user, content) # pylint: disable=line-too-long self.assertEqual( views.status.format_mentions(content, mentions), - 'a link to www.example.com/user/@mouse' + 'a link to www.example.com/user/@mouse', + ) + + def test_format_hashtag_with_pound_symbol_links(self, *_): + """A link with an @username shouldn't treat the username as a mention""" + content = "a link to https://example.com/page#anchor" + hashtags = views.status.find_or_create_hashtags(content) + # pylint: disable=line-too-long + self.assertEqual( + views.status.format_hashtags(content, hashtags), + 'a link to example.com/page#anchor', ) def test_to_markdown(self, *_): diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index 866657a3b..0e4d4e396 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -107,17 +107,11 @@ class CreateStatus(View): status.mention_users.add(status.reply_parent.user) # inspect the text for hashtags - for (mention_text, mention_hashtag) in find_or_create_hashtags(content).items(): + hashtags = find_or_create_hashtags(content) + for (_, mention_hashtag) in hashtags.items(): # add them to status mentions fk status.mention_hashtags.add(mention_hashtag) - - # turn the mention into a link - content = re.sub( - rf"{mention_text}\b(?!@)", - rf'' - + rf"{mention_text}", - content, - ) + content = format_hashtags(content, hashtags) # deduplicate mentions status.mention_users.set(set(status.mention_users.all())) @@ -143,6 +137,7 @@ class CreateStatus(View): return HttpResponse() return redirect_to_referer(request) + def format_mentions(content, mentions): """Detect @mentions and make them links""" for (mention_text, mention_user) in mentions.items(): @@ -155,6 +150,19 @@ def format_mentions(content, mentions): return content +def format_hashtags(content, hashtags): + """Detect #hashtags and make them links""" + for (mention_text, mention_hashtag) in hashtags.items(): + # turn the mention into a link + content = re.sub( + rf"{mention_text}\b(?!@)", + rf'' + + rf"{mention_text}", + content, + ) + return content + + @method_decorator(login_required, name="dispatch") class DeleteStatus(View): """tombstone that bad boy""" From 4fcb01805ef18eb9f4290acbe44acef48c1059d8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 09:14:30 -0700 Subject: [PATCH 3/6] Fixes test comparison string --- bookwyrm/tests/views/test_status.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 1ec148991..71b3eaacc 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -460,20 +460,18 @@ http://www.fish.com/""" """A link with an @username shouldn't treat the username as a mention""" content = "a link to https://example.com/user/@mouse" mentions = views.status.find_mentions(self.local_user, content) - # pylint: disable=line-too-long self.assertEqual( views.status.format_mentions(content, mentions), - 'a link to www.example.com/user/@mouse', + 'a link to https://example.com/user/@mouse', ) def test_format_hashtag_with_pound_symbol_links(self, *_): """A link with an @username shouldn't treat the username as a mention""" content = "a link to https://example.com/page#anchor" hashtags = views.status.find_or_create_hashtags(content) - # pylint: disable=line-too-long self.assertEqual( views.status.format_hashtags(content, hashtags), - 'a link to example.com/page#anchor', + 'a link to https://example.com/page#anchor', ) def test_to_markdown(self, *_): From fe856bcf2c69d5c806bb78a26ac5c5f53fedb5d9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 09:20:58 -0700 Subject: [PATCH 4/6] Updates regex on mentions and hashtags --- bookwyrm/views/status.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index 0e4d4e396..3c5b7bcf3 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -143,7 +143,7 @@ def format_mentions(content, mentions): for (mention_text, mention_user) in mentions.items(): # turn the mention into a link content = re.sub( - rf"{mention_text}\b(?!@)", + rf"(?{mention_text}', content, ) @@ -155,7 +155,7 @@ def format_hashtags(content, hashtags): for (mention_text, mention_hashtag) in hashtags.items(): # turn the mention into a link content = re.sub( - rf"{mention_text}\b(?!@)", + rf"(?' + rf"{mention_text}", content, From f1640399e3c3f1a08eb5755dc1f8c6d21fad3949 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 10:12:25 -0700 Subject: [PATCH 5/6] Python formatting --- bookwyrm/tests/views/test_status.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 71b3eaacc..7c64fdb0c 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -462,7 +462,7 @@ http://www.fish.com/""" mentions = views.status.find_mentions(self.local_user, content) self.assertEqual( views.status.format_mentions(content, mentions), - 'a link to https://example.com/user/@mouse', + "a link to https://example.com/user/@mouse", ) def test_format_hashtag_with_pound_symbol_links(self, *_): @@ -471,7 +471,7 @@ http://www.fish.com/""" hashtags = views.status.find_or_create_hashtags(content) self.assertEqual( views.status.format_hashtags(content, hashtags), - 'a link to https://example.com/page#anchor', + "a link to https://example.com/page#anchor", ) def test_to_markdown(self, *_): From b76da26c983f519033b610b3a2cc4b62e2db11c9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 10:13:02 -0700 Subject: [PATCH 6/6] Fixes iteration over mentions dict --- bookwyrm/views/status.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index 3c5b7bcf3..a8c874c13 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -97,7 +97,7 @@ class CreateStatus(View): # inspect the text for user tags content = status.content mentions = find_mentions(request.user, content) - for (_, mention_user) in mentions: + for (_, mention_user) in mentions.items(): # add them to status mentions fk status.mention_users.add(mention_user) content = format_mentions(content, mentions)