mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-23 08:36:32 +00:00
Add basic view listing activities from a given hashtag
This commit is contained in:
parent
11640f986e
commit
499aace9fb
5 changed files with 293 additions and 0 deletions
32
bookwyrm/templates/hashtag.html
Normal file
32
bookwyrm/templates/hashtag.html
Normal file
|
@ -0,0 +1,32 @@
|
|||
{% extends "layout.html" %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block title %}{{ hashtag }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container is-max-desktop">
|
||||
<section class="block">
|
||||
<header class="block content has-text-centered">
|
||||
<h1 class="title">{{ hashtag }}</h1>
|
||||
<p class="subtitle">
|
||||
{% blocktrans trimmed with site_name=site.name %}
|
||||
See tagged statuses in the local {{ site_name }} community
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
</header>
|
||||
|
||||
{% for activity in activities %}
|
||||
<div class="block">
|
||||
{% include 'snippets/status/status.html' with status=activity %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% if not activities %}
|
||||
<div class="block">
|
||||
<p>{% trans "No activities for this hashtag yet!" %}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% include 'snippets/pagination.html' with page=activities path=path %}
|
||||
</section>
|
||||
</div>
|
||||
{% endblock %}
|
197
bookwyrm/tests/views/test_hashtag.py
Normal file
197
bookwyrm/tests/views/test_hashtag.py
Normal 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)
|
|
@ -356,6 +356,15 @@ urlpatterns = [
|
|||
name="notifications",
|
||||
),
|
||||
re_path(r"^directory/?", views.Directory.as_view(), name="directory"),
|
||||
# hashtag
|
||||
re_path(
|
||||
r"^hashtag/(?P<hashtag_id>\d+)/?$", views.Hashtag.as_view(), name="hashtag"
|
||||
),
|
||||
re_path(
|
||||
rf"^hashtag/(?P<hashtag_id>\d+){regex.SLUG}/?$",
|
||||
views.Hashtag.as_view(),
|
||||
name="hashtag",
|
||||
),
|
||||
# Get started
|
||||
re_path(
|
||||
r"^get-started/profile/?$",
|
||||
|
|
|
@ -130,6 +130,7 @@ from .group import (
|
|||
accept_membership,
|
||||
reject_membership,
|
||||
)
|
||||
from .hashtag import Hashtag
|
||||
from .inbox import Inbox
|
||||
from .interaction import Favorite, Unfavorite, Boost, Unboost
|
||||
from .isbn import Isbn
|
||||
|
|
54
bookwyrm/views/hashtag.py
Normal file
54
bookwyrm/views/hashtag.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
""" listing statuses for a given hashtag """
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Q
|
||||
from django.views import View
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from bookwyrm.views.helpers import maybe_redirect_local_path
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Hashtag(View):
|
||||
"""listing statuses for a given hashtag"""
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def get(self, request, hashtag_id, slug=None):
|
||||
"""show hashtag with related statuses"""
|
||||
hashtag = get_object_or_404(models.Hashtag, id=hashtag_id)
|
||||
|
||||
if redirect_local_path := maybe_redirect_local_path(request, hashtag):
|
||||
return redirect_local_path
|
||||
|
||||
activities = (
|
||||
models.Status.privacy_filter(
|
||||
request.user,
|
||||
)
|
||||
.filter(
|
||||
Q(mention_hashtags=hashtag),
|
||||
)
|
||||
.exclude(
|
||||
privacy__in=["direct", "unlisted"],
|
||||
)
|
||||
.select_related(
|
||||
"user",
|
||||
"reply_parent",
|
||||
"review__book",
|
||||
"comment__book",
|
||||
"quotation__book",
|
||||
)
|
||||
.prefetch_related(
|
||||
"mention_books",
|
||||
"mention_users",
|
||||
"attachments",
|
||||
)
|
||||
)
|
||||
paginated = Paginator(activities, PAGE_LENGTH)
|
||||
|
||||
data = {
|
||||
"hashtag": hashtag.name,
|
||||
"activities": paginated.get_page(request.GET.get("page", 1)),
|
||||
}
|
||||
return TemplateResponse(request, "hashtag.html", data)
|
Loading…
Reference in a new issue