From 7caaddbb222d6bab129a23786b25e6b804cb5f0b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 15:20:35 -0700 Subject: [PATCH 1/6] Get thread children with depth first recusive search --- bookwyrm/templates/feed/status.html | 14 +++++++++++++- bookwyrm/templates/feed/thread.html | 25 ------------------------- bookwyrm/views/feed.py | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 26 deletions(-) delete mode 100644 bookwyrm/templates/feed/thread.html diff --git a/bookwyrm/templates/feed/status.html b/bookwyrm/templates/feed/status.html index 903ca7907..429c657d0 100644 --- a/bookwyrm/templates/feed/status.html +++ b/bookwyrm/templates/feed/status.html @@ -9,7 +9,19 @@ -{% include 'feed/thread.html' with status=status depth=0 max_depth=6 is_root=True direction=0 %} +
+
+
+ {% include 'snippets/status/status.html' with status=status main=True %} +
+ + {% for child in children %} +
+ {% include 'snippets/status/status.html' with status=child %} +
+ {% endfor %} +
+
{% endblock %} diff --git a/bookwyrm/templates/feed/thread.html b/bookwyrm/templates/feed/thread.html deleted file mode 100644 index c1b624e3c..000000000 --- a/bookwyrm/templates/feed/thread.html +++ /dev/null @@ -1,25 +0,0 @@ -{% load status_display %} - -
-
-{% with depth=depth|add:1 %} - {% if depth <= max_depth and status.reply_parent and direction <= 0 %} - {% with direction=-1 %} - {% include 'feed/thread.html' with status=status|parent is_root=False %} - {% endwith %} - {% endif %} - - - {% include 'snippets/status/status.html' with status=status main=is_root %} -
- -{% if depth <= max_depth and direction >= 0 %} - {% for reply in status|replies %} - {% with direction=1 %} - {% include 'feed/thread.html' with status=reply is_root=False %} - {% endwith %} - {% endfor %} -{% endif %} -{% endwith %} -
- diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index 0c2647aea..f79b15153 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -109,10 +109,30 @@ class Status(View): status.to_activity(pure=not is_bookwyrm_request(request)) ) + children = models.Status.objects.select_subclasses().raw(""" + WITH RECURSIVE get_thread(depth, id, path) AS ( + + SELECT 1, st.id, ARRAY[st.id] + FROM bookwyrm_status st + WHERE reply_parent_id = '%s' + + UNION + + SELECT (gt.depth + 1), st.id, path || st.id + FROM get_thread gt, bookwyrm_status st + + WHERE st.reply_parent_id = gt.id AND depth < 5 + + ) + + SELECT * FROM get_thread ORDER BY path; + """, params=[status.id]) + data = { **feed_page_data(request.user), **{ "status": status, + "children": children, }, } return TemplateResponse(request, "feed/status.html", data) From cd571614898af7d825a73bfa9abd04bdc26d3b75 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 16:55:05 -0700 Subject: [PATCH 2/6] Privacy filter for thread --- bookwyrm/views/feed.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index f79b15153..92953057d 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -109,24 +109,31 @@ class Status(View): status.to_activity(pure=not is_bookwyrm_request(request)) ) + visible_thread = privacy_filter( + request.user, + models.Status.objects.filter(thread_id=status.thread_id) + ).values_list("id", flat=True) + visible_thread = list(visible_thread) + children = models.Status.objects.select_subclasses().raw(""" WITH RECURSIVE get_thread(depth, id, path) AS ( SELECT 1, st.id, ARRAY[st.id] FROM bookwyrm_status st - WHERE reply_parent_id = '%s' + WHERE reply_parent_id = '%s' AND id = ANY(%s) UNION SELECT (gt.depth + 1), st.id, path || st.id FROM get_thread gt, bookwyrm_status st - WHERE st.reply_parent_id = gt.id AND depth < 5 + WHERE st.reply_parent_id = gt.id AND depth < 5 AND st.id = ANY(%s) ) SELECT * FROM get_thread ORDER BY path; - """, params=[status.id]) + """, params=[status.id, visible_thread, visible_thread]) + data = { **feed_page_data(request.user), From 43f0440505e1163ca35eb59de2f8b6b42b1a9a56 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 16:55:15 -0700 Subject: [PATCH 3/6] Improved privacy query --- bookwyrm/views/helpers.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index bd31fbbc8..7e469f7f0 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -61,8 +61,7 @@ def privacy_filter(viewer, queryset, privacy_levels=None, following_only=False): # exclude blocks from both directions if not viewer.is_anonymous: - blocked = models.User.objects.filter(id__in=viewer.blocks.all()).all() - queryset = queryset.exclude(Q(user__in=blocked) | Q(user__blocks=viewer)) + queryset = queryset.exclude(Q(user__blocked_by=viewer) | Q(user__blocks=viewer)) # you can't see followers only or direct messages if you're not logged in if viewer.is_anonymous: @@ -75,7 +74,7 @@ def privacy_filter(viewer, queryset, privacy_levels=None, following_only=False): if following_only: queryset = queryset.exclude( ~Q( # remove everythign except - Q(user__in=viewer.following.all()) + Q(user__followers=viewer) | Q(user=viewer) # user following | Q(mention_users=viewer) # is self # mentions user ), From 14ac8bb1b5a36aef2cb87134a143896dde5024b6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 16:56:23 -0700 Subject: [PATCH 4/6] Python formatting --- bookwyrm/views/feed.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index 92953057d..b873224b7 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -110,12 +110,12 @@ class Status(View): ) visible_thread = privacy_filter( - request.user, - models.Status.objects.filter(thread_id=status.thread_id) + request.user, models.Status.objects.filter(thread_id=status.thread_id) ).values_list("id", flat=True) visible_thread = list(visible_thread) - children = models.Status.objects.select_subclasses().raw(""" + children = models.Status.objects.select_subclasses().raw( + """ WITH RECURSIVE get_thread(depth, id, path) AS ( SELECT 1, st.id, ARRAY[st.id] @@ -132,8 +132,9 @@ class Status(View): ) SELECT * FROM get_thread ORDER BY path; - """, params=[status.id, visible_thread, visible_thread]) - + """, + params=[status.id, visible_thread, visible_thread], + ) data = { **feed_page_data(request.user), From c821aaa18e3b231c81327fc2c621dd2e4e235575 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 18:24:54 -0700 Subject: [PATCH 5/6] Load status ancestors --- bookwyrm/templates/feed/status.html | 7 +++++++ bookwyrm/views/feed.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bookwyrm/templates/feed/status.html b/bookwyrm/templates/feed/status.html index 429c657d0..8f354fb8d 100644 --- a/bookwyrm/templates/feed/status.html +++ b/bookwyrm/templates/feed/status.html @@ -11,6 +11,13 @@
+ {% for parent in ancestors %} + {% if parent %} +
+ {% include 'snippets/status/status.html' with status=parent %} +
+ {% endif %} + {% endfor %}
{% include 'snippets/status/status.html' with status=status main=True %}
diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index b873224b7..7f1bc22c2 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -114,6 +114,27 @@ class Status(View): ).values_list("id", flat=True) visible_thread = list(visible_thread) + ancestors = models.Status.objects.select_subclasses().raw( + """ + WITH RECURSIVE get_thread(depth, id, path) AS ( + + SELECT 1, st.id, ARRAY[st.id] + FROM bookwyrm_status st + WHERE id = '%s' AND id = ANY(%s) + + UNION + + SELECT (gt.depth + 1), st.reply_parent_id, path || st.id + FROM get_thread gt, bookwyrm_status st + + WHERE st.id = gt.id AND depth < 5 AND st.id = ANY(%s) + + ) + + SELECT * FROM get_thread ORDER BY path DESC; + """, + params=[status.reply_parent_id or 0, visible_thread, visible_thread], + ) children = models.Status.objects.select_subclasses().raw( """ WITH RECURSIVE get_thread(depth, id, path) AS ( @@ -141,6 +162,7 @@ class Status(View): **{ "status": status, "children": children, + "ancestors": ancestors, }, } return TemplateResponse(request, "feed/status.html", data) From 3c82230eedab24a6619e85032b096bd1a8f16618 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 2 Oct 2021 19:22:21 -0700 Subject: [PATCH 6/6] Load subclasses --- bookwyrm/templates/feed/status.html | 7 ++++--- bookwyrm/templatetags/bookwyrm_tags.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/bookwyrm/templates/feed/status.html b/bookwyrm/templates/feed/status.html index 8f354fb8d..5febf4e22 100644 --- a/bookwyrm/templates/feed/status.html +++ b/bookwyrm/templates/feed/status.html @@ -1,5 +1,6 @@ {% extends 'feed/layout.html' %} {% load i18n %} +{% load bookwyrm_tags %} {% block panel %}
@@ -12,13 +13,13 @@
{% for parent in ancestors %} - {% if parent %} + {% if parent.id %}
- {% include 'snippets/status/status.html' with status=parent %} + {% include 'snippets/status/status.html' with status=parent|load_subclass %}
{% endif %} {% endfor %} -
+
{% include 'snippets/status/status.html' with status=status main=True %}
diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index e683f9c24..2e03c13b4 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -53,18 +53,24 @@ def get_next_shelf(current_shelf): return "to-read" +@register.filter(name="load_subclass") +def load_subclass(status): + """sometimes you didn't select_subclass""" + if hasattr(status, "quotation"): + return status.quotation + if hasattr(status, "review"): + return status.review + if hasattr(status, "comment"): + return status.comment + return status + + @register.simple_tag(takes_context=False) def related_status(notification): """for notifications""" if not notification.related_status: return None - if hasattr(notification.related_status, "quotation"): - return notification.related_status.quotation - if hasattr(notification.related_status, "review"): - return notification.related_status.review - if hasattr(notification.related_status, "comment"): - return notification.related_status.comment - return notification.related_status + return load_subclass(notification.related_status) @register.simple_tag(takes_context=True)