replace random_word with get_suffix(user)

This commit is contained in:
devStorm 2021-05-26 22:29:00 -07:00
parent 4a0fc8380f
commit f7bef3941a
No known key found for this signature in database
GPG Key ID: D52E1B66F336AC57
4 changed files with 23 additions and 23 deletions

View File

@ -9,7 +9,7 @@ from app.dashboard.views.custom_alias import (
from app.extensions import db
from app.log import LOG
from app.models import AliasUsedOn, Alias, User
from app.utils import convert_to_id, random_word
from app.utils import convert_to_id, random_word, get_suffix
@api_bp.route("/alias/options")
@ -76,7 +76,7 @@ def options():
if DISABLE_ALIAS_SUFFIX:
ret["custom"]["suffixes"].append(f"@{domain}")
else:
ret["custom"]["suffixes"].append(f".{random_word()}@{domain}")
ret["custom"]["suffixes"].append(f".{get_suffix(user)}@{domain}")
for custom_domain in user.verified_custom_domains():
ret["custom"]["suffixes"].append("@" + custom_domain.domain)
@ -156,7 +156,7 @@ def options_v2():
if DISABLE_ALIAS_SUFFIX:
ret["suffixes"].append(f"@{domain}")
else:
ret["suffixes"].append(f".{random_word()}@{domain}")
ret["suffixes"].append(f".{get_suffix(user)}@{domain}")
for custom_domain in user.verified_custom_domains():
ret["suffixes"].append("@" + custom_domain.domain)
@ -232,7 +232,7 @@ def options_v3():
if DISABLE_ALIAS_SUFFIX:
ret["suffixes"].append(f"@{domain}")
else:
ret["suffixes"].append(f".{random_word()}@{domain}")
ret["suffixes"].append(f".{get_suffix(user)}@{domain}")
for custom_domain in user.verified_custom_domains():
ret["suffixes"].append("@" + custom_domain.domain)

View File

@ -10,7 +10,6 @@ from app.config import (
DISABLE_ALIAS_SUFFIX,
CUSTOM_ALIAS_SECRET,
ALIAS_LIMIT,
ALIAS_RANDOM_SUFFIX_LENGTH,
)
from app.dashboard.base import dashboard_bp
from app.extensions import db, limiter
@ -23,9 +22,8 @@ from app.models import (
User,
AliasMailbox,
DomainDeletedAlias,
AliasSuffixEnum,
)
from app.utils import random_word, random_string
from app.utils import get_suffix
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
@ -252,20 +250,6 @@ def custom_alias():
)
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 verify_prefix_suffix(user: User, alias_prefix, alias_suffix) -> bool:
"""verify if user could create an alias with the given prefix and suffix"""
if not alias_prefix or not alias_suffix: # should be caught on frontend

View File

@ -36,6 +36,7 @@ from app.utils import (
random_words,
random_word,
sanitize_email,
get_suffix,
)
@ -1124,7 +1125,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 = random_word()
suffix = get_suffix(user)
email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}"
if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email):

View File

@ -4,8 +4,9 @@ import urllib.parse
from unidecode import unidecode
from .config import WORDS_FILE_PATH
from .config import WORDS_FILE_PATH, ALIAS_RANDOM_SUFFIX_LENGTH
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)
@ -16,6 +17,20 @@ 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