Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(messaging): move views to presentation and seal the tach boundary (#113944)

Harley Alexander2db5077f4a80
diff --git a/posthog/urls.py b/posthog/urls.py
index fd7020d63d43..1cf2f2c7916b 100644
--- a/posthog/urls.py
+++ b/posthog/urls.py
@@ -48,8 +48,8 @@ from products.customer_analytics.backend.presentation.views.internal import (
InternalAccountView as CustomerAnalyticsInternalAccountView,
)
from products.early_access_features.backend.api import early_access_features
-from products.messaging.backend.api.customerio_webhook import CustomerIOWebhookView
-from products.messaging.backend.api.push_subscriptions import push_subscriptions
+from products.messaging.backend.presentation.views.customerio_webhook import CustomerIOWebhookView
+from products.messaging.backend.presentation.views.push_subscriptions import push_subscriptions
from products.messaging.backend.presentation.views.recipient_preferences import preferences_page, update_preferences
from products.notebooks.backend.facade.sql_v2 import (
notebook_sql_v2_callback,
diff --git a/products/messaging/backend/api/test/__init__.py b/products/messaging/backend/api/test/__init__.py
deleted file mode 100644
index e69de29bb2d1..000000000000
diff --git a/products/messaging/backend/facade/api.py b/products/messaging/backend/facade/api.py
index d92fd9b17289..bf2d377f12a0 100644
--- a/products/messaging/backend/facade/api.py
+++ b/products/messaging/backend/facade/api.py
@@ -2,17 +2,13 @@ from collections.abc import Callable
from typing import Any
from uuid import UUID
-from products.messaging.backend.api import design_operations, design_validation
+from products.messaging.backend.facade.contracts import UnlayerNotConfiguredError, UnlayerRenderError
from products.messaging.backend.remote_config import (
PUSH_APP_ID_CONFIG_KEYS,
build_push_config as build_push_app_ids,
)
-from products.messaging.backend.services import message_templates
-from products.messaging.backend.unlayer import (
- UnlayerNotConfiguredError,
- UnlayerRenderError,
- render_design_html as render_unlayer_design,
-)
+from products.messaging.backend.services import design_operations, design_validation, message_templates
+from products.messaging.backend.unlayer import render_design_html as render_unlayer_design
__all__ = [
"UnlayerNotConfiguredError",
diff --git a/products/messaging/backend/facade/contracts.py b/products/messaging/backend/facade/contracts.py
new file mode 100644
index 000000000000..9e84ab7c192d
--- /dev/null
+++ b/products/messaging/backend/facade/contracts.py
@@ -0,0 +1,10 @@
+class UnlayerError(Exception):
+ pass
+
+
+class UnlayerNotConfiguredError(UnlayerError):
+ pass
+
+
+class UnlayerRenderError(UnlayerError):
+ pass
diff --git a/products/messaging/backend/api/customerio_webhook.py b/products/messaging/backend/presentation/views/customerio_webhook.py
similarity index 100%
rename from products/messaging/backend/api/customerio_webhook.py
rename to products/messaging/backend/presentation/views/customerio_webhook.py
diff --git a/products/messaging/backend/api/message_categories.py b/products/messaging/backend/presentation/views/message_categories.py
similarity index 100%
rename from products/messaging/backend/api/message_categories.py
rename to products/messaging/backend/presentation/views/message_categories.py
diff --git a/products/messaging/backend/api/message_preferences.py b/products/messaging/backend/presentation/views/message_preferences.py
similarity index 100%
rename from products/messaging/backend/api/message_preferences.py
rename to products/messaging/backend/presentation/views/message_preferences.py
diff --git a/products/messaging/backend/api/message_suppression.py b/products/messaging/backend/presentation/views/message_suppression.py
similarity index 100%
rename from products/messaging/backend/api/message_suppression.py
rename to products/messaging/backend/presentation/views/message_suppression.py
diff --git a/products/messaging/backend/api/message_templates.py b/products/messaging/backend/presentation/views/message_templates.py
similarity index 97%
rename from products/messaging/backend/api/message_templates.py
rename to products/messaging/backend/presentation/views/message_templates.py
index 30721cdea784..e53dc5de5f94 100644
--- a/products/messaging/backend/api/message_templates.py
+++ b/products/messaging/backend/presentation/views/message_templates.py
@@ -18,12 +18,16 @@ from posthog.api.shared import UserBasicSerializer
from posthog.cdp.validation import build_html_wrap_design
from posthog.event_usage import report_user_action
-from products.messaging.backend.api.design_operations import apply_design_operations
-from products.messaging.backend.api.design_validation import validate_design
+from products.messaging.backend.facade.api import (
+ UnlayerNotConfiguredError,
+ UnlayerRenderError,
+ apply_design_operations,
+ render_design_html,
+ validate_design,
+)
from products.messaging.backend.models.message_category import MessageCategory
from products.messaging.backend.models.message_template import MessageTemplate
-from products.messaging.backend.presentation.serializers import DesignOperationSerializer
-from products.messaging.backend.unlayer import UnlayerNotConfiguredError, UnlayerRenderError, render_design_html
+from products.messaging.backend.presentation.views.serializers import DesignOperationSerializer
from products.notifications.backend.facade.api import publish_resource_edited
logger = structlog.get_logger(__name__)
diff --git a/products/messaging/backend/api/push_identity_tokens.py b/products/messaging/backend/presentation/views/push_identity_tokens.py
similarity index 100%
rename from products/messaging/backend/api/push_identity_tokens.py
rename to products/messaging/backend/presentation/views/push_identity_tokens.py
diff --git a/products/messaging/backend/api/push_subscriptions.py b/products/messaging/backend/presentation/views/push_subscriptions.py
similarity index 99%
rename from products/messaging/backend/api/push_subscriptions.py
rename to products/messaging/backend/presentation/views/push_subscriptions.py
index 07de31c24f86..25a26b6ed809 100644
--- a/products/messaging/backend/api/push_subscriptions.py
+++ b/products/messaging/backend/presentation/views/push_subscriptions.py
@@ -30,7 +30,7 @@ from posthog.models.team.team import Team
from posthog.utils import decompress, load_data_from_request
from posthog.utils_cors import cors_response
-from products.messaging.backend.api.push_identity_tokens import verify_push_identity_token
+from products.messaging.backend.presentation.views.push_identity_tokens import verify_push_identity_token
# Identity verification is opt-in per integration via config["push_identity_verification"]:
# "disabled" (default) — no token required; anyone with the public project token can register.
diff --git a/products/messaging/backend/presentation/serializers.py b/products/messaging/backend/presentation/views/serializers.py
similarity index 100%
rename from products/messaging/backend/presentation/serializers.py
rename to products/messaging/backend/presentation/views/serializers.py
diff --git a/products/messaging/backend/routes.py b/products/messaging/backend/routes.py
index 12f903832ca4..2e64fcaf697d 100644
--- a/products/messaging/backend/routes.py
+++ b/products/messaging/backend/routes.py
@@ -1,9 +1,9 @@
from posthog.api.routing import RouterRegistry
-from products.messaging.backend.api.message_categories import MessageCategoryViewSet
-from products.messaging.backend.api.message_preferences import MessagePreferencesViewSet
-from products.messaging.backend.api.message_suppression import MessageSuppressionViewSet
-from products.messaging.backend.api.message_templates import MessageTemplatesViewSet
+from products.messaging.backend.presentation.views.message_categories import MessageCategoryViewSet
+from products.messaging.backend.presentation.views.message_preferences import MessagePreferencesViewSet
+from products.messaging.backend.presentation.views.message_suppression import MessageSuppressionViewSet
+from products.messaging.backend.presentation.views.message_templates import MessageTemplatesViewSet
def register_routes(routers: RouterRegistry) -> None:
diff --git a/products/messaging/backend/api/design_operations.py b/products/messaging/backend/services/design_operations.py
similarity index 100%
rename from products/messaging/backend/api/design_operations.py
rename to products/messaging/backend/services/design_operations.py
diff --git a/products/messaging/backend/api/design_validation.py b/products/messaging/backend/services/design_validation.py
similarity index 100%
rename from products/messaging/backend/api/design_validation.py
rename to products/messaging/backend/services/design_validation.py
diff --git a/products/messaging/backend/api/__init__.py b/products/messaging/backend/tests/api/__init__.py
similarity index 100%
rename from products/messaging/backend/api/__init__.py
rename to products/messaging/backend/tests/api/__init__.py
diff --git a/products/messaging/backend/api/test/test_customerio_webhook.py b/products/messaging/backend/tests/api/test_customerio_webhook.py
similarity index 100%
rename from products/messaging/backend/api/test/test_customerio_webhook.py
rename to products/messaging/backend/tests/api/test_customerio_webhook.py
diff --git a/products/messaging/backend/api/test/test_design_operations.py b/products/messaging/backend/tests/api/test_design_operations.py
similarity index 98%
rename from products/messaging/backend/api/test/test_design_operations.py
rename to products/messaging/backend/tests/api/test_design_operations.py
index bfc9019d327a..e1e2520da31d 100644
--- a/products/messaging/backend/api/test/test_design_operations.py
+++ b/products/messaging/backend/tests/api/test_design_operations.py
@@ -4,8 +4,8 @@ import pytest
from rest_framework import serializers
-from products.messaging.backend.api.design_operations import apply_design_operations
-from products.messaging.backend.api.design_validation import validate_design
+from products.messaging.backend.services.design_operations import apply_design_operations
+from products.messaging.backend.services.design_validation import validate_design
def _sample_design() -> dict:
diff --git a/products/messaging/backend/api/test/test_message_categories.py b/products/messaging/backend/tests/api/test_message_categories.py
similarity index 97%
rename from products/messaging/backend/api/test/test_message_categories.py
rename to products/messaging/backend/tests/api/test_message_categories.py
index d32f5cd1a5ac..b858d2d40259 100644
--- a/products/messaging/backend/api/test/test_message_categories.py
+++ b/products/messaging/backend/tests/api/test_message_categories.py
@@ -208,7 +208,9 @@ class TestMessageCategoryAPI(APIBaseTest):
def test_import_preferences_csv_without_categories(self):
"""Test CSV import when no categories exist"""
- with patch("products.messaging.backend.api.message_categories.CustomerIOImportService") as mock_service_class:
+ with patch(
+ "products.messaging.backend.presentation.views.message_categories.CustomerIOImportService"
+ ) as mock_service_class:
mock_service = MagicMock()
mock_service_class.return_value = mock_service
mock_service.process_preferences_csv.return_value = {
@@ -309,7 +311,7 @@ class TestOptOutSyncConfigAPI(APIBaseTest):
self.assertEqual(data["app_import_result"]["categories_created"], 6)
self.assertIsNone(data["csv_import_result"])
- @patch("products.messaging.backend.api.message_categories.CustomerIOImportService")
+ @patch("products.messaging.backend.presentation.views.message_categories.CustomerIOImportService")
def test_import_from_customerio_stores_integration_and_result(self, mock_service_class):
mock_service = MagicMock()
mock_service_class.return_value = mock_service
@@ -338,7 +340,7 @@ class TestOptOutSyncConfigAPI(APIBaseTest):
self.assertEqual(config.app_import_result["status"], "completed")
self.assertEqual(config.app_import_result["categories_created"], 3)
- @patch("products.messaging.backend.api.message_categories.CustomerIOImportService")
+ @patch("products.messaging.backend.presentation.views.message_categories.CustomerIOImportService")
def test_import_from_customerio_stores_failure_result(self, mock_service_class):
mock_service = MagicMock()
mock_service_class.return_value = mock_service
@@ -360,7 +362,7 @@ class TestOptOutSyncConfigAPI(APIBaseTest):
self.assertEqual(config.app_import_result["status"], "failed")
self.assertIn("Invalid API key", config.app_import_result["error"])
- @patch("products.messaging.backend.api.message_categories.CustomerIOImportService")
+ @patch("products.messaging.backend.presentation.views.message_categories.CustomerIOImportService")
def test_import_from_customerio_reuses_stored_key(self, mock_service_class):
Integration.objects.create(
team=self.team,
@@ -526,7 +528,7 @@ class TestOptOutSyncConfigAPI(APIBaseTest):
integration = Integration.objects.get(team=self.team, kind="customerio-webhook")
self.assertEqual(integration.sensitive_config["webhook_signing_secret"], "secret")
- @patch("products.messaging.backend.api.message_categories.CustomerIOImportService")
+ @patch("products.messaging.backend.presentation.views.message_categories.CustomerIOImportService")
def test_csv_import_stores_success_result(self, mock_service_class):
mock_service = MagicMock()
mock_service_class.return_value = mock_service
@@ -552,7 +554,7 @@ class TestOptOutSyncConfigAPI(APIBaseTest):
self.assertEqual(config.csv_import_result["total_rows"], 100)
self.assertEqual(config.csv_import_result["users_with_optouts"], 60)
- @patch("products.messaging.backend.api.message_categories.CustomerIOImportService")
+ @patch("products.messaging.backend.presentation.views.message_categories.CustomerIOImportService")
def test_csv_import_stores_failure_result(self, mock_service_class):
mock_service = MagicMock()
mock_service_class.return_value = mock_service
diff --git a/products/messaging/backend/api/test/test_message_preference_styles.py b/products/messaging/backend/tests/api/test_message_preference_styles.py
similarity index 100%
rename from products/messaging/backend/api/test/test_message_preference_styles.py
rename to products/messaging/backend/tests/api/test_message_preference_styles.py
diff --git a/products/messaging/backend/api/test/test_message_preferences.py b/products/messaging/backend/tests/api/test_message_preferences.py
similarity index 100%
rename from products/messaging/backend/api/test/test_message_preferences.py
rename to products/messaging/backend/tests/api/test_message_preferences.py
diff --git a/products/messaging/backend/api/test/test_message_suppression.py b/products/messaging/backend/tests/api/test_message_suppression.py
similarity index 100%
rename from products/messaging/backend/api/test/test_message_suppression.py
rename to products/messaging/backend/tests/api/test_message_suppression.py
diff --git a/products/messaging/backend/api/test/test_message_suppression_unit.py b/products/messaging/backend/tests/api/test_message_suppression_unit.py
similarity index 89%
rename from products/messaging/backend/api/test/test_message_suppression_unit.py
rename to products/messaging/backend/tests/api/test_message_suppression_unit.py
index 202292017057..24909d4c0d0e 100644
--- a/products/messaging/backend/api/test/test_message_suppression_unit.py
+++ b/products/messaging/backend/tests/api/test_message_suppression_unit.py
@@ -1,6 +1,6 @@
from django.test import SimpleTestCase
-from products.messaging.backend.api.message_suppression import MessageSuppressionViewSet
+from products.messaging.backend.presentation.views.message_suppression import MessageSuppressionViewSet
class TestMessageSuppressionViewSetScope(SimpleTestCase):
diff --git a/products/messaging/backend/api/test/test_message_templates.py b/products/messaging/backend/tests/api/test_message_templates.py
similarity index 95%
rename from products/messaging/backend/api/test/test_message_templates.py
rename to products/messaging/backend/tests/api/test_message_templates.py
index 011f65f3a507..660b83732158 100644
--- a/products/messaging/backend/api/test/test_message_templates.py
+++ b/products/messaging/backend/tests/api/test_message_templates.py
@@ -103,7 +103,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
- @patch("products.messaging.backend.api.message_templates.report_user_action")
+ @patch("products.messaging.backend.presentation.views.message_templates.report_user_action")
def test_create_email_template_with_subject_succeeds(self, mock_report):
response = self.client.post(
f"/api/environments/{self.team.id}/messaging_templates/",
@@ -180,7 +180,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "templating" in str(response.json())
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_create_with_design_only_renders_html_server_side(self, mock_render):
mock_render.return_value = "<html><body>Rendered</body></html>"
@@ -200,7 +200,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
assert email["html"] == "<html><body>Rendered</body></html>"
assert email["design"] == MINIMAL_DESIGN
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_create_with_design_and_html_keeps_submitted_html(self, mock_render):
"""The visual editor exports html from the design client-side and submits both —
a present html is trusted, not re-rendered."""
@@ -224,7 +224,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
("not_configured", UnlayerNotConfiguredError(), "not configured"),
]
)
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_create_with_design_render_error_returns_400(self, _name, side_effect, expected_message, mock_render):
mock_render.side_effect = side_effect
@@ -241,7 +241,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
assert expected_message in str(response.json())
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_update_with_design_only_renders_html_server_side(self, mock_render):
mock_render.return_value = "<html><body>Re-rendered</body></html>"
@@ -303,7 +303,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
response = getattr(self.client, method)(url, data=data, format="json")
assert response.status_code == expected_status, response.json()
- @patch("products.messaging.backend.api.message_templates.publish_resource_edited")
+ @patch("products.messaging.backend.presentation.views.message_templates.publish_resource_edited")
def test_update_replaces_content_wholesale(self, mock_emit):
"""The content JSONField is replaced as a unit on update, never deep-merged —
a payload without design makes the submitted html canonical."""
@@ -380,8 +380,8 @@ class TestMessageTemplatesAPI(APIBaseTest):
},
}
- @patch("products.messaging.backend.api.message_templates.publish_resource_edited")
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.publish_resource_edited")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_design_patch_updates_one_block_and_rerenders(self, mock_render, mock_emit):
mock_render.return_value = "<html>patched</html>"
self.message_template.content = {"email": {"subject": "Hi", "design": self._design_with_text()}}
@@ -403,8 +403,8 @@ class TestMessageTemplatesAPI(APIBaseTest):
mock_render.assert_called_once()
self._assert_resource_edited_emitted(mock_emit)
- @patch("products.messaging.backend.api.message_templates.publish_resource_edited")
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.publish_resource_edited")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_design_patch_unknown_id_leaves_template_untouched(self, mock_render, mock_emit):
mock_render.return_value = "<html>x</html>"
original = self._design_with_text()
@@ -473,7 +473,7 @@ class TestMessageTemplatesAPI(APIBaseTest):
assert response.status_code == status.HTTP_403_FORBIDDEN
- @patch("products.messaging.backend.api.message_templates.render_design_html")
+ @patch("products.messaging.backend.presentation.views.message_templates.render_design_html")
def test_design_patch_allows_write_scoped_personal_api_key(self, mock_render):
# Regression: the design action must declare hog_flow:write so MCP / personal API key callers
# aren't rejected as "action does not support personal API key access".
diff --git a/products/messaging/backend/api/test/test_push_identity_tokens.py b/products/messaging/backend/tests/api/test_push_identity_tokens.py
similarity index 98%
rename from products/messaging/backend/api/test/test_push_identity_tokens.py
rename to products/messaging/backend/tests/api/test_push_identity_tokens.py
index 20dffd4c66f2..7bdd13cc6d9e 100644
--- a/products/messaging/backend/api/test/test_push_identity_tokens.py
+++ b/products/messaging/backend/tests/api/test_push_identity_tokens.py
@@ -12,7 +12,7 @@ from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
from parameterized import parameterized
-from products.messaging.backend.api.push_identity_tokens import (
+from products.messaging.backend.presentation.views.push_identity_tokens import (
PUSH_IDENTITY_TOKEN_AUDIENCE,
sign_push_identity_token_es256,
verify_push_identity_token,
diff --git a/products/messaging/backend/api/test/test_push_subscriptions.py b/products/messaging/backend/tests/api/test_push_subscriptions.py
similarity index 92%
rename from products/messaging/backend/api/test/test_push_subscriptions.py
rename to products/messaging/backend/tests/api/test_push_subscriptions.py
index 4fdc69948c5b..a3bfc2f203d8 100644
--- a/products/messaging/backend/api/test/test_push_subscriptions.py
+++ b/products/messaging/backend/tests/api/test_push_subscriptions.py
@@ -18,9 +18,9 @@ from posthog.models.integration import Integration
from posthog.models.team.team import Team
from posthog.models.team.team_caching import set_team_in_cache
-from products.messaging.backend.api import push_subscriptions
-from products.messaging.backend.api.push_identity_tokens import sign_push_identity_token_es256
-from products.messaging.backend.api.push_subscriptions import (
+from products.messaging.backend.presentation.views import push_subscriptions
+from products.messaging.backend.presentation.views.push_identity_tokens import sign_push_identity_token_es256
+from products.messaging.backend.presentation.views.push_subscriptions import (
PUSH_SUBSCRIPTION_DISCARD_COUNTER,
PUSH_SUBSCRIPTION_REJECTION_COUNTER,
_api_key_fingerprint,
@@ -92,7 +92,7 @@ class TestPushSubscriptionsAPI(BaseTest):
# The endpoint resolves the team from the token cache, so refresh it.
set_team_in_cache(self.team.api_token, self.team)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_register_android_token(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -120,7 +120,7 @@ class TestPushSubscriptionsAPI(BaseTest):
for key in call_kwargs["properties"]["$set"]
)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_two_devices_on_one_app_are_stored_under_separate_keys(self, mock_capture: MagicMock):
# A second device on the same app used to overwrite the first, leaving it unreachable.
mock_capture.return_value = MagicMock(status_code=200)
@@ -144,7 +144,7 @@ class TestPushSubscriptionsAPI(BaseTest):
device_subscription_key("my-firebase-project", "fcm-token-tablet"),
]
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_the_same_device_registering_twice_reuses_its_key(self, mock_capture: MagicMock):
# Re-registration must not accumulate a key per call.
mock_capture.return_value = MagicMock(status_code=200)
@@ -171,7 +171,7 @@ class TestPushSubscriptionsAPI(BaseTest):
== "$device_push_subscription_my-project:7d8d408df65cffa5"
)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_register_ios_token(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -194,7 +194,7 @@ class TestPushSubscriptionsAPI(BaseTest):
key.startswith("$device_push_subscription_com.example.app:") for key in call_kwargs["properties"]["$set"]
)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_ios_device_registers_a_firebase_token(self, mock_capture: MagicMock):
# An iOS app delivering via Firebase registers with the Firebase project_id even though its
# platform is "ios": the provider is resolved from the app_id, not the device platform.
@@ -216,7 +216,7 @@ class TestPushSubscriptionsAPI(BaseTest):
for key in call_kwargs["properties"]["$set"]
)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_token_is_encrypted(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -239,7 +239,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert isinstance(encrypted_value, str)
assert len(encrypted_value) > 0
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_unregister_unsets_the_subscription_property(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -268,7 +268,7 @@ class TestPushSubscriptionsAPI(BaseTest):
"$device_push_subscription_my-firebase-project",
]
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_unregister_ios_token(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -285,7 +285,7 @@ class TestPushSubscriptionsAPI(BaseTest):
call_kwargs = mock_capture.call_args.kwargs
assert call_kwargs["properties"]["$unset"][-1] == "$device_push_subscription_com.example.app"
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_unregister_without_integration_still_unsets(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -360,7 +360,7 @@ class TestPushSubscriptionsAPI(BaseTest):
}
headers = {"User-Agent": user_agent} if user_agent else None
- with patch("products.messaging.backend.api.push_subscriptions.capture_internal") as capture:
+ with patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal") as capture:
response = self.client.post(
"/api/push_subscriptions/",
data=json.dumps(payload),
@@ -377,7 +377,7 @@ class TestPushSubscriptionsAPI(BaseTest):
)
def test_platform_sent_by_older_sdks_is_ignored(self):
- with patch("products.messaging.backend.api.push_subscriptions.capture_internal") as capture:
+ with patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal") as capture:
response = self._post(
{
"distinct_id": "user-1",
@@ -391,7 +391,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert "platform" not in response.json()
assert capture.call_count == 1
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_register_without_integration_returns_200_and_discards(self, mock_capture: MagicMock):
counter = PUSH_SUBSCRIPTION_DISCARD_COUNTER.labels(reason="no_integration")
before = counter._value.get()
@@ -416,7 +416,7 @@ class TestPushSubscriptionsAPI(BaseTest):
mock_capture.assert_not_called()
assert counter._value.get() == before + 1
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_discard_is_logged_once_per_window_and_counted_every_time(self, mock_capture: MagicMock):
# The log names the project behind the discards, so it has to survive. It also has to stay
# bounded: discarding is this endpoint's most common request, so a line per discard restates
@@ -445,7 +445,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert discard_logs[0]["app_id"] == "nonexistent-project"
assert counter._value.get() == before + 7
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_configured_app_registers_on_the_cached_path(self, mock_capture: MagicMock):
# The first request fills the team's app_id cache and the second reads it. A short-circuit
# that got those ids wrong would discard registrations the team is entitled to, and the device
@@ -463,8 +463,8 @@ class TestPushSubscriptionsAPI(BaseTest):
assert mock_capture.call_count == 2
- @patch("products.messaging.backend.api.push_subscriptions.cache")
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.cache")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_configured_app_registers_when_the_cache_is_unavailable(
self, mock_capture: MagicMock, mock_cache: MagicMock
):
@@ -486,7 +486,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_200_OK
mock_capture.assert_called_once()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_team_isolation(self, mock_capture: MagicMock):
other_team = Team.objects.create(organization=self.organization, name="Other Team")
Integration.objects.create(
@@ -522,7 +522,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_200_OK
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_gzip_compressed_body(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
@@ -546,7 +546,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.json()["distinct_id"] == "user-1"
mock_capture.assert_called_once()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_oversized_body_is_rejected_before_parsing(self, mock_capture: MagicMock):
# A body over the cap is rejected before load_data_from_request decompresses it, so a
# compressed body can't inflate into a memory-exhaustion payload.
@@ -568,7 +568,7 @@ class TestPushSubscriptionsAPI(BaseTest):
# No shared secret is set: ES256 must verify on the public key alone.
set_team_in_cache(self.team.api_token, self.team)
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_required_mode_accepts_an_es256_token_signed_by_the_registered_public_key(self, mock_capture: MagicMock):
# End to end through the endpoint: the integration holds only the EC public key, the device
# presents a token its backend signed with the private key, and registration succeeds. Guards
@@ -592,7 +592,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_200_OK
mock_capture.assert_called_once()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_required_mode_rejects_registration_without_a_token(self, mock_capture: MagicMock):
self._enable_identity_verification("required")
@@ -609,7 +609,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.json()["code"] == "identity_verification_failed"
mock_capture.assert_not_called()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_required_mode_rejects_a_token_minted_for_another_distinct_id(self, mock_capture: MagicMock):
# The takeover guard: a token the attacker legitimately minted for their own distinct_id
# cannot authorize binding a device to the victim's distinct_id.
@@ -729,7 +729,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
assert counter._value.get() == before + 1
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_optional_mode_stores_even_without_a_token(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
self._enable_identity_verification("optional")
@@ -746,7 +746,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_200_OK
mock_capture.assert_called_once()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_required_mode_rejects_unregister_without_a_token(self, mock_capture: MagicMock):
self._enable_identity_verification("required")
@@ -762,7 +762,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_401_UNAUTHORIZED
mock_capture.assert_not_called()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_required_mode_accepts_a_valid_token_for_unregister(self, mock_capture: MagicMock):
mock_capture.return_value = MagicMock(status_code=200)
private_pem, public_pem = _es256_keypair()
@@ -782,7 +782,7 @@ class TestPushSubscriptionsAPI(BaseTest):
assert response.status_code == status.HTTP_200_OK
mock_capture.assert_called_once()
- @patch("products.messaging.backend.api.push_subscriptions.capture_internal")
+ @patch("products.messaging.backend.presentation.views.push_subscriptions.capture_internal")
def test_strictest_mode_wins_when_two_integrations_share_an_app_id(self, mock_capture: MagicMock):
# project_id/bundle_id aren't unique, so an app_id can match several integrations. Resolution
# must fail closed: a second integration with the same project_id and verification disabled
diff --git a/products/messaging/backend/unlayer.py b/products/messaging/backend/unlayer.py
index 6d39d677573d..9265f9d621a0 100644
--- a/products/messaging/backend/unlayer.py
+++ b/products/messaging/backend/unlayer.py
@@ -5,6 +5,8 @@ from django.conf import settings
import requests
+from products.messaging.backend.facade.contracts import UnlayerNotConfiguredError, UnlayerRenderError
+
# Unlayer renders synchronously; exports of large designs take a few seconds.
EXPORT_TIMEOUT_SECONDS = 30
@@ -16,18 +18,6 @@ EXPORT_TIMEOUT_SECONDS = 30
CUSTOM_TOOL_HTML_OPTIONS = {"unsubscribe_link": "unsubscribe_link_content"}
-class UnlayerError(Exception):
- pass
-
-
-class UnlayerNotConfiguredError(UnlayerError):
- pass
-
-
-class UnlayerRenderError(UnlayerError):
- pass
-
-
def _dicts(value: Any) -> list[dict[str, Any]]:
return [item for item in value if isinstance(item, dict)] if isinstance(value, list) else []
diff --git a/products/workflows/backend/presentation/views/hog_flow.py b/products/workflows/backend/presentation/views/hog_flow.py
index ed73dc79160d..4afda0454d0c 100644
--- a/products/workflows/backend/presentation/views/hog_flow.py
+++ b/products/workflows/backend/presentation/views/hog_flow.py
@@ -97,7 +97,7 @@ from products.access_control.backend.presentation.access_control import (
AccessControlViewSetMixin,
UserAccessControlSerializerMixin,
)
-from products.messaging.backend.presentation.serializers import DesignOperationSerializer
+from products.messaging.backend.presentation.views.serializers import DesignOperationSerializer
from products.notifications.backend.facade.api import publish_resource_edited
from products.tasks.backend.facade.api import list_workflow_last_runs
from products.tasks.backend.facade.contracts import WorkflowLastRunDTO
diff --git a/pyproject.toml b/pyproject.toml
index 2bf11a0397ec..9a09a52ce669 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -745,12 +745,30 @@ ignore_imports = [
"products.batch_exports.backend.presentation.views.file_download -> products.batch_exports.backend.service",
"products.batch_exports.backend.presentation.views.file_download -> products.batch_exports.backend.temporal.record_batch_model",
"products.batch_exports.backend.presentation.views.file_download -> products.batch_exports.backend.temporal.sql.common",
- # TODO: messaging presentation wave — the recipient preferences page moved out of posthog/views.py
- # unchanged, so it still reads messaging models and services directly. Route each read through
- # the facade and delete each entry. Only delete entries here: a new import goes through the facade.
+ # TODO: messaging presentation wave — the moved views still read messaging models and services
+ # directly. Each lane thins its own modules to parse -> facade -> serialize and deletes only its
+ # own entries, keeping its header. backend:contract-check stays off until this list is empty.
+ # Only delete entries here: a new import from these views goes through the facade.
+ # Lane A: suppression
+ "products.messaging.backend.presentation.views.message_suppression -> products.messaging.backend.models.message_suppression",
+ # Lane B: categories, Customer.io config and the Customer.io webhook
+ "products.messaging.backend.presentation.views.customerio_webhook -> products.messaging.backend.models.message_category",
+ "products.messaging.backend.presentation.views.customerio_webhook -> products.messaging.backend.models.message_preferences",
+ "products.messaging.backend.presentation.views.customerio_webhook -> products.messaging.backend.models.optout_sync_config",
+ "products.messaging.backend.presentation.views.message_categories -> products.messaging.backend.models.message_category",
+ "products.messaging.backend.presentation.views.message_categories -> products.messaging.backend.models.optout_sync_config",
+ "products.messaging.backend.presentation.views.message_categories -> products.messaging.backend.services.customerio_import_service",
+ # Lane C: preferences and the recipient preferences page
+ "products.messaging.backend.presentation.views.message_preferences -> products.messaging.backend.models.message_category",
+ "products.messaging.backend.presentation.views.message_preferences -> products.messaging.backend.models.message_preferences",
+ "products.messaging.backend.presentation.views.message_preferences -> products.messaging.backend.services.opt_out_service",
+ "products.messaging.backend.presentation.views.message_preferences -> products.messaging.backend.tasks",
"products.messaging.backend.presentation.views.recipient_preferences -> products.messaging.backend.models.message_category",
"products.messaging.backend.presentation.views.recipient_preferences -> products.messaging.backend.models.message_preferences",
"products.messaging.backend.presentation.views.recipient_preferences -> products.messaging.backend.services.customerio_sync_service",
+ # Lane D: templates
+ "products.messaging.backend.presentation.views.message_templates -> products.messaging.backend.models.message_category",
+ "products.messaging.backend.presentation.views.message_templates -> products.messaging.backend.models.message_template",
]
# routes.py is the one module core reads to assemble a product's API router, so it is the
@@ -799,10 +817,6 @@ ignore_imports = [
"products.managed_migrations.backend.routes -> products.managed_migrations.backend.api.batch_imports",
"products.managed_migrations.backend.routes -> products.managed_migrations.backend.api.support_batch_imports",
"products.marketing_analytics.backend.routes -> products.marketing_analytics.backend.api",
- "products.messaging.backend.routes -> products.messaging.backend.api.message_categories",
- "products.messaging.backend.routes -> products.messaging.backend.api.message_preferences",
- "products.messaging.backend.routes -> products.messaging.backend.api.message_suppression",
- "products.messaging.backend.routes -> products.messaging.backend.api.message_templates",
"products.posthog_ai.backend.routes -> products.posthog_ai.backend.api",
"products.product_tours.backend.routes -> products.product_tours.backend.api",
"products.pulse.backend.routes -> products.pulse.backend.api.brief",
diff --git a/tach.toml b/tach.toml
index 6c736c84e66c..455e29cbdecb 100644
--- a/tach.toml
+++ b/tach.toml
@@ -1080,7 +1080,7 @@ expose = [
"backend\\.routes.*",
]
from = [
- "products\\.(autoresearch|batch_exports|canvas|customer_analytics|data_modeling|data_warehouse|demo|endpoints|error_tracking|experiments|managed_warehouse|mcp_analytics|mcp_store|metrics|notebooks|notifications|streamlit_apps|tasks|tracing|visual_review|warehouse_sources|workflows)",
+ "products\\.(autoresearch|batch_exports|canvas|customer_analytics|data_modeling|data_warehouse|demo|endpoints|error_tracking|experiments|managed_warehouse|mcp_analytics|mcp_store|messaging|metrics|notebooks|notifications|streamlit_apps|tasks|tracing|visual_review|warehouse_sources|workflows)",
]
# TODO: Experiments-specific temporary interface