diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index bd112c28d..30d7b7551 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -3136,12 +3136,21 @@ class ThreadViewSet( del serializer.validated_data["body"] thread = serializer.save() + user = self.request.user if self.request.user.is_authenticated else None + models.Comment.objects.create( thread=thread, - user=self.request.user if self.request.user.is_authenticated else None, + user=user, body=body, ) + posthog_capture( + PosthogEventName.THREAD_CREATED, + user, + {"thread_id": str(thread.id)}, + document=self.get_document_or_404(), + ) + @drf.decorators.action(detail=True, methods=["post"], url_path="resolve") def resolve(self, request, *args, **kwargs): """Resolve a thread.""" diff --git a/src/backend/core/tests/documents/test_api_documents_threads.py b/src/backend/core/tests/documents/test_api_documents_threads.py index d3831de7c..cd6961284 100644 --- a/src/backend/core/tests/documents/test_api_documents_threads.py +++ b/src/backend/core/tests/documents/test_api_documents_threads.py @@ -1,9 +1,12 @@ """Test Thread viewset.""" +from unittest import mock + import pytest from rest_framework.test import APIClient from core import factories, models +from core.utils.analytics import PosthogEventName pytestmark = pytest.mark.django_db @@ -47,16 +50,26 @@ def test_api_documents_threads_public_document(link_role): ) client = APIClient() - response = client.post( - f"/api/v1.0/documents/{document.id!s}/threads/", - { - "body": "test", - }, - ) + with mock.patch("core.api.viewsets.posthog_capture") as mock_capture: + response = client.post( + f"/api/v1.0/documents/{document.id!s}/threads/", + { + "body": "test", + }, + ) assert response.status_code == 201 thread = models.Thread.objects.first() comment = thread.comments.first() + + # The thread creation should be tracked in PostHog + mock_capture.assert_called_once_with( + PosthogEventName.THREAD_CREATED, + None, + {"thread_id": str(thread.id)}, + document=document, + ) + content = response.json() assert content == { "id": str(thread.id), @@ -136,16 +149,26 @@ def test_api_documents_threads_restricted_document_editor(role): client = APIClient() client.force_login(user) - response = client.post( - f"/api/v1.0/documents/{document.id!s}/threads/", - { - "body": "test", - }, - ) + with mock.patch("core.api.viewsets.posthog_capture") as mock_capture: + response = client.post( + f"/api/v1.0/documents/{document.id!s}/threads/", + { + "body": "test", + }, + ) assert response.status_code == 201 thread = models.Thread.objects.first() comment = thread.comments.first() + + # The thread creation should be tracked in PostHog + mock_capture.assert_called_once_with( + PosthogEventName.THREAD_CREATED, + user, + {"thread_id": str(thread.id)}, + document=document, + ) + content = response.json() assert content == { "id": str(thread.id), @@ -247,16 +270,26 @@ def test_api_documents_threads_authenticated_document(link_role): client = APIClient() client.force_login(user) - response = client.post( - f"/api/v1.0/documents/{document.id!s}/threads/", - { - "body": "test", - }, - ) + with mock.patch("core.api.viewsets.posthog_capture") as mock_capture: + response = client.post( + f"/api/v1.0/documents/{document.id!s}/threads/", + { + "body": "test", + }, + ) assert response.status_code == 201 thread = models.Thread.objects.first() comment = thread.comments.first() + + # The thread creation should be tracked in PostHog + mock_capture.assert_called_once_with( + PosthogEventName.THREAD_CREATED, + user, + {"thread_id": str(thread.id)}, + document=document, + ) + content = response.json() assert content == { "id": str(thread.id), diff --git a/src/backend/core/utils/analytics.py b/src/backend/core/utils/analytics.py index 40322a0d4..a867d8dd1 100644 --- a/src/backend/core/utils/analytics.py +++ b/src/backend/core/utils/analytics.py @@ -26,6 +26,9 @@ class PosthogEventName(StrEnum): DOC_ACCESS_CREATED = "doc_access_created" DOC_ACCESS_DELETED = "doc_access_deleted" + # Thread + THREAD_CREATED = "thread_created" + # User USER_LOGIN = "user_login"