Link hashtags in status content

This commit is contained in:
Christof Dorner 2022-12-17 21:00:32 +01:00
parent c68304a99b
commit ba0fcccfc5
2 changed files with 16 additions and 5 deletions

View file

@ -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'<p>this is an <a href="{hashtag_exising.remote_id}">#existing</a> '
+ f'hashtag, this one is <a href="{hashtag_new.remote_id}">#new</a>.</p>',
)
def test_find_hashtags(self, *_):
"""detect and look up #hashtags"""

View file

@ -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'<a href="{mention_hashtag.remote_id}">{mention_text}</a>',
content,
)
# deduplicate mentions
status.mention_users.set(set(status.mention_users.all()))