use custom domain to generate a random alias if user enables this option

This commit is contained in:
Son NK 2020-06-25 13:05:25 +02:00
parent abe9768db4
commit b2f82ba4a8

View file

@ -656,17 +656,20 @@ class OauthToken(db.Model, ModelMixin):
def generate_email( def generate_email(
scheme: int = AliasGeneratorEnum.word.value, in_hex: bool = False scheme: int = AliasGeneratorEnum.word.value,
in_hex: bool = False,
alias_domain=FIRST_ALIAS_DOMAIN,
) -> str: ) -> str:
"""generate an email address that does not exist before """generate an email address that does not exist before
:param alias_domain: the domain used to generate the alias.
:param scheme: int, value of AliasGeneratorEnum, indicate how the email is generated :param scheme: int, value of AliasGeneratorEnum, indicate how the email is generated
:type in_hex: bool, if the generate scheme is uuid, is hex favorable? :type in_hex: bool, if the generate scheme is uuid, is hex favorable?
""" """
if scheme == AliasGeneratorEnum.uuid.value: if scheme == AliasGeneratorEnum.uuid.value:
name = uuid.uuid4().hex if in_hex else uuid.uuid4().__str__() name = uuid.uuid4().hex if in_hex else uuid.uuid4().__str__()
random_email = name + "@" + FIRST_ALIAS_DOMAIN random_email = name + "@" + alias_domain
else: else:
random_email = random_words() + "@" + FIRST_ALIAS_DOMAIN random_email = random_words() + "@" + alias_domain
random_email = random_email.lower().strip() random_email = random_email.lower().strip()
@ -800,14 +803,28 @@ class Alias(db.Model, ModelMixin):
note: str = None, note: str = None,
): ):
"""create a new random alias""" """create a new random alias"""
domain = None
if user.default_random_alias_domain_id:
domain = CustomDomain.get(user.default_random_alias_domain_id)
random_email = generate_email(
scheme=scheme, in_hex=in_hex, alias_domain=domain.domain
)
else:
random_email = generate_email(scheme=scheme, in_hex=in_hex) random_email = generate_email(scheme=scheme, in_hex=in_hex)
return Alias.create(
alias = Alias.create(
user_id=user.id, user_id=user.id,
email=random_email, email=random_email,
mailbox_id=user.default_mailbox_id, mailbox_id=user.default_mailbox_id,
note=note, note=note,
) )
if domain:
alias.custom_domain_id = domain.id
return alias
def mailbox_email(self): def mailbox_email(self):
if self.mailbox_id: if self.mailbox_id:
return self.mailbox.email return self.mailbox.email
@ -1257,7 +1274,7 @@ class CustomDomain(db.Model, ModelMixin):
# an alias is created automatically the first time it receives an email # an alias is created automatically the first time it receives an email
catch_all = db.Column(db.Boolean, nullable=False, default=False, server_default="0") catch_all = db.Column(db.Boolean, nullable=False, default=False, server_default="0")
user = db.relationship(User) user = db.relationship(User, foreign_keys=[user_id])
def nb_alias(self): def nb_alias(self):
return Alias.filter_by(custom_domain_id=self.id).count() return Alias.filter_by(custom_domain_id=self.id).count()