Skip to content

Commit

Permalink
MPP-3119: add disable_mask_on_complaint waffle flag
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Sep 24, 2024
1 parent a156885 commit 324695d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
6 changes: 5 additions & 1 deletion emails/tests/views_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from markus.main import MetricsRecord
from markus.testing import MetricsMock
from model_bakery import baker
from waffle.testutils import override_flag

from emails.models import (
DeletedAddress,
Expand Down Expand Up @@ -1139,6 +1140,7 @@ def test_complaint_log_with_optout(self) -> None:
assert log_data["user_match"] == "found"
assert not log_data["fxa_id"]

@override_flag("disable_mask_on_complaint", active=True)
def test_complaint_disables_mask(self):
"""
A notificationType of complaint:
Expand Down Expand Up @@ -1168,7 +1170,9 @@ def test_complaint_disables_mask(self):

self.ra.refresh_from_db()
source = self.mock_ses_client.send_raw_email.call_args.kwargs["Source"]
destinations = self.mock_ses_client.send_raw_email.call_args.kwargs["Destinations"]
destinations = self.mock_ses_client.send_raw_email.call_args.kwargs[
"Destinations"
]
raw_message = self.mock_ses_client.send_raw_email.call_args.kwargs["RawMessage"]
data_without_newlines = raw_message["Data"].replace("\n", "")

Expand Down
43 changes: 28 additions & 15 deletions emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from decouple import strtobool
from markus.utils import generate_tag
from sentry_sdk import capture_message
from waffle import sample_is_active
from waffle import get_waffle_flag_model, sample_is_active

from privaterelay.ftl_bundles import main as ftl_bundle
from privaterelay.models import Profile
Expand Down Expand Up @@ -1640,6 +1640,24 @@ def _send_disabled_mask_for_spam_email(
incr_if_enabled("free_user_reply_attempt", 1)


def _disable_masks_for_complaint(message_json: dict) -> None:
for destination_address in message_json.get("mail", {}).get("destination", []):
try:
address = _get_address(destination_address, False)
address.enabled = False
address.save()
_send_disabled_mask_for_spam_email(address, message_json.get("mail", {}))
except (
ObjectDoesNotExist,
RelayAddress.DoesNotExist,
DomainAddress.DoesNotExist,
):
logger.error(
"Received a complaint from a destination address that does not match "
"a Relay address.",
)


def _handle_complaint(message_json: AWS_SNSMessageJSON) -> HttpResponse:
"""
Handle an AWS SES complaint notification.
Expand Down Expand Up @@ -1711,21 +1729,16 @@ def _handle_complaint(message_json: AWS_SNSMessageJSON) -> HttpResponse:
profile.auto_block_spam = True
profile.save()

for destination_address in message_json.get("mail", {}).get("destination", []):
try:
address = _get_address(destination_address, False)
address.enabled = False
address.save()
_send_disabled_mask_for_spam_email(address, message_json.get("mail", {}))
except (
ObjectDoesNotExist,
RelayAddress.DoesNotExist,
DomainAddress.DoesNotExist,
):
logger.error(
"Received a complaint from a destination address that does not match "
"a Relay address.",
disable_mask_on_complaint_flag, _ = get_waffle_flag_model().objects.get_or_create(
name="disable_mask_on_complaint",
defaults={
"note": (
"MPP-3119: When a Relay user marks an email as spam, disable the mask."
)
},
)
if disable_mask_on_complaint_flag.is_active_for_user(user):
_disable_masks_for_complaint(message_json)

if not complaint_data:
# Data when there are no identified recipients
Expand Down

0 comments on commit 324695d

Please sign in to comment.