From ba0fcccfc5e1ee88d8a249436695b1d1d5a526ec Mon Sep 17 00:00:00 2001 From: Christof Dorner Date: Sat, 17 Dec 2022 21:00:32 +0100 Subject: [PATCH] Link hashtags in status content --- bookwyrm/tests/views/test_status.py | 11 +++++++++-- bookwyrm/views/status.py | 10 +++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 2ef20935b..b1b448180 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -339,7 +339,7 @@ class StatusViews(TestCase): view = views.CreateStatus.as_view() form = forms.CommentForm( { - "content": "this is an #existing hashtag, this is a #new hashtag", + "content": "this is an #existing hashtag, this one is #new.", "user": self.local_user.id, "book": self.book.id, "privacy": "public", @@ -354,7 +354,14 @@ class StatusViews(TestCase): hashtags = models.Hashtag.objects.all() self.assertEqual(len(hashtags), 2) self.assertEqual(list(status.mention_hashtags.all()), list(hashtags)) - # TODO: assert tag is linked to a page listing all statuses by tag + + hashtag_exising = models.Hashtag.objects.filter(name="#existing").first() + hashtag_new = models.Hashtag.objects.filter(name="#new").first() + self.assertEqual( + status.content, + f'

this is an #existing ' + + f'hashtag, this one is #new.

', + ) def test_find_hashtags(self, *_): """detect and look up #hashtags""" diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index b220c1240..d7ecc5aa4 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -116,12 +116,16 @@ class CreateStatus(View): status.mention_users.add(status.reply_parent.user) # inspect the text for hashtags - for (tag, mention_hashtag) in find_hashtags(content).items(): + for (mention_text, mention_hashtag) in find_hashtags(content).items(): # add them to status mentions fk status.mention_hashtags.add(mention_hashtag) - # TODO: turn the mention into a link - content = content + # turn the mention into a link + content = re.sub( + rf"{mention_text}\b(?!@)", + rf'{mention_text}', + content, + ) # deduplicate mentions status.mention_users.set(set(status.mention_users.all()))