From 8a0e88db8343341e5161cf0437407134b5182bc3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Mar 2023 08:39:41 -0700 Subject: [PATCH] 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"""