: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): add MessageSuppression model + migrations (#71965)
Meikel Ratz5a2d6282f206
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yamlindex b6b51ef37b25..d0a2d7ff4ab5 100644--- a/.semgrep/rules/security/idor-team-scoped-models.yaml+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml@@ -198,6 +198,7 @@ rules:|MarketingAnalyticsGoalMapping|MessageCategory|MessageRecipientPreference+ |MessageSuppression|MessageTemplate|Metric|RelationshipProposal@@ -501,6 +502,7 @@ rules:|MarketingAnalyticsGoalMapping|MessageCategory|MessageRecipientPreference+ |MessageSuppression|MessageTemplate|Metric|RelationshipProposaldiff --git a/products/messaging/backend/migrations/0003_message_suppression.py b/products/messaging/backend/migrations/0003_message_suppression.pynew file mode 100644index 000000000000..0e4a3c4c9235--- /dev/null+++ b/products/messaging/backend/migrations/0003_message_suppression.py@@ -0,0 +1,79 @@+# Generated by Django 5.2.14 on 2026-07-17 14:27++import django.db.models.deletion+from django.conf import settings+from django.db import migrations, models++import posthog.uuidt+++class Migration(migrations.Migration):+ dependencies = [+ ("messaging", "0002_optout_sync_config"),+ ("posthog", "1256_userproductlist_default_reason"),+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name="MessageSuppression",+ fields=[+ (+ "id",+ models.UUIDField(+ default=posthog.uuidt.uuid7,+ editable=False,+ primary_key=True,+ serialize=False,+ ),+ ),+ ("created_at", models.DateTimeField(auto_now_add=True)),+ ("updated_at", models.DateTimeField(auto_now=True)),+ ("deleted", models.BooleanField(default=False)),+ ("identifier", models.CharField(max_length=512)),+ (+ "source",+ models.CharField(+ choices=[("BOUNCE", "Bounce"), ("MANUAL", "Manual")],+ default="BOUNCE",+ max_length=16,+ ),+ ),+ ("reason", models.TextField(blank=True, null=True)),+ ("transient_bounce_count", models.IntegerField(default=0)),+ ("last_bounce_at", models.DateTimeField(blank=True, null=True)),+ ("last_bounce_diagnostic", models.TextField(blank=True, null=True)),+ ("suppressed", models.BooleanField(default=False)),+ ("suppressed_at", models.DateTimeField(blank=True, null=True)),+ (+ "created_by",+ models.ForeignKey(+ blank=True,+ db_constraint=False,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to=settings.AUTH_USER_MODEL,+ ),+ ),+ (+ "team",+ models.ForeignKey(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ to="posthog.team",+ ),+ ),+ ],+ options={+ "db_table": "posthog_messagesuppression",+ "indexes": [+ models.Index(+ condition=models.Q(("deleted", False), ("suppressed", True)),+ fields=["team", "-updated_at"],+ name="pmsg_supp_active_by_updated",+ )+ ],+ "unique_together": {("team", "identifier")},+ },+ ),+ ]diff --git a/products/messaging/backend/migrations/max_migration.txt b/products/messaging/backend/migrations/max_migration.txtindex 860050a92ef4..a079707d6adf 100644--- a/products/messaging/backend/migrations/max_migration.txt+++ b/products/messaging/backend/migrations/max_migration.txt@@ -1 +1 @@-0002_optout_sync_config+0003_message_suppressiondiff --git a/products/messaging/backend/models/__init__.py b/products/messaging/backend/models/__init__.pyindex 9cbdf378335f..d79860723e8e 100644--- a/products/messaging/backend/models/__init__.py+++ b/products/messaging/backend/models/__init__.py@@ -1,5 +1,6 @@from .message_category import MessageCategory, MessageCategoryTypefrom .message_preferences import ALL_MESSAGE_PREFERENCE_CATEGORY_ID, MessageRecipientPreference, PreferenceStatus+from .message_suppression import MessageSuppression, SuppressionSourcefrom .message_template import MessageTemplatefrom .optout_sync_config import OptOutSyncConfig@@ -8,7 +9,9 @@ __all__ = ["MessageCategory","MessageCategoryType","MessageRecipientPreference",+ "MessageSuppression","MessageTemplate","OptOutSyncConfig","PreferenceStatus",+ "SuppressionSource",]diff --git a/products/messaging/backend/models/message_suppression.py b/products/messaging/backend/models/message_suppression.pynew file mode 100644index 000000000000..1f925f74fe4a--- /dev/null+++ b/products/messaging/backend/models/message_suppression.py@@ -0,0 +1,79 @@+from django.db import models+from django.db.models import Q++from posthog.models.scoping.root_mixin import TeamScopedRootMixin+from posthog.models.utils import UUIDModel+++class SuppressionSource(models.TextChoices):+ # Added automatically after an address repeatedly soft-bounced (see the transient+ # bounce handling in the SES webhook path).+ BOUNCE = "BOUNCE"+ # Added by a user via the Suppression list tab / API.+ MANUAL = "MANUAL"+++class MessageSuppression(TeamScopedRootMixin, UUIDModel):+ """+ Per-team list of email addresses we should not send to because they can't (or shouldn't)+ receive mail. Two ways an address lands here:++ - Automatically, when it soft-bounces (`Transient`) on `transient_bounce_count` consecutive+ sends without a successful delivery in between. A single soft bounce is not enough — the+ recipient server may just be briefly down — so we count and only suppress once the count+ crosses a configurable threshold. Any successful delivery resets the count.+ - Manually, when a user adds it in the Suppression list UI.++ The pre-send path consults this list (see the Node `EmailSuppressionService`) and skips+ matching recipients. Unlike opt-outs, a suppression is a deliverability signal, not a+ messaging preference, so it applies regardless of message category (including transactional).+ """++ # db_constraint=False on these hot-table FKs so CreateModel takes no lock on+ # posthog_team / posthog_user (see HotTableAlterPolicy / safe-django-migrations).+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False)+ created_at = models.DateTimeField(auto_now_add=True)+ updated_at = models.DateTimeField(auto_now=True)+ created_by = models.ForeignKey(+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False+ )+ deleted = models.BooleanField(default=False)++ # Lower-cased recipient email address.+ identifier = models.CharField(max_length=512)++ source = models.CharField(max_length=16, choices=SuppressionSource.choices, default=SuppressionSource.BOUNCE)+ reason = models.TextField(null=True, blank=True)++ # Rolling count of consecutive soft bounces with no successful delivery in between.+ # Reset to 0 whenever the address successfully delivers. Ignored for manual entries.+ transient_bounce_count = models.IntegerField(default=0)+ last_bounce_at = models.DateTimeField(null=True, blank=True)+ # Last SMTP diagnostic we saw, kept for visibility in the UI (e.g. the "Message expired" text).+ last_bounce_diagnostic = models.TextField(null=True, blank=True)++ # Whether the address is actively suppressed. A row can exist while still only counting+ # bounces (suppressed=False) before it crosses the threshold.+ suppressed = models.BooleanField(default=False)+ suppressed_at = models.DateTimeField(null=True, blank=True)++ class Meta:+ unique_together = (+ "team",+ "identifier",+ )+ db_table = "posthog_messagesuppression"+ indexes = [+ # Partial index for the Suppression list UI query:+ # WHERE team_id = ? AND suppressed = true AND deleted = false ORDER BY updated_at DESC+ # Small (only actively-suppressed rows) and sorted the way we read, so it also serves+ # the paginator COUNT without a full team scan.+ models.Index(+ fields=["team", "-updated_at"],+ name="pmsg_supp_active_by_updated",+ condition=Q(suppressed=True, deleted=False),+ ),+ ]++ def __str__(self) -> str:+ return f"Suppression for {self.identifier}"