From 552a150e5726de33600f76b07cbb088cf574bc09 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 2 Mar 2023 10:28:27 -0700 Subject: [PATCH] Stop over-recursion in post contexts --- activities/services/post.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/activities/services/post.py b/activities/services/post.py index 1631ce4..f225fdd 100644 --- a/activities/services/post.py +++ b/activities/services/post.py @@ -105,6 +105,7 @@ class PostService: # Retrieve descendants via breadth-first-search descendants: list[Post] = [] queue = [self.post] + seen: set[str] = set() while queue and len(descendants) < num_descendants: node = queue.pop() child_queryset = ( @@ -119,8 +120,10 @@ class PostService: else: child_queryset = child_queryset.unlisted(include_replies=True) for child in child_queryset: - descendants.append(child) - queue.append(child) + if child.pk not in seen: + descendants.append(child) + queue.append(child) + seen.add(child.pk) return ancestors, descendants def delete(self):