Use a shorter suffix in case of custom domain (#1670)

This commit is contained in:
Son Nguyen Kim 2023-03-28 22:33:28 +02:00 committed by GitHub
parent c09b5bc526
commit 1f9d784382
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View file

@ -99,7 +99,12 @@ def get_alias_suffixes(user: User) -> [AliasSuffix]:
# for each user domain, generate both the domain and a random suffix version
for custom_domain in user_custom_domains:
if custom_domain.random_prefix_generation:
suffix = "." + user.get_random_alias_suffix() + "@" + custom_domain.domain
suffix = (
"."
+ user.get_random_alias_suffix(custom_domain)
+ "@"
+ custom_domain.domain
)
alias_suffix = AliasSuffix(
is_custom=True,
suffix=suffix,

View file

@ -1000,16 +1000,21 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
> 0
)
def get_random_alias_suffix(self):
def get_random_alias_suffix(self, custom_domain: Optional["CustomDomain"] = None):
"""Get random suffix for an alias based on user's preference.
Use a shorter suffix in case of custom domain
Returns:
str: the random suffix generated
"""
if self.random_alias_suffix == AliasSuffixEnum.random_string.value:
return random_string(config.ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
return random_words(1, 3)
if custom_domain is None:
return random_words(1, 3)
return random_words(1)
def __repr__(self):
return f"<User {self.id} {self.name} {self.email}>"