diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index e6da8f4b..48137182 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -23,7 +23,6 @@ from app.models import ( AliasMailbox, DomainDeletedAlias, ) -from app.utils import get_suffix signer = TimestampSigner(CUSTOM_ALIAS_SECRET) @@ -54,7 +53,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]: # 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 = "." + get_suffix(user) + "@" + custom_domain.domain + suffix = "." + user.get_random_alias_suffix() + "@" + custom_domain.domain suffix_info = SuffixInfo(True, suffix, signer.sign(suffix).decode(), False) if user.default_alias_custom_domain_id == custom_domain.id: suffixes.insert(0, suffix_info) @@ -77,7 +76,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]: # then SimpleLogin domain for sl_domain in user.get_sl_domains(): suffix = ( - ("" if DISABLE_ALIAS_SUFFIX else "." + get_suffix(user)) + ("" if DISABLE_ALIAS_SUFFIX else "." + user.get_random_alias_suffix()) + "@" + sl_domain.domain ) diff --git a/app/models.py b/app/models.py index 3ca044cf..f1e9f3bb 100644 --- a/app/models.py +++ b/app/models.py @@ -24,6 +24,7 @@ from app.config import ( FIRST_ALIAS_DOMAIN, DISABLE_ONBOARDING, UNSUBSCRIBER, + ALIAS_RANDOM_SUFFIX_LENGTH, ) from app.errors import AliasInTrashError from app.extensions import db @@ -35,7 +36,7 @@ from app.utils import ( random_string, random_words, sanitize_email, - get_suffix, + random_word, ) @@ -758,6 +759,17 @@ class User(db.Model, ModelMixin, UserMixin, PasswordOracle): > 0 ) + def get_random_alias_suffix(self): + """Get random suffix for an alias based on user's preference. + + + Returns: + str: the random suffix generated + """ + if self.random_alias_suffix == AliasSuffixEnum.random_string.value: + return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True) + return random_word() + def __repr__(self): return f"" @@ -1147,7 +1159,7 @@ class Alias(db.Model, ModelMixin): # find the right suffix - avoid infinite loop by running this at max 1000 times for i in range(1000): - suffix = get_suffix(user) + suffix = user.get_random_alias_suffix() email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}" if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email): diff --git a/app/utils.py b/app/utils.py index 743c0dea..10e3ab67 100644 --- a/app/utils.py +++ b/app/utils.py @@ -4,9 +4,8 @@ import urllib.parse from unidecode import unidecode -from .config import WORDS_FILE_PATH, ALIAS_RANDOM_SUFFIX_LENGTH +from .config import WORDS_FILE_PATH from .log import LOG -from .models import User, AliasSuffixEnum with open(WORDS_FILE_PATH) as f: LOG.d("load words file: %s", WORDS_FILE_PATH) @@ -17,20 +16,6 @@ def random_word(): return random.choice(_words) -def get_suffix(user: User) -> str: - """Get random suffix for an alias based on user's preference. - - Args: - user (User): the user who is trying to create an alias - - Returns: - str: the random suffix generated - """ - if user.random_alias_suffix == AliasSuffixEnum.random_string.value: - return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True) - return random_word() - - def word_exist(word): return word in _words