1
0
Fork 0

Merge pull request #2524 from chdorner/feature/tag-support

Initial hashtag support
This commit is contained in:
Mouse Reeve 2023-03-12 16:37:39 -07:00 committed by GitHub
commit 12af5992a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 635 additions and 13 deletions

View file

@ -0,0 +1,197 @@
""" tests for hashtag view """
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.tests.validate_html import validate_html
class HashtagView(TestCase):
"""hashtag view"""
def setUp(self):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
self.follower_user = models.User.objects.create_user(
"follower@local.com",
"follower@email.com",
"followerword",
local=True,
localname="follower",
remote_id="https://example.com/users/follower",
)
self.local_user.followers.add(self.follower_user)
self.other_user = models.User.objects.create_user(
"other@local.com",
"other@email.com",
"otherword",
local=True,
localname="other",
remote_id="https://example.com/users/other",
)
self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=self.work,
)
self.hashtag_bookclub = models.Hashtag.objects.create(name="#BookClub")
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.activitystreams.add_status_task.delay"):
self.statuses_bookclub = [
models.Comment.objects.create(
book=self.book, user=self.local_user, content="#BookClub"
),
]
for status in self.statuses_bookclub:
status.mention_hashtags.add(self.hashtag_bookclub)
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
models.SiteSettings.objects.create()
def test_hashtag_page(self):
"""just make sure it loads"""
view = views.Hashtag.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request, self.hashtag_bookclub.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["activities"]), 1)
def test_privacy_direct(self):
"""ensure statuses with privacy set to direct are always filtered out"""
view = views.Hashtag.as_view()
request = self.factory.get("")
hashtag = models.Hashtag.objects.create(name="#test")
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.activitystreams.add_status_task.delay"):
status = models.Comment.objects.create(
user=self.local_user, book=self.book, content="#test", privacy="direct"
)
status.mention_hashtags.add(hashtag)
for user in [
self.local_user,
self.follower_user,
self.other_user,
self.anonymous_user,
]:
request.user = user
result = view(request, hashtag.id)
self.assertNotIn(status, result.context_data["activities"])
def test_privacy_unlisted(self):
"""ensure statuses with privacy set to unlisted are always filtered out"""
view = views.Hashtag.as_view()
request = self.factory.get("")
hashtag = models.Hashtag.objects.create(name="#test")
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.activitystreams.add_status_task.delay"):
status = models.Comment.objects.create(
user=self.local_user,
book=self.book,
content="#test",
privacy="unlisted",
)
status.mention_hashtags.add(hashtag)
for user in [
self.local_user,
self.follower_user,
self.other_user,
self.anonymous_user,
]:
request.user = user
result = view(request, hashtag.id)
self.assertNotIn(status, result.context_data["activities"])
def test_privacy_following(self):
"""ensure only creator and followers can see statuses with privacy
set to followers"""
view = views.Hashtag.as_view()
request = self.factory.get("")
hashtag = models.Hashtag.objects.create(name="#test")
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.activitystreams.add_status_task.delay"):
status = models.Comment.objects.create(
user=self.local_user,
book=self.book,
content="#test",
privacy="followers",
)
status.mention_hashtags.add(hashtag)
for user in [self.local_user, self.follower_user]:
request.user = user
result = view(request, hashtag.id)
self.assertIn(status, result.context_data["activities"])
for user in [self.other_user, self.anonymous_user]:
request.user = user
result = view(request, hashtag.id)
self.assertNotIn(status, result.context_data["activities"])
def test_not_found(self):
"""make sure 404 is rendered"""
view = views.Hashtag.as_view()
request = self.factory.get("")
request.user = self.local_user
with self.assertRaises(Http404):
view(request, 42)
def test_empty(self):
"""hashtag without any statuses should still render"""
view = views.Hashtag.as_view()
request = self.factory.get("")
request.user = self.local_user
hashtag_empty = models.Hashtag.objects.create(name="#empty")
result = view(request, hashtag_empty.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["activities"]), 0)
def test_logged_out(self):
"""make sure it loads all activities"""
view = views.Hashtag.as_view()
request = self.factory.get("")
request.user = self.anonymous_user
result = view(request, self.hashtag_bookclub.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(len(result.context_data["activities"]), 1)

View file

@ -6,7 +6,7 @@ from django.test import TestCase, TransactionTestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.views.status import find_mentions
from bookwyrm.views.status import find_mentions, find_or_create_hashtags
from bookwyrm.settings import DOMAIN
from bookwyrm.tests.validate_html import validate_html
@ -95,6 +95,7 @@ class StatusViews(TestCase):
local=True,
localname="nutria",
)
self.existing_hashtag = models.Hashtag.objects.create(name="#existing")
with patch("bookwyrm.models.user.set_remote_server"):
self.remote_user = models.User.objects.create_user(
"rat",
@ -333,6 +334,71 @@ class StatusViews(TestCase):
result = find_mentions(self.local_user, "@beep@beep.com")
self.assertEqual(result, {})
def test_create_status_hashtags(self, *_):
"""#mention a hashtag in a post"""
view = views.CreateStatus.as_view()
form = forms.CommentForm(
{
"content": "this is an #EXISTING hashtag but all uppercase, "
+ "this one is #NewTag.",
"user": self.local_user.id,
"book": self.book.id,
"privacy": "public",
}
)
request = self.factory.post("", form.data)
request.user = self.local_user
view(request, "comment")
status = models.Status.objects.get()
hashtags = models.Hashtag.objects.all()
self.assertEqual(len(hashtags), 2)
self.assertEqual(list(status.mention_hashtags.all()), list(hashtags))
hashtag_exising = models.Hashtag.objects.filter(name="#existing").first()
hashtag_new = models.Hashtag.objects.filter(name="#NewTag").first()
self.assertEqual(
status.content,
"<p>this is an "
+ f'<a href="{hashtag_exising.remote_id}" data-mention="hashtag">'
+ "#EXISTING</a> hashtag but all uppercase, this one is "
+ f'<a href="{hashtag_new.remote_id}" data-mention="hashtag">'
+ "#NewTag</a>.</p>",
)
def test_find_or_create_hashtags(self, *_):
"""detect and look up #hashtags"""
result = find_or_create_hashtags("no hashtag to be found here")
self.assertEqual(result, {})
result = find_or_create_hashtags("#existing")
self.assertEqual(result["#existing"], self.existing_hashtag)
result = find_or_create_hashtags("leading text #existing")
self.assertEqual(result["#existing"], self.existing_hashtag)
result = find_or_create_hashtags("leading #existing trailing")
self.assertEqual(result["#existing"], self.existing_hashtag)
self.assertIsNone(models.Hashtag.objects.filter(name="new").first())
result = find_or_create_hashtags("leading #new trailing")
new_hashtag = models.Hashtag.objects.filter(name="#new").first()
self.assertIsNotNone(new_hashtag)
self.assertEqual(result["#new"], new_hashtag)
result = find_or_create_hashtags("leading #existing #new trailing")
self.assertEqual(result["#existing"], self.existing_hashtag)
self.assertEqual(result["#new"], new_hashtag)
result = find_or_create_hashtags("#Braunbär")
hashtag = models.Hashtag.objects.filter(name="#Braunbär").first()
self.assertEqual(result["#Braunbär"], hashtag)
result = find_or_create_hashtags("#ひぐま")
hashtag = models.Hashtag.objects.filter(name="#ひぐま").first()
self.assertEqual(result["#ひぐま"], hashtag)
def test_format_links_simple_url(self, *_):
"""find and format urls into a tags"""
url = "http://www.fish.com/"