Commit f887411d authored by Aryan Noshirvani's avatar Aryan Noshirvani

Make remix tree fetching actually recursive

GET /social/posts/{id}/tree only ever fetched one level of children -
a remix of a remix wouldn't show up at all. Rewrote it as a proper
recursive walk (_build_remix_tree), each node now nesting its own
children the same way, all the way down. Depth-capped at 50 as a
guard against runaway recursion on pathological/corrupted data, well
beyond anything a real remix-of-a-remix chain would reach.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCxXmJVvDFJVHTUi4QgNzi
parent 8ff8c429
Pipeline #1005 passed with stage
in 40 minutes and 16 seconds
......@@ -40,18 +40,27 @@ def create_post(post: PostCreate, db: Session = Depends(get_db)):
db.refresh(db_post)
return db_post
MAX_TREE_DEPTH = 50 # guards against runaway recursion on pathological/corrupted data;
# normal remix trees (remixes of remixes of remixes...) come nowhere
# close to this in practice.
def _build_remix_tree(post_id: int, db: Session, depth: int = 0) -> dict:
post = db.query(Post).filter(Post.id == post_id).first()
children = db.query(Post).filter(Post.parent_post_id == post_id).all() if depth < MAX_TREE_DEPTH else []
return {
"post": post,
"children": [_build_remix_tree(c.id, db, depth + 1) for c in children],
}
@router.get("/posts/{post_id}/tree")
def get_remix_tree(post_id: int, db: Session = Depends(get_db)):
post = db.query(Post).filter(Post.id == post_id).first()
if not post:
raise HTTPException(status_code=404, detail="Post not found")
children = db.query(Post).filter(Post.parent_post_id == post_id).all()
return {
"post": post,
"children": children
}
return _build_remix_tree(post_id, db)
@router.post("/geo-drop", status_code=status.HTTP_201_CREATED)
def create_geo_drop(drop: GeoDropCreate, db: Session = Depends(get_db)):
......
......@@ -65,4 +65,31 @@ def test_remix_tree_with_children():
body = response.json()
assert body["post"]["id"] == root_id
assert len(body["children"]) == 1
assert body["children"][0]["caption"] == "a remix of the root"
assert body["children"][0]["post"]["id"] == child_res.json()["id"]
def test_remix_tree_is_recursive_across_multiple_generations():
root_id = client.post("/social/posts", json={"user_id": 1, "song_id": 1, "caption": "gen 0"}).json()["id"]
child_id = client.post("/social/posts", json={
"user_id": 2, "song_id": 1, "parent_post_id": root_id, "caption": "gen 1",
}).json()["id"]
grandchild_id = client.post("/social/posts", json={
"user_id": 3, "song_id": 1, "parent_post_id": child_id, "caption": "gen 2",
}).json()["id"]
response = client.get(f"/social/posts/{root_id}/tree")
assert response.status_code == 200
body = response.json()
assert body["post"]["id"] == root_id
assert len(body["children"]) == 1
child_node = body["children"][0]
assert child_node["post"]["id"] == child_id
assert len(child_node["children"]) == 1
grandchild_node = child_node["children"][0]
assert grandchild_node["post"]["id"] == grandchild_id
assert grandchild_node["children"] == []
def test_remix_tree_leaf_node_has_no_children():
leaf_id = client.post("/social/posts", json={"user_id": 1, "song_id": 1, "caption": "no remixes yet"}).json()["id"]
response = client.get(f"/social/posts/{leaf_id}/tree")
assert response.json()["children"] == []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment