Stop over-recursion in post contexts
This commit is contained in:
parent
6411a375ba
commit
552a150e57
|
@ -105,6 +105,7 @@ class PostService:
|
||||||
# Retrieve descendants via breadth-first-search
|
# Retrieve descendants via breadth-first-search
|
||||||
descendants: list[Post] = []
|
descendants: list[Post] = []
|
||||||
queue = [self.post]
|
queue = [self.post]
|
||||||
|
seen: set[str] = set()
|
||||||
while queue and len(descendants) < num_descendants:
|
while queue and len(descendants) < num_descendants:
|
||||||
node = queue.pop()
|
node = queue.pop()
|
||||||
child_queryset = (
|
child_queryset = (
|
||||||
|
@ -119,8 +120,10 @@ class PostService:
|
||||||
else:
|
else:
|
||||||
child_queryset = child_queryset.unlisted(include_replies=True)
|
child_queryset = child_queryset.unlisted(include_replies=True)
|
||||||
for child in child_queryset:
|
for child in child_queryset:
|
||||||
descendants.append(child)
|
if child.pk not in seen:
|
||||||
queue.append(child)
|
descendants.append(child)
|
||||||
|
queue.append(child)
|
||||||
|
seen.add(child.pk)
|
||||||
return ancestors, descendants
|
return ancestors, descendants
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
|
Loading…
Reference in New Issue