Show dms in the right places

This commit is contained in:
Mouse Reeve 2021-02-24 11:59:21 -08:00
parent bcdf2ee142
commit 2a5d4b83d8
9 changed files with 42 additions and 46 deletions

View file

@ -3,7 +3,7 @@
<div class="columns"> <div class="columns">
<div class="column is-narrow"> <div class="column is-narrow">
<a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="large" %}</a> <a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="large" %}</a>
{% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %} {% include 'snippets/stars.html' with rating=book|rating:request.user %}
</div> </div>
<div class="column"> <div class="column">
<h3 class="title is-5"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3> <h3 class="title is-5"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3>

View file

@ -1,9 +1,7 @@
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% if book %} {% if book %}
<a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book %}</a> <a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book %}</a>
{% if ratings %} {% include 'snippets/stars.html' with rating=book|rating:request.user %}
{% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
{% endif %}
<h3 class="title is-6"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3> <h3 class="title is-6"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3>
{% if book.authors %} {% if book.authors %}

View file

@ -106,7 +106,7 @@ class ViewsHelpers(TestCase):
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], privacy=['public', 'unlisted', 'followers'],
following_only=True, following_only=True,
queryset=models.Comment.objects queryset=models.Comment.objects
) )
@ -115,7 +115,7 @@ class ViewsHelpers(TestCase):
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'followers'], privacy=['public', 'followers'],
local_only=True local_only=True
) )
self.assertEqual(len(statuses), 2) self.assertEqual(len(statuses), 2)
@ -128,7 +128,7 @@ class ViewsHelpers(TestCase):
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'followers'], privacy=['public', 'followers'],
) )
self.assertEqual(len(statuses), 3) self.assertEqual(len(statuses), 3)
self.assertEqual(statuses[2], public_status) self.assertEqual(statuses[2], public_status)
@ -137,7 +137,7 @@ class ViewsHelpers(TestCase):
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], privacy=['public', 'unlisted', 'followers'],
following_only=True following_only=True
) )
self.assertEqual(len(statuses), 2) self.assertEqual(len(statuses), 2)
@ -147,7 +147,7 @@ class ViewsHelpers(TestCase):
rat.followers.add(self.local_user) rat.followers.add(self.local_user)
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], privacy=['public', 'unlisted', 'followers'],
following_only=True following_only=True
) )
self.assertEqual(len(statuses), 5) self.assertEqual(len(statuses), 5)
@ -170,18 +170,18 @@ class ViewsHelpers(TestCase):
content='blah blah', user=rat) content='blah blah', user=rat)
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, ['public']) self.local_user, privacy=['public'])
self.assertEqual(len(statuses), 2) self.assertEqual(len(statuses), 2)
# block relationship # block relationship
rat.blocks.add(self.local_user) rat.blocks.add(self.local_user)
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, ['public']) self.local_user, privacy=['public'])
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], public_status) self.assertEqual(statuses[0], public_status)
statuses = views.helpers.get_activity_feed( statuses = views.helpers.get_activity_feed(
rat, ['public']) rat, privacy=['public'])
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], rat_public) self.assertEqual(statuses[0], rat_public)

View file

@ -50,9 +50,7 @@ class Book(View):
) )
# all reviews for the book # all reviews for the book
reviews = get_activity_feed( reviews = get_activity_feed(
request.user, request.user, queryset=reviews
['public', 'unlisted', 'followers', 'direct'],
queryset=reviews
) )
# the reviews to show # the reviews to show

View file

