Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 12, 2023
1 parent 6d8345a commit 6ab8724
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
8 changes: 6 additions & 2 deletions posthog/api/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ def get_queryset(self) -> QuerySet:
if params.get("item_id"):
queryset = queryset.filter(item_id=params.get("item_id"))

source_comment_id = params.get("source_comment_id")
if self.action == "thread":
# Filter based on the source_comment_id
object_id = self.kwargs.get("pk")
queryset = queryset.filter(source_comment_id=object_id)
source_comment_id = self.kwargs.get("pk")

if source_comment_id:
# NOTE: Should we also return the source_comment ?
queryset = queryset.filter(source_comment_id=source_comment_id)

return queryset

Expand Down
47 changes: 40 additions & 7 deletions posthog/api/test/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@


class TestComments(APIBaseTest, QueryMatchingTest):
def _create_comment(self, content: str, scope: str = "Notebook") -> None:
def _create_comment(self, data={}) -> None:
payload = {
"content": "my content",
"scope": "Notebook",
}

payload.update(data)

return self.client.post(
f"/api/projects/{self.team.id}/comments",
{
"content": content,
"scope": scope,
},
payload,
).json()

def test_creates_comment_with_validation_errors(self) -> None:
Expand Down Expand Up @@ -91,10 +95,39 @@ def test_empty_comments_list(self) -> None:
}

def test_lists_comments(self) -> None:
self._create_comment("comment 1")
self._create_comment("comment 2")
self._create_comment({"content": "comment 1"})
self._create_comment({"content": "comment 2"})
response = self.client.get(f"/api/projects/{self.team.id}/comments")
assert len(response.json()["results"]) == 2

assert response.json()["results"][0]["content"] == "comment 2"
assert response.json()["results"][1]["content"] == "comment 1"

def test_lists_comments_filtering(self) -> None:
self._create_comment({"content": "comment notebook-1", "scope": "Notebook", "item_id": "1"})
self._create_comment({"content": "comment notebook-2", "scope": "Notebook", "item_id": "2"})
self._create_comment({"content": "comment dashboard-1", "scope": "Dashboard", "item_id": "1"})

response = self.client.get(f"/api/projects/{self.team.id}/comments?scope=Notebook")
assert len(response.json()["results"]) == 2
assert response.json()["results"][0]["content"] == "comment notebook-2"
assert response.json()["results"][1]["content"] == "comment notebook-1"

response = self.client.get(f"/api/projects/{self.team.id}/comments?scope=Notebook&item_id=2")
assert len(response.json()["results"]) == 1
assert response.json()["results"][0]["content"] == "comment notebook-2"

def test_lists_comments_thread(self) -> None:
initial_comment = self._create_comment({"content": "comment notebook-1", "scope": "Notebook", "item_id": "1"})
self._create_comment({"content": "comment reply", "source_comment_id": initial_comment["id"]})
self._create_comment({"content": "comment other reply", "source_comment_id": initial_comment["id"]})
self._create_comment({"content": "comment elsewhere"})

for url in [
f"/api/projects/{self.team.id}/comments/{initial_comment['id']}/thread",
f"/api/projects/{self.team.id}/comments/?source_comment_id={initial_comment['id']}",
]:
response = self.client.get(url)
assert len(response.json()["results"]) == 2
assert response.json()["results"][0]["content"] == "comment other reply"
assert response.json()["results"][1]["content"] == "comment reply"

0 comments on commit 6ab8724

Please sign in to comment.