Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email_mirror: Remove limits (expiry, max uses) to improve usability.
Jason Hildebrand7efd59b6d7d7
diff --git a/zerver/lib/email_mirror.py b/zerver/lib/email_mirror.pyindex db2e1bdf33d..4988e0f5661 100644--- a/zerver/lib/email_mirror.py+++ b/zerver/lib/email_mirror.py@@ -6,8 +6,6 @@ from email.message import EmailMessagefrom typing import Dict, List, Match, Optional, Tuplefrom django.conf import settings-from django.utils.timezone import now as timezone_now-from django.utils.timezone import timedeltafrom zerver.actions.message_send import (check_send_message,@@ -134,18 +132,9 @@ def get_missed_message_token_from_address(address: str) -> str:def get_usable_missed_message_address(address: str) -> MissedMessageEmailAddress:token = get_missed_message_token_from_address(address)try:- mm_address = MissedMessageEmailAddress.objects.select_related().get(- email_token=token,- timestamp__gt=timezone_now()- - timedelta(seconds=MissedMessageEmailAddress.EXPIRY_SECONDS),- )+ mm_address = MissedMessageEmailAddress.objects.select_related().get(email_token=token)except MissedMessageEmailAddress.DoesNotExist:- raise ZulipEmailForwardUserError("Missed message address expired or doesn't exist.")-- if not mm_address.is_usable():- # Technical, this also checks whether the event is expired,- # but that case is excluded by the logic above.- raise ZulipEmailForwardUserError("Missed message address out of uses.")+ raise ZulipEmailForwardError("Zulip notification reply address is invalid.")return mm_addressdiff --git a/zerver/models.py b/zerver/models.pyindex 404fa7f6bba..9ede2165f20 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -4220,27 +4220,19 @@ class ScheduledEmail(AbstractScheduledJob):class MissedMessageEmailAddress(models.Model):- EXPIRY_SECONDS = 60 * 60 * 24 * 5- ALLOWED_USES = 1-id: int = models.AutoField(auto_created=True, primary_key=True, verbose_name="ID")message: Message = models.ForeignKey(Message, on_delete=CASCADE)user_profile: UserProfile = models.ForeignKey(UserProfile, on_delete=CASCADE)email_token: str = models.CharField(max_length=34, unique=True, db_index=True)# Timestamp of when the missed message address generated.- # The address is valid until timestamp + EXPIRY_SECONDS.timestamp: datetime.datetime = models.DateTimeField(db_index=True, default=timezone_now)+ # Number of times the missed message address has been used.times_used: int = models.PositiveIntegerField(default=0, db_index=True)def __str__(self) -> str:return settings.EMAIL_GATEWAY_PATTERN % (self.email_token,)- def is_usable(self) -> bool:- not_expired = timezone_now() <= self.timestamp + timedelta(seconds=self.EXPIRY_SECONDS)- has_uses_left = self.times_used < self.ALLOWED_USES- return has_uses_left and not_expired-def increment_times_used(self) -> None:self.times_used += 1self.save(update_fields=["times_used"])diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex 72d94c16ecf..23a81bbd0e2 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -39,7 +39,6 @@ from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import mock_queue_publish, most_recent_message, most_recent_usermessagefrom zerver.models import (Attachment,- MissedMessageEmailAddress,Recipient,Stream,UserProfile,@@ -1246,10 +1245,8 @@ class TestMissedMessageEmailMessages(ZulipTestCase):incoming_valid_message["To"] = mm_addressincoming_valid_message["Reply-to"] = user_profile.delivery_email- for i in range(0, MissedMessageEmailAddress.ALLOWED_USES):- process_missed_message(mm_address, incoming_valid_message)-- with self.assertRaises(ZulipEmailForwardError):+ # there is no longer a usage limit. Ensure we can send multiple times.+ for i in range(0, 5):process_missed_message(mm_address, incoming_valid_message)@@ -1502,22 +1499,18 @@ class TestEmailMirrorTornadoView(ZulipTestCase):def test_using_mm_address_multiple_times(self) -> None:mm_address = self.send_private_message()- for i in range(0, MissedMessageEmailAddress.ALLOWED_USES):+ # there is no longer a usage limit. Ensure we can send multiple times.+ for i in range(0, 5):result = self.send_offline_message(mm_address, self.example_user("cordelia"))self.assert_json_success(result)- result = self.send_offline_message(mm_address, self.example_user("cordelia"))- self.assert_json_error(- result, "5.1.1 Bad destination mailbox address: Missed message address out of uses."- )-def test_wrong_missed_email_private_message(self) -> None:self.send_private_message()mm_address = "mm" + ("x" * 32) + "@testserver"result = self.send_offline_message(mm_address, self.example_user("cordelia"))self.assert_json_error(result,- "5.1.1 Bad destination mailbox address: Missed message address expired or doesn't exist.",+ "5.1.1 Bad destination mailbox address: Zulip notification reply address is invalid.",)