2021-11-12 09:45:31 +01:00
|
|
|
import tldextract
|
2020-01-05 21:15:16 +01:00
|
|
|
from flask import g
|
|
|
|
from flask import jsonify, request
|
|
|
|
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2020-05-23 19:18:24 +02:00
|
|
|
from app.api.serializer import (
|
|
|
|
get_alias_info_v2,
|
|
|
|
serialize_alias_info_v2,
|
|
|
|
)
|
2021-03-24 16:26:42 +01:00
|
|
|
from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_LIMIT
|
2021-11-12 11:04:00 +01:00
|
|
|
from app.dashboard.views.custom_alias import get_available_suffixes
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-11-17 10:56:43 +01:00
|
|
|
from app.errors import AliasInTrashError
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.extensions import limiter
|
2020-01-05 21:15:16 +01:00
|
|
|
from app.log import LOG
|
2020-03-17 11:51:40 +01:00
|
|
|
from app.models import Alias, AliasUsedOn, AliasGeneratorEnum
|
2021-11-12 09:45:31 +01:00
|
|
|
from app.utils import convert_to_id
|
2020-01-05 21:15:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/alias/random/new", methods=["POST"])
|
2021-03-24 16:26:42 +01:00
|
|
|
@limiter.limit(ALIAS_LIMIT)
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-01-05 21:15:16 +01:00
|
|
|
def new_random_alias():
|
|
|
|
"""
|
|
|
|
Create a new random alias
|
2020-03-11 12:24:30 +01:00
|
|
|
Input:
|
|
|
|
(Optional) note
|
2020-01-05 21:15:16 +01:00
|
|
|
Output:
|
|
|
|
201 if success
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
if not user.can_create_new_alias():
|
|
|
|
LOG.d("user %s cannot create new random alias", user)
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
error=f"You have reached the limitation of a free account with the maximum of "
|
|
|
|
f"{MAX_NB_EMAIL_FREE_PLAN} aliases, please upgrade your plan to create more aliases"
|
|
|
|
),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
2020-03-11 12:24:30 +01:00
|
|
|
note = None
|
2020-03-11 14:02:35 +01:00
|
|
|
data = request.get_json(silent=True)
|
2020-03-11 12:24:30 +01:00
|
|
|
if data:
|
|
|
|
note = data.get("note")
|
|
|
|
|
2021-11-12 11:04:00 +01:00
|
|
|
alias = None
|
|
|
|
|
2021-11-12 09:45:31 +01:00
|
|
|
# custom alias suggestion and suffix
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
if hostname and user.include_website_in_one_click_alias:
|
|
|
|
LOG.d("Use %s to create new alias", hostname)
|
|
|
|
# keep only the domain name of hostname, ignore TLD and subdomain
|
|
|
|
# for ex www.groupon.com -> groupon
|
|
|
|
ext = tldextract.extract(hostname)
|
|
|
|
prefix_suggestion = ext.domain
|
|
|
|
prefix_suggestion = convert_to_id(prefix_suggestion)
|
2020-02-07 15:30:46 +01:00
|
|
|
|
2021-11-12 11:04:00 +01:00
|
|
|
suffixes = get_available_suffixes(user)
|
|
|
|
# use the first suffix
|
|
|
|
suggested_alias = prefix_suggestion + suffixes[0].suffix
|
|
|
|
|
|
|
|
alias = Alias.get_by(email=suggested_alias)
|
|
|
|
|
|
|
|
# cannot use this alias as it belongs to another user
|
|
|
|
if alias and not alias.user_id == user.id:
|
|
|
|
LOG.d("%s belongs to another user", alias)
|
|
|
|
alias = None
|
|
|
|
elif alias and alias.user_id == user.id:
|
|
|
|
# make sure alias was created for this website
|
|
|
|
if AliasUsedOn.get_by(
|
|
|
|
alias_id=alias.id, hostname=hostname, user_id=alias.user_id
|
|
|
|
):
|
|
|
|
LOG.d("Use existing alias %s", alias)
|
|
|
|
else:
|
|
|
|
LOG.d("%s wasn't created for this website %s", alias, hostname)
|
|
|
|
alias = None
|
|
|
|
elif not alias:
|
|
|
|
LOG.d("create new alias %s", suggested_alias)
|
2021-11-17 10:56:43 +01:00
|
|
|
try:
|
|
|
|
alias = Alias.create(
|
|
|
|
user_id=user.id,
|
|
|
|
email=suggested_alias,
|
|
|
|
note=note,
|
|
|
|
mailbox_id=user.default_mailbox_id,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
except AliasInTrashError:
|
|
|
|
LOG.i("Alias %s is in trash", suggested_alias)
|
|
|
|
alias = None
|
2021-11-12 09:45:31 +01:00
|
|
|
|
2021-11-12 11:04:00 +01:00
|
|
|
if not alias:
|
2021-11-12 09:45:31 +01:00
|
|
|
scheme = user.alias_generator
|
|
|
|
mode = request.args.get("mode")
|
|
|
|
if mode:
|
|
|
|
if mode == "word":
|
|
|
|
scheme = AliasGeneratorEnum.word.value
|
|
|
|
elif mode == "uuid":
|
|
|
|
scheme = AliasGeneratorEnum.uuid.value
|
|
|
|
else:
|
|
|
|
return jsonify(error=f"{mode} must be either word or uuid"), 400
|
|
|
|
|
|
|
|
alias = Alias.create_new_random(user=user, scheme=scheme, note=note)
|
|
|
|
Session.commit()
|
2020-01-05 21:15:16 +01:00
|
|
|
|
2021-11-12 11:04:00 +01:00
|
|
|
if hostname and not AliasUsedOn.get_by(alias_id=alias.id, hostname=hostname):
|
2020-03-20 12:29:11 +01:00
|
|
|
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-05 21:15:16 +01:00
|
|
|
|
2020-03-26 19:50:22 +01:00
|
|
|
return (
|
2020-05-23 19:18:24 +02:00
|
|
|
jsonify(alias=alias.email, **serialize_alias_info_v2(get_alias_info_v2(alias))),
|
2020-03-26 19:50:22 +01:00
|
|
|
201,
|
|
|
|
)
|