mirror of
https://github.com/suitenumerique/docs.git
synced 2026-09-02 05:08:54 +02:00
📈(backend) capture an event when a thread is created
When a new thread is created, we capture an event related to this thread creation.
This commit is contained in:
@@ -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."""
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user