Skip to content

Commit

Permalink
Make RelayAddress with different domains
Browse files Browse the repository at this point in the history
  • Loading branch information
say-yawn committed Jul 29, 2021
1 parent 0ab81b7 commit c8a0310
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions emails/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,18 @@ def add_subdomain(self, subdomain):
return subdomain


def address_hash(address, subdomain=None):
def address_hash(address, subdomain=None, domain=DOMAIN_DEFAULT):
if subdomain:
return sha256(
f'{address}@{subdomain}'.encode('utf-8')
f'{address}@{subdomain}.{domain}'.encode('utf-8')
).hexdigest()
return sha256(
if domain == DOMAIN_DEFAULT:
return sha256(
f'{address}'.encode('utf-8')
).hexdigest()
return sha256(
f'{address}@{domain}'.encode('utf-8')
).hexdigest()


def address_default():
Expand Down Expand Up @@ -230,7 +234,7 @@ def delete(self, *args, **kwargs):
profile.save()
return super(RelayAddress, self).delete(*args, **kwargs)

def make_relay_address(user_profile, num_tries=0):
def make_relay_address(user_profile, num_tries=0, domain=DOMAIN_DEFAULT):
if (
user_profile.at_max_free_aliases
and not user_profile.has_unlimited
Expand All @@ -241,10 +245,10 @@ def make_relay_address(user_profile, num_tries=0):
)
if num_tries >= 5:
raise CannotMakeAddressException
relay_address = RelayAddress.objects.create(user=user_profile.user)
relay_address = RelayAddress.objects.create(user=user_profile.user, domain=domain)
address_contains_badword = has_bad_words(relay_address.address)
address_already_deleted = DeletedAddress.objects.filter(
address_hash=address_hash(relay_address.address)
address_hash=address_hash(relay_address.address, domain=domain)
).count()
if address_already_deleted > 0 or address_contains_badword:
relay_address.delete()
Expand Down
3 changes: 2 additions & 1 deletion emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def _index_POST(request):

with transaction.atomic():
locked_profile = Profile.objects.select_for_update().get(
user=user_profile.user
user=user_profile.user,
domain=request_data.get('domain', get_email_domain_from_settings())
)
try:
relay_address = RelayAddress.make_relay_address(locked_profile)
Expand Down

0 comments on commit c8a0310

Please sign in to comment.