Load status ancestors

This commit is contained in:
Mouse Reeve 2021-10-02 18:24:54 -07:00
parent 14ac8bb1b5
commit c821aaa18e
2 changed files with 29 additions and 0 deletions

View file

@ -11,6 +11,13 @@
<div class="thread-parent is-relative block">
<div class="thread">
{% for parent in ancestors %}
{% if parent %}
<div class="block">
{% include 'snippets/status/status.html' with status=parent %}
</div>
{% endif %}
{% endfor %}
<div class="is-main block">
{% include 'snippets/status/status.html' with status=status main=True %}
</div>

View file

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