replace get_suffix() by User.get_random_alias_suffix()

This commit is contained in:
Son NK 2021-06-27 17:51:13 +02:00
parent 09d00df363
commit 01815b9153
3 changed files with 17 additions and 21 deletions

View File

@ -23,7 +23,6 @@ from app.models import (
AliasMailbox, AliasMailbox,
DomainDeletedAlias, DomainDeletedAlias,
) )
from app.utils import get_suffix
signer = TimestampSigner(CUSTOM_ALIAS_SECRET) 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 each user domain, generate both the domain and a random suffix version
for custom_domain in user_custom_domains: for custom_domain in user_custom_domains:
if custom_domain.random_prefix_generation: 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) suffix_info = SuffixInfo(True, suffix, signer.sign(suffix).decode(), False)
if user.default_alias_custom_domain_id == custom_domain.id: if user.default_alias_custom_domain_id == custom_domain.id:
suffixes.insert(0, suffix_info) suffixes.insert(0, suffix_info)
@ -77,7 +76,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]:
# then SimpleLogin domain # then SimpleLogin domain
for sl_domain in user.get_sl_domains(): for sl_domain in user.get_sl_domains():
suffix = ( suffix = (
("" if DISABLE_ALIAS_SUFFIX else "." + get_suffix(user)) ("" if DISABLE_ALIAS_SUFFIX else "." + user.get_random_alias_suffix())
+ "@" + "@"
+ sl_domain.domain + sl_domain.domain
) )

View File

@ -24,6 +24,7 @@ from app.config import (
FIRST_ALIAS_DOMAIN, FIRST_ALIAS_DOMAIN,
DISABLE_ONBOARDING, DISABLE_ONBOARDING,
UNSUBSCRIBER, UNSUBSCRIBER,
ALIAS_RANDOM_SUFFIX_LENGTH,
) )
from app.errors import AliasInTrashError from app.errors import AliasInTrashError
from app.extensions import db from app.extensions import db
@ -35,7 +36,7 @@ from app.utils import (
random_string, random_string,
random_words, random_words,
sanitize_email, sanitize_email,
get_suffix, random_word,
) )
@ -758,6 +759,17 @@ class User(db.Model, ModelMixin, UserMixin, PasswordOracle):
> 0 > 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): def __repr__(self):
return f"<User {self.id} {self.name} {self.email}>" return f"<User {self.id} {self.name} {self.email}>"
@ -1147,7 +1159,7 @@ class Alias(db.Model, ModelMixin):
# find the right suffix - avoid infinite loop by running this at max 1000 times # find the right suffix - avoid infinite loop by running this at max 1000 times
for i in range(1000): for i in range(1000):
suffix = get_suffix(user) suffix = user.get_random_alias_suffix()
email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}" email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}"
if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email): if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email):

View File

@ -4,9 +4,8 @@ import urllib.parse
from unidecode import unidecode 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 .log import LOG
from .models import User, AliasSuffixEnum
with open(WORDS_FILE_PATH) as f: with open(WORDS_FILE_PATH) as f:
LOG.d("load words file: %s", WORDS_FILE_PATH) LOG.d("load words file: %s", WORDS_FILE_PATH)
@ -17,20 +16,6 @@ def random_word():
return random.choice(_words) 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): def word_exist(word):
return word in _words return word in _words