Get thread children with depth first recusive search

This commit is contained in:
Mouse Reeve 2021-10-02 15:20:35 -07:00
parent a36bbaf809
commit 7caaddbb22
3 changed files with 33 additions and 26 deletions

View file

@ -9,7 +9,19 @@
</a>
</header>
{% include 'feed/thread.html' with status=status depth=0 max_depth=6 is_root=True direction=0 %}
<div class="thread-parent is-relative block">
<div class="thread">
<div class="is-main block">
{% include 'snippets/status/status.html' with status=status main=True %}
</div>
{% for child in children %}
<div class="block">
{% include 'snippets/status/status.html' with status=child %}
</div>
{% endfor %}
</div>
</div>
{% endblock %}

View file

@ -1,25 +0,0 @@
{% load status_display %}
<div class="thread-parent is-relative block">
<div class="thread">
{% 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 %}
<div{% if is_root %} class="block mt-5 is-main"{% endif %}>
{% include 'snippets/status/status.html' with status=status main=is_root %}
</div>
{% 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 %}
</div>
</div>

View file

@ -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)