Link hashtags in status content
This commit is contained in:
parent
c68304a99b
commit
ba0fcccfc5
2 changed files with 16 additions and 5 deletions
|
@ -339,7 +339,7 @@ class StatusViews(TestCase):
|
||||||
view = views.CreateStatus.as_view()
|
view = views.CreateStatus.as_view()
|
||||||
form = forms.CommentForm(
|
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,
|
"user": self.local_user.id,
|
||||||
"book": self.book.id,
|
"book": self.book.id,
|
||||||
"privacy": "public",
|
"privacy": "public",
|
||||||
|
@ -354,7 +354,14 @@ class StatusViews(TestCase):
|
||||||
hashtags = models.Hashtag.objects.all()
|
hashtags = models.Hashtag.objects.all()
|
||||||
self.assertEqual(len(hashtags), 2)
|
self.assertEqual(len(hashtags), 2)
|
||||||
self.assertEqual(list(status.mention_hashtags.all()), list(hashtags))
|
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, *_):
|
def test_find_hashtags(self, *_):
|
||||||
"""detect and look up #hashtags"""
|
"""detect and look up #hashtags"""
|
||||||
|
|
|
@ -116,12 +116,16 @@ class CreateStatus(View):
|
||||||
status.mention_users.add(status.reply_parent.user)
|
status.mention_users.add(status.reply_parent.user)
|
||||||
|
|
||||||
# inspect the text for hashtags
|
# 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
|
# add them to status mentions fk
|
||||||
status.mention_hashtags.add(mention_hashtag)
|
status.mention_hashtags.add(mention_hashtag)
|
||||||
|
|
||||||
# TODO: turn the mention into a link
|
# turn the mention into a link
|
||||||
content = content
|
content = re.sub(
|
||||||
|
rf"{mention_text}\b(?!@)",
|
||||||
|
rf'<a href="{mention_hashtag.remote_id}">{mention_text}</a>',
|
||||||
|
content,
|
||||||
|
)
|
||||||
|
|
||||||
# deduplicate mentions
|
# deduplicate mentions
|
||||||
status.mention_users.set(set(status.mention_users.all()))
|
status.mention_users.set(set(status.mention_users.all()))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue