: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): confirm suppression removal and remove in bulk (#130345)
Meikel Ratz6ac29ac482d4
diff --git a/products/messaging/backend/facade/suppression.py b/products/messaging/backend/facade/suppression.pyindex 02d241a18059..69da8b164689 100644--- a/products/messaging/backend/facade/suppression.py+++ b/products/messaging/backend/facade/suppression.py@@ -34,6 +34,15 @@ class AddedSuppression:created: bool+@frozen+class RemovedSuppressions:+ removed: list[str]+ not_found: list[str]+++ComplaintConfirmationRequired = suppression_service.ComplaintConfirmationRequired++def _to_contract(row: MessageSuppression) -> Suppression:return Suppression(id=row.id,@@ -61,6 +70,7 @@ def add_manual_suppression(team_id: int, identifier: str, created_by_id: int | Nreturn AddedSuppression(suppression=_to_contract(row), created=created)-def remove_suppression(team_id: int, identifier: str) -> bool:- """Un-suppress `identifier`. Returns False when the team has no row for it."""- return suppression_service.remove_suppression(team_id, identifier)+def remove_suppressions(team_id: int, identifiers: list[str], confirm_complaints: bool) -> RemovedSuppressions:+ """Un-suppress `identifiers`. Raises `ComplaintConfirmationRequired` for an unconfirmed spam complaint."""+ removed = suppression_service.remove_suppressions(team_id, identifiers, confirm_complaints)+ return RemovedSuppressions(removed=removed, not_found=sorted(set(identifiers) - set(removed)))diff --git a/products/messaging/backend/models/email_hard_bounce.py b/products/messaging/backend/models/email_hard_bounce.pyindex d06e0daac68f..2f7bd28ea947 100644--- a/products/messaging/backend/models/email_hard_bounce.py+++ b/products/messaging/backend/models/email_hard_bounce.py@@ -1,5 +1,10 @@+from datetime import timedelta+from django.db import models+# The Node `EmailSuppressionService` holds a global entry for at most this long after its last bounce.+GLOBAL_HARD_BOUNCE_MAX_HOLD = timedelta(days=14)+class EmailHardBounce(models.Model):"""@@ -8,6 +13,10 @@ class EmailHardBounce(models.Model):team that removed the address from its own suppression list. A later successful delivery from any teamproves the address exists, and deletes the row.+ An entry only holds for as long as the address has been bouncing, from 1 day up to 14 days after its last+ bounce. After that the next send tries the address again, and a new bounce renews the entry with a longer+ hold. The Node `EmailSuppressionService` applies the hold when it reads the table.+The table has no team_id because the fact belongs to the address, not to a team."""diff --git a/products/messaging/backend/presentation/views/message_suppression.py b/products/messaging/backend/presentation/views/message_suppression.pyindex 2989e58c2cf1..9f2b65533124 100644--- a/products/messaging/backend/presentation/views/message_suppression.py+++ b/products/messaging/backend/presentation/views/message_suppression.py@@ -3,7 +3,7 @@ from typing import Anyfrom drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schemafrom rest_framework import serializers, status, viewsetsfrom rest_framework.decorators import action-from rest_framework.exceptions import PermissionDenied+from rest_framework.exceptions import PermissionDenied, ValidationErrorfrom rest_framework.request import Requestfrom rest_framework.response import Response@@ -14,10 +14,12 @@ from posthog.api.routing import TeamAndOrgViewSetMixinfrom products.messaging.backend.facade.suppression import (SUPPRESSION_SOURCE_CHOICES,+ ComplaintConfirmationRequired,+ RemovedSuppressions,Suppression,add_manual_suppression,list_active_suppressions,- remove_suppression,+ remove_suppressions,)@@ -95,6 +97,43 @@ class AddSuppressionRequestSerializer(serializers.Serializer):)+CONFIRM_COMPLAINTS_HELP_TEXT = (+ "Set to true to remove addresses that are suppressed because the recipient reported a message as spam. "+ "Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing "+ "and returns 400."+)++MAX_BULK_REMOVE = 100+++class RemoveSuppressionRequestSerializer(serializers.Serializer):+ identifier = serializers.CharField(max_length=512, help_text="The email address to remove.")+ confirm_complaints = serializers.BooleanField(required=False, default=False, help_text=CONFIRM_COMPLAINTS_HELP_TEXT)+++class BulkRemoveSuppressionsRequestSerializer(serializers.Serializer):+ identifiers = serializers.ListField(+ child=serializers.CharField(max_length=512),+ min_length=1,+ max_length=MAX_BULK_REMOVE,+ help_text=f"The email addresses to remove, at most {MAX_BULK_REMOVE} per request.",+ )+ confirm_complaints = serializers.BooleanField(required=False, default=False, help_text=CONFIRM_COMPLAINTS_HELP_TEXT)+++class BulkRemoveSuppressionsResponseSerializer(serializers.Serializer):+ removed = serializers.ListField(+ child=serializers.CharField(), help_text="Addresses removed from the suppression list."+ )+ not_found = serializers.ListField(+ child=serializers.CharField(), help_text="Addresses that were not on the suppression list."+ )+++def _normalized(identifier: str) -> str:+ return identifier.strip().lower()++class MessageSuppressionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):"""Per-team email suppression list. Addresses here are skipped before send.@@ -105,9 +144,9 @@ class MessageSuppressionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):scope_object = "hog_flow"# Custom actions must declare their write status so TeamAndOrgViewSetMixin's AccessControlPermission- # checks hog_flow:write on the mutating endpoints. `suppressions` is scoped by its own- # `required_scopes` instead, since it needs person:read on top of the workflow scope.- scope_object_write_actions = ["add_suppression", "remove_suppression"]+ # checks hog_flow:write on the mutating endpoints. `suppressions` and the removals also need person:read in their+ # `required_scopes`, because their responses tell which addresses are on the list and which reported spam.+ scope_object_write_actions = ["add_suppression", "remove_suppression", "bulk_remove_suppressions"]serializer_class = _FallbackSerializer@validated_request(@@ -155,40 +194,69 @@ class MessageSuppressionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):@action(detail=False, methods=["post"])def add_suppression(self, request: Request, **kwargs: Any) -> Response:"""Manually suppress an email address so no workflow sends to it."""- # Team-wide mutation with no per-workflow object — require project-wide hog_flow editor- # access. Otherwise an editor grant on one workflow could add arbitrary addresses to the- # team's suppression list, blocking unrelated (including transactional) delivery.- if not self.user_access_control.check_access_level_for_resource("hog_flow", "editor"):- raise PermissionDenied("You need hog_flow editor access to modify the suppression list.")-+ self._require_editor_access()serializer = AddSuppressionRequestSerializer(data=request.data)serializer.is_valid(raise_exception=True)- identifier = serializer.validated_data["identifier"].strip().lower()-- added = add_manual_suppression(self.team_id, identifier, request.user.id)+ added = add_manual_suppression(+ self.team_id, _normalized(serializer.validated_data["identifier"]), request.user.id+ )response_status = status.HTTP_201_CREATED if added.created else status.HTTP_200_OKreturn Response(MessageSuppressionSerializer(added.suppression).data, status=response_status)@extend_schema(- request=AddSuppressionRequestSerializer,+ request=RemoveSuppressionRequestSerializer,responses={204: None},summary="Remove an email address from the suppression list",)- @action(detail=False, methods=["post"])+ @action(detail=False, methods=["post"], required_scopes=["hog_flow:write", "person:read"])def remove_suppression(self, request: Request, **kwargs: Any) -> Response:"""Remove an address from the suppression list so it can receive messages again."""- # Same rationale as add_suppression — this is a team-wide mutation.- if not self.user_access_control.check_access_level_for_resource("hog_flow", "editor"):- raise PermissionDenied("You need hog_flow editor access to modify the suppression list.")+ self._require_editor_access()+ serializer = RemoveSuppressionRequestSerializer(data=request.data)+ serializer.is_valid(raise_exception=True)- serializer = AddSuppressionRequestSerializer(data=request.data)+ result = self._remove(+ [_normalized(serializer.validated_data["identifier"])], serializer.validated_data["confirm_complaints"]+ )+ if not result.removed:+ return Response({"error": "Suppression not found"}, status=status.HTTP_404_NOT_FOUND)+ return Response(status=status.HTTP_204_NO_CONTENT)++ @extend_schema(+ request=BulkRemoveSuppressionsRequestSerializer,+ responses={200: BulkRemoveSuppressionsResponseSerializer},+ summary="Remove several email addresses from the suppression list",+ )+ @action(detail=False, methods=["post"], required_scopes=["hog_flow:write", "person:read"])+ def bulk_remove_suppressions(self, request: Request, **kwargs: Any) -> Response:+ """Remove up to 100 addresses from the suppression list so they can receive messages again."""+ self._require_editor_access()+ serializer = BulkRemoveSuppressionsRequestSerializer(data=request.data)serializer.is_valid(raise_exception=True)- identifier = serializer.validated_data["identifier"].strip().lower()+ identifiers = list({_normalized(identifier) for identifier in serializer.validated_data["identifiers"]})+ result = self._remove(identifiers, serializer.validated_data["confirm_complaints"])+ return Response(BulkRemoveSuppressionsResponseSerializer(result).data)- if not remove_suppression(self.team_id, identifier):- return Response({"error": "Suppression not found"}, status=status.HTTP_404_NOT_FOUND)+ def _require_editor_access(self) -> None:+ # Team-wide mutation with no per-workflow object — require project-wide hog_flow editor+ # access. Otherwise an editor grant on one workflow could add arbitrary addresses to the+ # team's suppression list, blocking unrelated (including transactional) delivery.+ if not self.user_access_control.check_access_level_for_resource("hog_flow", "editor"):+ raise PermissionDenied("You need hog_flow editor access to modify the suppression list.")- return Response(status=status.HTTP_204_NO_CONTENT)+ def _remove(self, identifiers: list[str], confirm_complaints: bool) -> RemovedSuppressions:+ try:+ return remove_suppressions(self.team_id, identifiers, confirm_complaints)+ except ComplaintConfirmationRequired as error:+ raise ValidationError(+ {+ "confirm_complaints": (+ "These addresses are suppressed because the recipient reported a message as spam: "+ f"{', '.join(error.identifiers)}. Set confirm_complaints to true to remove them."+ )+ },+ code="complaint_confirmation_required",+ )diff --git a/products/messaging/backend/services/suppression.py b/products/messaging/backend/services/suppression.pyindex 38da2fdab431..9699223fbbb6 100644--- a/products/messaging/backend/services/suppression.py+++ b/products/messaging/backend/services/suppression.py@@ -1,8 +1,9 @@+from django.db import transactionfrom django.db.models import F, QuerySetfrom django.db.models.functions import Coalesce, Nowfrom django.utils import timezone-from products.messaging.backend.models.email_hard_bounce import EmailHardBounce+from products.messaging.backend.models.email_hard_bounce import GLOBAL_HARD_BOUNCE_MAX_HOLD, EmailHardBouncefrom products.messaging.backend.models.message_suppression import MessageSuppression, SuppressionSource@@ -44,7 +45,39 @@ def add_manual_suppression(team_id: int, identifier: str, created_by_id: int | Nreturn suppression, created-def remove_suppression(team_id: int, identifier: str) -> bool:+class ComplaintConfirmationRequired(Exception):+ def __init__(self, identifiers: list[str]) -> None:+ super().__init__(f"Removing spam complaints needs confirmation: {', '.join(identifiers)}")+ self.identifiers = identifiers+++def remove_suppressions(team_id: int, identifiers: list[str], confirm_complaints: bool) -> list[str]:+ """Remove `identifiers` from the team's list, and return the ones the team had a row for.++ Raises `ComplaintConfirmationRequired`, and removes nothing, when a spam complaint is among them unconfirmed.+ """+ with transaction.atomic():+ rows = MessageSuppression.objects.for_team(team_id)+ # The lock stops a complaint that lands during the check from being removed unconfirmed. A row inserted after+ # the lock is not checked, so only the locked rows are removed.+ locked = list(+ rows.filter(identifier__in=identifiers)+ .order_by("identifier")+ .select_for_update()+ .values_list("id", "identifier", "source", "suppressed", "deleted")+ )+ complaints = sorted(+ identifier+ for _, identifier, source, suppressed, deleted in locked+ if source == SuppressionSource.COMPLAINT and suppressed and not deleted+ )+ if complaints and not confirm_complaints:+ raise ComplaintConfirmationRequired(complaints)+ _remove(rows.filter(id__in=[row_id for row_id, *_ in locked]))+ return sorted(identifier for _, identifier, *_ in locked)+++def _remove(rows: QuerySet[MessageSuppression]) -> None:# Soft-delete and un-suppress. Reset the bounce counter and the hard-bounce mark so a# previously-dead address that a user deliberately re-enables starts from a clean slate. The reason is cleared too,# because the node writes treat a BOUNCE row with a legacy hard-bounce reason as hard-bounced, and the next soft@@ -52,28 +85,25 @@ def remove_suppression(team_id: int, identifier: str) -> bool:# a future auto-suppression can re-suppress this row — the node upserts skip rows with# source='MANUAL' (to protect user-managed entries), so a removed MANUAL row would otherwise# be permanently invisible to the bounce-driven write path.- updated = (- MessageSuppression.objects.for_team(team_id)- .filter(identifier=identifier)- .update(- suppressed=False,- deleted=True,- transient_bounce_count=0,- hard_bounced_at=None,- reason=None,- source=SuppressionSource.BOUNCE,- updated_at=timezone.now(),- )+ rows.update(+ suppressed=False,+ deleted=True,+ transient_bounce_count=0,+ hard_bounced_at=None,+ reason=None,+ source=SuppressionSource.BOUNCE,+ updated_at=timezone.now(),)- return bool(updated)def add_global_hard_bounces(emails: list[str]) -> None:"""Add addresses to the global hard-bounce list that every team's sends skip. Keeps existing entries as they are."""identifiers = sorted({email.strip().lower() for email in emails if email.strip()})now = timezone.now()- # An existing row came from a real bounce and carries better times, so it wins.+ # The source list keeps no bounce history, so each address gets the longest hold rather than the one-day hold of a+ # single bounce. An existing row came from a real bounce and carries better times, so it wins.+ first_bounced_at = now - GLOBAL_HARD_BOUNCE_MAX_HOLDEmailHardBounce.objects.bulk_create(- [EmailHardBounce(email=email, first_bounced_at=now, last_bounced_at=now) for email in identifiers],+ [EmailHardBounce(email=email, first_bounced_at=first_bounced_at, last_bounced_at=now) for email in identifiers],ignore_conflicts=True,)diff --git a/products/messaging/backend/tests/api/test_message_suppression.py b/products/messaging/backend/tests/api/test_message_suppression.pyindex 6f191aa0d697..76f865a186d8 100644--- a/products/messaging/backend/tests/api/test_message_suppression.py+++ b/products/messaging/backend/tests/api/test_message_suppression.py@@ -46,6 +46,28 @@ class TestSuppressionListPersonalAPIKeyAccess(APIBaseTest):if expected_status == 200:assert set(response.json()) == {"count", "next", "previous", "results"}+ @parameterized.expand(+ [+ ("remove_suppression", {"identifier": "[email protected]"}, ["hog_flow:write", "person:read"], 404),+ ("remove_suppression", {"identifier": "[email protected]"}, ["hog_flow:write"], 403),+ ("bulk_remove_suppressions", {"identifiers": ["[email protected]"]}, ["hog_flow:write", "person:read"], 200),+ ("bulk_remove_suppressions", {"identifiers": ["[email protected]"]}, ["hog_flow:write"], 403),+ ]+ )+ def test_removal_requires_person_read(+ self, action: str, body: dict, scopes: list[str], expected_status: int+ ) -> None:+ self.client.logout()++ response = self.client.post(+ f"/api/projects/{self.team.id}/messaging_suppressions/{action}/",+ body,+ format="json",+ HTTP_AUTHORIZATION=f"Bearer {self._key(scopes)}",+ )++ assert response.status_code == expected_status+class TestSuppressionListSearch(APIBaseTest):def test_search_filters_by_identifier_within_suppressed_rows(self) -> None:@@ -127,3 +149,81 @@ class TestRemoveSuppressionResetsSource(APIBaseTest):), ("remove_suppression must reset the row so the node write path can auto-suppress this address again if it later bounces")+++class TestRemoveSuppressions(APIBaseTest):+ def _url(self, action: str) -> str:+ return f"/api/projects/{self.team.id}/messaging_suppressions/{action}/"++ def _suppress(self, identifier: str, source: SuppressionSource) -> None:+ MessageSuppression.objects.for_team(self.team.id).create(+ team_id=self.team.id, identifier=identifier, source=source, suppressed=True, suppressed_at=timezone.now()+ )++ def _suppressed(self) -> set[str]:+ return set(+ MessageSuppression.objects.for_team(self.team.id)+ .filter(suppressed=True, deleted=False)+ .values_list("identifier", flat=True)+ )++ @parameterized.expand(+ [+ ("unconfirmed", {}, 400, {"[email protected]"}),+ ("confirmed", {"confirm_complaints": True}, 204, set()),+ ]+ )+ def test_removing_a_complaint_needs_confirmation(+ self, _name: str, extra: dict, expected_status: int, still_suppressed: set[str]+ ) -> None:+ self._suppress("[email protected]", SuppressionSource.COMPLAINT)++ response = self.client.post(+ self._url("remove_suppression"), {"identifier": "[email protected]", **extra}, format="json"+ )++ assert response.status_code == expected_status+ assert self._suppressed() == still_suppressed++ @parameterized.expand(+ [+ (+ "removes what is listed and reports the rest",+ 200,+ {"[email protected]"},+ ),+ (+ "removes nothing while a complaint is unconfirmed",+ {"identifiers": ["[email protected]", "[email protected]"]},+ 400,+ ),+ (+ "removes a confirmed complaint",+ {"identifiers": ["[email protected]", "[email protected]"], "confirm_complaints": True},+ 200,+ {"[email protected]"},+ ),+ ]+ )+ def test_bulk_remove(self, _name: str, body: dict, expected_status: int, still_suppressed: set[str]) -> None:+ self._suppress("[email protected]", SuppressionSource.BOUNCE)+ self._suppress("[email protected]", SuppressionSource.MANUAL)+ self._suppress("[email protected]", SuppressionSource.COMPLAINT)++ response = self.client.post(self._url("bulk_remove_suppressions"), body, format="json")++ assert response.status_code == expected_status+ assert self._suppressed() == still_suppressed++ def test_bulk_remove_reports_removed_and_unknown_addresses(self) -> None:+ self._suppress("[email protected]", SuppressionSource.BOUNCE)++ response = self.client.post(+ self._url("bulk_remove_suppressions"),+ {"identifiers": ["[email protected]", "[email protected]"]},+ format="json",+ )++ assert response.json() == {"removed": ["[email protected]"], "not_found": ["[email protected]"]}diff --git a/products/messaging/backend/tests/api/test_message_suppression_unit.py b/products/messaging/backend/tests/api/test_message_suppression_unit.pyindex 24909d4c0d0e..c7413806dc0d 100644--- a/products/messaging/backend/tests/api/test_message_suppression_unit.py+++ b/products/messaging/backend/tests/api/test_message_suppression_unit.py@@ -18,4 +18,5 @@ class TestMessageSuppressionViewSetScope(SimpleTestCase):assert set(MessageSuppressionViewSet.scope_object_write_actions) == {"add_suppression","remove_suppression",+ "bulk_remove_suppressions",}diff --git a/products/messaging/backend/tests/test_import_ses_hard_bounces.py b/products/messaging/backend/tests/test_import_ses_hard_bounces.pyindex d13287cdd1bb..33bac673e8f7 100644--- a/products/messaging/backend/tests/test_import_ses_hard_bounces.py+++ b/products/messaging/backend/tests/test_import_ses_hard_bounces.py@@ -6,7 +6,7 @@ from unittest.mock import MagicMock, patchfrom django.core.management import call_commandfrom django.utils import timezone-from products.messaging.backend.models.email_hard_bounce import EmailHardBounce+from products.messaging.backend.models.email_hard_bounce import GLOBAL_HARD_BOUNCE_MAX_HOLD, EmailHardBounceclass TestImportSesHardBounces(BaseTest):@@ -32,6 +32,8 @@ class TestImportSesHardBounces(BaseTest):rows = {row.email: row.first_bounced_at for row in EmailHardBounce.objects.all()}assert rows["[email protected]"] == bounced_at+ imported = EmailHardBounce.objects.get(email="[email protected]")+ assert imported.last_bounced_at - imported.first_bounced_at == GLOBAL_HARD_BOUNCE_MAX_HOLDcalls = client_factory.return_value.list_suppressed_destinations.call_args_listassert [call.kwargs for call in calls] == [{"Reasons": ["BOUNCE"], "PageSize": 1000},diff --git a/products/messaging/frontend/generated/api.schemas.ts b/products/messaging/frontend/generated/api.schemas.tsindex 2ecfd7c2cc67..834381f7259c 100644--- a/products/messaging/frontend/generated/api.schemas.ts+++ b/products/messaging/frontend/generated/api.schemas.ts@@ -645,6 +645,35 @@ export interface MessageSuppressionApi {readonly updated_at: string}+export interface BulkRemoveSuppressionsRequestApi {+ /**+ * The email addresses to remove, at most 100 per request.+ * @minItems 1+ * @maxItems 100+ * @items.maxLength 512+ */+ identifiers: string[]+ /** Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400. */+ confirm_complaints?: boolean+}++export interface BulkRemoveSuppressionsResponseApi {+ /** Addresses removed from the suppression list. */+ removed: string[]+ /** Addresses that were not on the suppression list. */+ not_found: string[]+}++export interface RemoveSuppressionRequestApi {+ /**+ * The email address to remove.+ * @maxLength 512+ */+ identifier: string+ /** Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400. */+ confirm_complaints?: boolean+}+/*** OpenAPI shape for the paginated suppressions response. Declared so drf-spectacular emits* the {count, next, previous, results} envelope on the generated client, rather than a barediff --git a/products/messaging/frontend/generated/api.ts b/products/messaging/frontend/generated/api.tsindex b3aff9984455..15e37e6f5169 100644--- a/products/messaging/frontend/generated/api.ts+++ b/products/messaging/frontend/generated/api.ts@@ -13,6 +13,8 @@ import type {AddSuppressionRequestApi,BulkAddOptOutsRequestApi,BulkAddOptOutsResultApi,+ BulkRemoveSuppressionsRequestApi,+ BulkRemoveSuppressionsResponseApi,EmailStatusApi,GenerateLinkRequestApi,ImportProviderOptionApi,@@ -37,6 +39,7 @@ import type {PreferencesLinkApi,ProviderImportRunApi,RemoveOptOutRequestApi,+ RemoveSuppressionRequestApi,SendEmailApi,SendTransactionalEmailApi,SendTransactionalEmailResponseApi,@@ -777,6 +780,30 @@ export const messagingSuppressionsAddSuppressionCreate = async (})}+export const getMessagingSuppressionsBulkRemoveSuppressionsCreateUrl = (projectId: string) => {+ return `/api/projects/${projectId}/messaging_suppressions/bulk_remove_suppressions/`+}++/**+ * Remove up to 100 addresses from the suppression list so they can receive messages again.+ * @summary Remove several email addresses from the suppression list+ */+export const messagingSuppressionsBulkRemoveSuppressionsCreate = async (+ projectId: string,+ bulkRemoveSuppressionsRequestApi: BulkRemoveSuppressionsRequestApi,+ options?: RequestInit+): Promise<BulkRemoveSuppressionsResponseApi> => {+ return apiMutator<BulkRemoveSuppressionsResponseApi>(+ getMessagingSuppressionsBulkRemoveSuppressionsCreateUrl(projectId),+ {+ ...options,+ method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(bulkRemoveSuppressionsRequestApi),+ }+ )+}+export const getMessagingSuppressionsRemoveSuppressionCreateUrl = (projectId: string) => {return `/api/projects/${projectId}/messaging_suppressions/remove_suppression/`}@@ -787,14 +814,14 @@ export const getMessagingSuppressionsRemoveSuppressionCreateUrl = (projectId: st*/export const messagingSuppressionsRemoveSuppressionCreate = async (projectId: string,- addSuppressionRequestApi: AddSuppressionRequestApi,+ removeSuppressionRequestApi: RemoveSuppressionRequestApi,options?: RequestInit): Promise<void> => {return apiMutator<void>(getMessagingSuppressionsRemoveSuppressionCreateUrl(projectId), {...options,method: 'POST',headers: { 'Content-Type': 'application/json', ...options?.headers },- body: JSON.stringify(addSuppressionRequestApi),+ body: JSON.stringify(removeSuppressionRequestApi),})}diff --git a/products/messaging/frontend/generated/api.zod.ts b/products/messaging/frontend/generated/api.zod.tsindex a487441b2840..8c6823e6f009 100644--- a/products/messaging/frontend/generated/api.zod.ts+++ b/products/messaging/frontend/generated/api.zod.ts@@ -588,17 +588,49 @@ export const MessagingSuppressionsAddSuppressionCreateBody = /* @__PURE__ */ zod.describe('The email address to suppress. Will not receive any messages until removed.'),})+/**+ * Remove up to 100 addresses from the suppression list so they can receive messages again.+ * @summary Remove several email addresses from the suppression list+ */+export const messagingSuppressionsBulkRemoveSuppressionsCreateBodyIdentifiersItemMax = 512++export const messagingSuppressionsBulkRemoveSuppressionsCreateBodyIdentifiersMax = 100++export const messagingSuppressionsBulkRemoveSuppressionsCreateBodyConfirmComplaintsDefault = false++export const MessagingSuppressionsBulkRemoveSuppressionsCreateBody = /* @__PURE__ */ zod.object({+ identifiers: zod+ .array(zod.string().max(messagingSuppressionsBulkRemoveSuppressionsCreateBodyIdentifiersItemMax))+ .min(1)+ .max(messagingSuppressionsBulkRemoveSuppressionsCreateBodyIdentifiersMax)+ .describe('The email addresses to remove, at most 100 per request.'),+ confirm_complaints: zod+ .boolean()+ .default(messagingSuppressionsBulkRemoveSuppressionsCreateBodyConfirmComplaintsDefault)+ .describe(+ 'Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400.'+ ),+})+/*** Remove an address from the suppression list so it can receive messages again.* @summary Remove an email address from the suppression list*/export const messagingSuppressionsRemoveSuppressionCreateBodyIdentifierMax = 512+export const messagingSuppressionsRemoveSuppressionCreateBodyConfirmComplaintsDefault = false+export const MessagingSuppressionsRemoveSuppressionCreateBody = /* @__PURE__ */ zod.object({identifier: zod.string().max(messagingSuppressionsRemoveSuppressionCreateBodyIdentifierMax)- .describe('The email address to suppress. Will not receive any messages until removed.'),+ .describe('The email address to remove.'),+ confirm_complaints: zod+ .boolean()+ .default(messagingSuppressionsRemoveSuppressionCreateBodyConfirmComplaintsDefault)+ .describe(+ 'Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400.'+ ),})export const messagingTemplatesCreateBodyNameMax = 400diff --git a/products/workflows/backend/tests/api/test_message_suppression_access_control.py b/products/workflows/backend/tests/api/test_message_suppression_access_control.pyindex 4839e5477d2d..68ebf920f449 100644--- a/products/workflows/backend/tests/api/test_message_suppression_access_control.py+++ b/products/workflows/backend/tests/api/test_message_suppression_access_control.py@@ -104,3 +104,10 @@ class TestMessageSuppressionAccessControl(APIBaseTest):).status_code== remove_status)+ bulk_remove_status = status.HTTP_200_OK if remove_status == status.HTTP_204_NO_CONTENT else remove_status+ assert (+ self.client.post(+ self._url("bulk_remove_suppressions"), {"identifiers": [f"{grant_type}@example.com"]}, format="json"+ ).status_code+ == bulk_remove_status+ )diff --git a/products/workflows/frontend/Suppression/SuppressionList.tsx b/products/workflows/frontend/Suppression/SuppressionList.tsxindex be362fd1e23b..2fbbc18d9d98 100644--- a/products/workflows/frontend/Suppression/SuppressionList.tsx+++ b/products/workflows/frontend/Suppression/SuppressionList.tsx@@ -20,7 +20,7 @@ export function SuppressionList(): JSX.Element {setShowAddModal,setNewIdentifier,addSuppression,- removeSuppression,+ confirmRemove,setSearchTerm,} = useActions(suppressionListLogic)const {@@ -29,7 +29,7 @@ export function SuppressionList(): JSX.Element {currentPage,showAddModal,addSuppressionLoading,- removeSuppressionLoading,+ removedSuppressionsLoading,newIdentifier,searchTerm,} = useValues(suppressionListLogic)@@ -74,9 +74,9 @@ export function SuppressionList(): JSX.Element {<LemonButtonstatus="danger"icon={<IconTrash />}- onClick={() => removeSuppression(entry.identifier)}- loading={removeSuppressionLoading}- disabledReason={removeSuppressionLoading ? 'Removing…' : undefined}+ onClick={() => confirmRemove([entry])}+ loading={removedSuppressionsLoading}+ disabledReason={removedSuppressionsLoading ? 'Removing…' : undefined}fullWidth>Remove@@ -122,6 +122,24 @@ export function SuppressionList(): JSX.Element {loading={suppressionsLoading}loadingSkeletonRows={3}rowKey="identifier"+ bulkSelection={{+ noun: ['address', 'addresses'],+ clearSelectionKey: suppressions,+ rowAriaLabel: (entry: MessageSuppressionApi) => `Select ${entry.identifier}`,+ headerAriaLabel: 'Select all addresses on this page',+ renderActions: ({ selectedRecords }) => (+ <LemonButton+ icon={<IconTrash />}+ size="small"+ type="secondary"+ status="danger"+ onClick={() => confirmRemove(selectedRecords)}+ loading={removedSuppressionsLoading}+ >+ Remove+ </LemonButton>+ ),+ }}emptyState={searchTerm.trim()? 'No suppressed addresses match your search'diff --git a/products/workflows/frontend/Suppression/suppressionListLogic.test.ts b/products/workflows/frontend/Suppression/suppressionListLogic.test.tsindex 2bcae00e9671..7ea7519536fd 100644--- a/products/workflows/frontend/Suppression/suppressionListLogic.test.ts+++ b/products/workflows/frontend/Suppression/suppressionListLogic.test.ts@@ -1,16 +1,18 @@import { expectLogic } from 'kea-test-utils'+import { LemonDialog } from '@posthog/lemon-ui'+import { initKeaTests } from '~/test/init'import * as messagingApi from 'products/messaging/frontend/generated/api'import type { PaginatedMessageSuppressionApi } from 'products/messaging/frontend/generated/api.schemas'-import { suppressionListLogic } from './suppressionListLogic'+import { removalWarning, suppressionListLogic } from './suppressionListLogic'-const suppressionRow = (identifier: string): any => ({+const suppressionRow = (identifier: string, source = 'MANUAL'): any => ({id: identifier,identifier,- source: 'MANUAL',+ source,reason: '',transient_bounce_count: 0,last_bounce_at: null,@@ -109,4 +111,50 @@ describe('suppressionListLogic', () => {expect(logic.values.currentPage).toBe(1)})})+ describe('removal warning', () => {+ it.each([+ ['a bounced address', [suppressionRow('[email protected]', 'BOUNCE')], 'Remove', false],+ ['a spam complaint', [suppressionRow('[email protected]', 'COMPLAINT')], 'Remove anyway', true],+ [+ 'a selection with one spam complaint',+ [suppressionRow('[email protected]', 'MANUAL'), suppressionRow('[email protected]', 'COMPLAINT')],+ 'Remove anyway',+ true,+ ],+ ])('warns about %s', (_label, entries, confirmLabel, mentionsSpam) => {+ const warning = removalWarning(entries)++ expect(warning.confirmLabel).toBe(confirmLabel)+ expect(warning.description.includes('as spam')).toBe(mentionsSpam)+ expect(warning.description).toContain('back on the list')+ })+ })+ describe('confirmed removal', () => {+ it.each([+ ['a bounced address', 'BOUNCE', false],+ ['a spam complaint', 'COMPLAINT', true],+ ])('sends the complaint confirmation only for %s', async (_label, source, confirmComplaints) => {+ jest.spyOn(messagingApi, 'messagingSuppressionsSuppressionsRetrieve').mockResolvedValue({+ count: 0,+ next: null,+ previous: null,+ results: [],+ })+ const remove = jest+ .spyOn(messagingApi, 'messagingSuppressionsBulkRemoveSuppressionsCreate')+ .mockResolvedValue({ removed: ['[email protected]'], not_found: [] })+ const openDialog = jest.spyOn(LemonDialog, 'open').mockImplementation(() => {})+ const logic = suppressionListLogic()+ logic.mount()++ logic.actions.confirmRemove([suppressionRow('[email protected]', source)])+ openDialog.mock.calls[0][0].primaryButton?.onClick?.({} as any)+ await expectLogic(logic).toDispatchActions(['removeSuppressionsSuccess'])++ expect(remove).toHaveBeenCalledWith(expect.any(String), {+ identifiers: ['[email protected]'],+ confirm_complaints: confirmComplaints,+ })+ })+ })})diff --git a/products/workflows/frontend/Suppression/suppressionListLogic.ts b/products/workflows/frontend/Suppression/suppressionListLogic.tsindex c77938e024c5..289a46979932 100644--- a/products/workflows/frontend/Suppression/suppressionListLogic.ts+++ b/products/workflows/frontend/Suppression/suppressionListLogic.ts@@ -1,13 +1,13 @@import { MakeLogicType, actions, afterMount, connect, isBreakpoint, kea, listeners, path, reducers } from 'kea'import { loaders } from 'kea-loaders'-import { lemonToast } from '@posthog/lemon-ui'+import { LemonDialog, lemonToast } from '@posthog/lemon-ui'import { projectLogic } from 'scenes/projectLogic'import {messagingSuppressionsAddSuppressionCreate,- messagingSuppressionsRemoveSuppressionCreate,+ messagingSuppressionsBulkRemoveSuppressionsCreate,messagingSuppressionsSuppressionsRetrieve,} from 'products/messaging/frontend/generated/api'import type {@@ -24,8 +24,8 @@ export interface suppressionListLogicValues {addSuppressionLoading: booleancurrentPage: numbernewIdentifier: string- removeSuppression: string | null- removeSuppressionLoading: boolean+ removedSuppressions: string[]+ removedSuppressionsLoading: booleansearchTerm: stringshowAddModal: booleansuppressions: PaginatedMessageSuppressionApi@@ -49,6 +49,9 @@ export interface suppressionListLogicActions {addSuppression: MessageSuppressionApipayload?: string}+ confirmRemove: (entries: readonly MessageSuppressionApi[]) => {+ entries: readonly MessageSuppressionApi[]+ }loadNextPage: () => {value: true}@@ -76,20 +79,29 @@ export interface suppressionListLogicActions {page: number}}- removeSuppression: (identifier: string) => string- removeSuppressionFailure: (+ removeSuppressions: ({ identifiers, confirmComplaints }: { confirmComplaints: boolean; identifiers: string[] }) => {+ identifiers: string[]+ confirmComplaints: boolean+ }+ removeSuppressionsFailure: (error: string,errorObject?: any) => {error: stringerrorObject?: any}- removeSuppressionSuccess: (- removeSuppression: string,- payload?: string+ removeSuppressionsSuccess: (+ removedSuppressions: string[],+ payload?: {+ identifiers: string[]+ confirmComplaints: boolean+ }) => {- removeSuppression: string- payload?: string+ removedSuppressions: string[]+ payload?: {+ identifiers: string[]+ confirmComplaints: boolean+ }}setCurrentPage: (page: number) => {page: number@@ -121,6 +133,28 @@ const fetchPage = async (})}+export function removalWarning(entries: readonly MessageSuppressionApi[]): {+ title: string+ description: string+ confirmLabel: string+} {+ const single = entries.length === 1+ const title = single+ ? `Remove ${entries[0].identifier} from the suppression list?`+ : `Remove ${entries.length} addresses from the suppression list?`+ const resend = single+ ? 'Your workflows will send to this address again. If it bounces again, it goes back on the list.'+ : 'Your workflows will send to these addresses again. Any that bounce again go back on the list.'+ if (!entries.some((entry) => entry.source === 'COMPLAINT')) {+ return { title, description: resend, confirmLabel: 'Remove' }+ }+ return {+ title,+ description: `${single ? 'This recipient' : 'Some of these recipients'} reported your emails as spam. Sending to them again can hurt your sender reputation, so more of your email may land in spam. ${resend}`,+ confirmLabel: 'Remove anyway',+ }+}+export const suppressionListLogic = kea<suppressionListLogicType>([path(['products', 'workflows', 'frontend', 'Suppression', 'suppressionListLogic']),connect(() => ({@@ -134,6 +168,7 @@ export const suppressionListLogic = kea<suppressionListLogicType>([setShowAddModal: (show: boolean) => ({ show }),setNewIdentifier: (identifier: string) => ({ identifier }),setSearchTerm: (searchTerm: string) => ({ searchTerm }),+ confirmRemove: (entries: readonly MessageSuppressionApi[]) => ({ entries }),}),reducers({currentPage: [@@ -209,19 +244,36 @@ export const suppressionListLogic = kea<suppressionListLogicType>([}},},- removeSuppression: {- __default: null as string | null,- removeSuppression: async (identifier: string): Promise<string> => {+ removedSuppressions: {+ __default: [] as string[],+ removeSuppressions: async ({+ identifiers,+ confirmComplaints,+ }: {+ identifiers: string[]+ confirmComplaints: boolean+ }): Promise<string[]> => {if (values.currentProjectId === null) {throw new Error('No current project')}try {- await messagingSuppressionsRemoveSuppressionCreate(String(values.currentProjectId), { identifier })- lemonToast.success(`${identifier} removed from suppression list`)- actions.loadSuppressions()- return identifier+ const { removed } = await messagingSuppressionsBulkRemoveSuppressionsCreate(+ String(values.currentProjectId),+ { identifiers, confirm_complaints: confirmComplaints }+ )+ lemonToast.success(+ removed.length === 0+ ? 'Already removed from the suppression list'+ : removed.length === 1+ ? `${removed[0]} removed from the suppression list`+ : `${removed.length} addresses removed from the suppression list`+ )+ // Removing every row on the last page leaves that page empty, and the API rejects an empty page.+ const pageEmptied = removed.length >= (values.suppressions.results?.length ?? 0)+ actions.loadSuppressions(pageEmptied ? Math.max(1, values.currentPage - 1) : values.currentPage)+ return removed} catch (e) {- lemonToast.error('Failed to remove from suppression list')+ lemonToast.error('Failed to remove from the suppression list. Reload the list and try again.')throw e}},@@ -234,6 +286,24 @@ export const suppressionListLogic = kea<suppressionListLogicType>([},loadNextPage: () => actions.loadSuppressions(values.currentPage + 1),loadPreviousPage: () => actions.loadSuppressions(Math.max(1, values.currentPage - 1)),+ confirmRemove: ({ entries }) => {+ const { title, description, confirmLabel } = removalWarning(entries)+ const confirmComplaints = entries.some((entry) => entry.source === 'COMPLAINT')+ LemonDialog.open({+ title,+ description,+ primaryButton: {+ children: confirmLabel,+ status: 'danger',+ onClick: () =>+ actions.removeSuppressions({+ identifiers: entries.map((entry) => entry.identifier),+ confirmComplaints,+ }),+ },+ secondaryButton: { children: 'Cancel' },+ })+ },})),afterMount(({ actions }) => {actions.loadSuppressions()diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex f959ff134e6e..97da62bd6db8 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -21436,6 +21436,25 @@ export namespace Schemas {results: BulkObserveResult[];}+ export interface BulkRemoveSuppressionsRequest {+ /**+ * The email addresses to remove, at most 100 per request.+ * @minItems 1+ * @maxItems 100+ * @items.maxLength 512+ */+ identifiers: string[];+ /** Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400. */+ confirm_complaints?: boolean;+ }++ export interface BulkRemoveSuppressionsResponse {+ /** Addresses removed from the suppression list. */+ removed: string[];+ /** Addresses that were not on the suppression list. */+ not_found: string[];+ }+/*** * `new` - New* * `open` - Open@@ -99321,6 +99340,16 @@ export namespace Schemas {category_key?: string;}+ export interface RemoveSuppressionRequest {+ /**+ * The email address to remove.+ * @maxLength 512+ */+ identifier: string;+ /** Set to true to remove addresses that are suppressed because the recipient reported a message as spam. Mailing them again can hurt your sender reputation. Without it, a request that includes one removes nothing and returns 400. */+ confirm_complaints?: boolean;+ }+export interface ReorderTilesRequest {/*** Array of tile IDs in the desired display order (top to bottom, left to right).