@ -11,8 +11,7 @@ from django.views import View
from bookwyrm import forms, models from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH from bookwyrm.settings import PAGE_LENGTH
from .helpers import get_activity_feed from .helpers import get_activity_feed, get_user_from_username
from .helpers import get_user_from_username
from .helpers import is_api_request, is_bookwyrm_request, object_visible_to_user from .helpers import is_api_request, is_bookwyrm_request, object_visible_to_user
@ -28,15 +27,8 @@ class Feed(View):
page = 1 page = 1
if tab == 'home': if tab == 'home':
activities = get_activity_feed(request.user, following_only=True) activities = get_activity_feed(
# we only want to show private messages if they're related to books request.user, following_only=True, hide_dms=True)
activities = activities.exclude(
review__isnull=True,
comment__isnull=True,
quotation__isnull=True,
generatednote__isnull=True,
privacy='direct'
)
elif tab == 'local': elif tab == 'local':
activities = get_activity_feed( activities = get_activity_feed(
request.user, privacy=['public', 'followers'], local_only=True) request.user, privacy=['public', 'followers'], local_only=True)

View file

@ -1,6 +1,7 @@
''' helper functions used in various views ''' ''' helper functions used in various views '''
import re import re
from requests import HTTPError from requests import HTTPError
from django.core.exceptions import FieldError
from django.db.models import Q from django.db.models import Q
from bookwyrm import activitypub, models from bookwyrm import activitypub, models
@ -98,17 +99,23 @@ def privacy_filter(viewer, queryset, privacy_levels=None, following_only=False):
# exclude direct messages not intended for the user # exclude direct messages not intended for the user
if 'direct' in privacy_levels: if 'direct' in privacy_levels:
try:
queryset = queryset.exclude( queryset = queryset.exclude(
~Q( ~Q(
Q(user=viewer) | Q(mention_users=viewer) Q(user=viewer) | Q(mention_users=viewer)
), privacy='direct' ), privacy='direct'
) )
except FieldError:
queryset = queryset.exclude(
~Q(user=viewer), privacy='direct'
)
return queryset return queryset
def get_activity_feed( def get_activity_feed(
user, privacy=None, local_only=False, following_only=False, user, privacy=None, local_only=False, following_only=False,
queryset=None): queryset=None, hide_dms=False):
''' get a filtered queryset of statuses ''' ''' get a filtered queryset of statuses '''
if not queryset: if not queryset:
queryset = models.Status.objects.select_subclasses() queryset = models.Status.objects.select_subclasses()
@ -120,6 +127,16 @@ def get_activity_feed(
queryset = privacy_filter( queryset = privacy_filter(
user, queryset, privacy, following_only=following_only) user, queryset, privacy, following_only=following_only)
if hide_dms:
# dms are direct statuses not related to books
queryset = queryset.exclude(
review__isnull=True,
comment__isnull=True,
quotation__isnull=True,
generatednote__isnull=True,
privacy='direct'
)
# filter for only local status # filter for only local status
if local_only: if local_only:
queryset = queryset.filter(user__local=True) queryset = queryset.filter(user__local=True)

View file

@ -1,11 +1,10 @@
''' non-interactive pages ''' ''' non-interactive pages '''
from django.db.models import Avg, Max from django.db.models import Max
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.views import View from django.views import View
from bookwyrm import forms, models from bookwyrm import forms, models
from .feed import Feed from .feed import Feed
from .helpers import get_activity_feed
# pylint: disable= no-self-use # pylint: disable= no-self-use
@ -34,6 +33,7 @@ class Discover(View):
''' tiled book activity page ''' ''' tiled book activity page '''
books = models.Edition.objects.filter( books = models.Edition.objects.filter(
review__published_date__isnull=False, review__published_date__isnull=False,
review__deleted=False,
review__user__local=True, review__user__local=True,
review__privacy__in=['public', 'unlisted'], review__privacy__in=['public', 'unlisted'],
).exclude( ).exclude(
@ -42,18 +42,9 @@ class Discover(View):
Max('review__published_date') Max('review__published_date')
).order_by('-review__published_date__max')[:6] ).order_by('-review__published_date__max')[:6]
ratings = {}
for book in books:
reviews = models.Review.objects.filter(
book__in=book.parent_work.editions.all()
)
reviews = get_activity_feed(
request.user, ['public', 'unlisted'], queryset=reviews)
ratings[book.id] = reviews.aggregate(Avg('rating'))['rating__avg']
data = { data = {
'title': 'Discover', 'title': 'Discover',
'register_form': forms.RegisterForm(), 'register_form': forms.RegisterForm(),
'books': list(set(books)), 'books': list(set(books)),
'ratings': ratings
} }
return TemplateResponse(request, 'discover/discover.html', data) return TemplateResponse(request, 'discover/discover.html', data)

View file

@ -27,7 +27,7 @@ class RssFeed(Feed):
def items(self, obj): def items(self, obj):
''' the user's activity feed ''' ''' the user's activity feed '''
return get_activity_feed( return get_activity_feed(
obj, ['public', 'unlisted'], queryset=obj.status_set) obj, privacy=['public', 'unlisted'], queryset=obj.status_set)
def item_link(self, item): def item_link(self, item):

View file

@ -71,8 +71,8 @@ class User(View):
# user's posts # user's posts
activities = get_activity_feed( activities = get_activity_feed(
request.user, request.user,
['public', 'unlisted', 'followers'], queryset=user.status_set.select_subclasses(),
queryset=user.status_set hide_dms=True
) )
paginated = Paginator(activities, PAGE_LENGTH) paginated = Paginator(activities, PAGE_LENGTH)
goal = models.AnnualGoal.objects.filter( goal = models.AnnualGoal.objects.filter(