2019-12-04 00:48:30 +01:00
|
|
|
from flask import jsonify, request, g
|
|
|
|
from flask_cors import cross_origin
|
2019-12-05 00:23:16 +01:00
|
|
|
from sqlalchemy import desc
|
2019-12-04 00:48:30 +01:00
|
|
|
|
|
|
|
from app.api.base import api_bp, verify_api_key
|
2020-01-22 14:21:01 +01:00
|
|
|
from app.config import ALIAS_DOMAINS, DISABLE_ALIAS_SUFFIX
|
2019-12-04 00:48:30 +01:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-03-17 11:51:40 +01:00
|
|
|
from app.models import AliasUsedOn, Alias, User
|
2019-12-09 22:36:04 +01:00
|
|
|
from app.utils import convert_to_id, random_word
|
2019-12-04 00:48:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/alias/options")
|
|
|
|
@cross_origin()
|
|
|
|
@verify_api_key
|
|
|
|
def options():
|
|
|
|
"""
|
|
|
|
Return what options user has when creating new alias.
|
|
|
|
Input:
|
|
|
|
a valid api-key in "Authentication" header and
|
|
|
|
optional "hostname" in args
|
|
|
|
Output: cf README
|
|
|
|
optional recommendation:
|
|
|
|
optional custom
|
|
|
|
can_create_custom: boolean
|
|
|
|
existing: array of existing aliases
|
|
|
|
|
|
|
|
"""
|
2020-03-16 21:10:53 +01:00
|
|
|
LOG.warning("/v2/alias/options should be used instead")
|
2019-12-04 00:48:30 +01:00
|
|
|
user = g.user
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
ret = {
|
2020-03-17 11:51:40 +01:00
|
|
|
"existing": [ge.email for ge in Alias.query.filter_by(user_id=user.id)],
|
2019-12-22 17:27:55 +01:00
|
|
|
"can_create_custom": user.can_create_new_alias(),
|
2019-12-04 00:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# recommendation alias if exist
|
|
|
|
if hostname:
|
2019-12-05 00:23:16 +01:00
|
|
|
# put the latest used alias first
|
|
|
|
q = (
|
2020-03-17 11:51:40 +01:00
|
|
|
db.session.query(AliasUsedOn, Alias, User)
|
2019-12-05 00:23:16 +01:00
|
|
|
.filter(
|
2020-03-17 12:01:18 +01:00
|
|
|
AliasUsedOn.alias_id == Alias.id,
|
2020-03-17 11:51:40 +01:00
|
|
|
Alias.user_id == user.id,
|
2019-12-05 00:23:16 +01:00
|
|
|
AliasUsedOn.hostname == hostname,
|
|
|
|
)
|
|
|
|
.order_by(desc(AliasUsedOn.created_at))
|
2019-12-04 00:48:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
r = q.first()
|
|
|
|
if r:
|
|
|
|
_, alias, _ = r
|
|
|
|
LOG.d("found alias %s %s %s", alias, hostname, user)
|
|
|
|
ret["recommendation"] = {"alias": alias.email, "hostname": hostname}
|
|
|
|
|
|
|
|
# custom alias suggestion and suffix
|
2019-12-08 16:22:42 +01:00
|
|
|
ret["custom"] = {}
|
|
|
|
if hostname:
|
|
|
|
# keep only the domain name of hostname, ignore TLD and subdomain
|
|
|
|
# for ex www.groupon.com -> groupon
|
|
|
|
domain_name = hostname
|
|
|
|
if "." in hostname:
|
|
|
|
parts = hostname.split(".")
|
|
|
|
domain_name = parts[-2]
|
|
|
|
domain_name = convert_to_id(domain_name)
|
|
|
|
ret["custom"]["suggestion"] = domain_name
|
|
|
|
else:
|
|
|
|
ret["custom"]["suggestion"] = ""
|
2019-12-04 00:48:30 +01:00
|
|
|
|
2020-01-22 10:22:59 +01:00
|
|
|
ret["custom"]["suffixes"] = []
|
2019-12-08 16:22:42 +01:00
|
|
|
# maybe better to make sure the suffix is never used before
|
|
|
|
# but this is ok as there's a check when creating a new custom alias
|
2020-01-22 10:22:59 +01:00
|
|
|
for domain in ALIAS_DOMAINS:
|
2020-01-22 14:21:01 +01:00
|
|
|
if DISABLE_ALIAS_SUFFIX:
|
|
|
|
ret["custom"]["suffixes"].append(f"@{domain}")
|
|
|
|
else:
|
|
|
|
ret["custom"]["suffixes"].append(f".{random_word()}@{domain}")
|
2019-12-04 00:48:30 +01:00
|
|
|
|
2019-12-08 16:22:42 +01:00
|
|
|
for custom_domain in user.verified_custom_domains():
|
|
|
|
ret["custom"]["suffixes"].append("@" + custom_domain.domain)
|
2019-12-04 00:48:30 +01:00
|
|
|
|
2019-12-08 16:22:42 +01:00
|
|
|
# custom domain should be put first
|
|
|
|
ret["custom"]["suffixes"] = list(reversed(ret["custom"]["suffixes"]))
|
2019-12-04 00:48:30 +01:00
|
|
|
|
|
|
|
return jsonify(ret)
|
2020-01-05 20:47:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/v2/alias/options")
|
|
|
|
@cross_origin()
|
|
|
|
@verify_api_key
|
|
|
|
def options_v2():
|
|
|
|
"""
|
|
|
|
Return what options user has when creating new alias.
|
|
|
|
Input:
|
|
|
|
a valid api-key in "Authentication" header and
|
|
|
|
optional "hostname" in args
|
|
|
|
Output: cf README
|
|
|
|
can_create: bool
|
|
|
|
suffixes: [str]
|
|
|
|
prefix_suggestion: str
|
|
|
|
existing: [str]
|
|
|
|
recommendation: Optional dict
|
|
|
|
alias: str
|
|
|
|
hostname: str
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
ret = {
|
|
|
|
"existing": [
|
2020-03-17 11:51:40 +01:00
|
|
|
ge.email for ge in Alias.query.filter_by(user_id=user.id, enabled=True)
|
2020-01-05 20:47:09 +01:00
|
|
|
],
|
|
|
|
"can_create": user.can_create_new_alias(),
|
|
|
|
"suffixes": [],
|
|
|
|
"prefix_suggestion": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
# recommendation alias if exist
|
|
|
|
if hostname:
|
|
|
|
# put the latest used alias first
|
|
|
|
q = (
|
2020-03-17 11:51:40 +01:00
|
|
|
db.session.query(AliasUsedOn, Alias, User)
|
2020-01-05 20:47:09 +01:00
|
|
|
.filter(
|
2020-03-17 12:01:18 +01:00
|
|
|
AliasUsedOn.alias_id == Alias.id,
|
2020-03-17 11:51:40 +01:00
|
|
|
Alias.user_id == user.id,
|
2020-01-05 20:47:09 +01:00
|
|
|
AliasUsedOn.hostname == hostname,
|
|
|
|
)
|
|
|
|
.order_by(desc(AliasUsedOn.created_at))
|
|
|
|
)
|
|
|
|
|
|
|
|
r = q.first()
|
|
|
|
if r:
|
|
|
|
_, alias, _ = r
|
|
|
|
LOG.d("found alias %s %s %s", alias, hostname, user)
|
|
|
|
ret["recommendation"] = {"alias": alias.email, "hostname": hostname}
|
|
|
|
|
|
|
|
# custom alias suggestion and suffix
|
|
|
|
if hostname:
|
|
|
|
# keep only the domain name of hostname, ignore TLD and subdomain
|
|
|
|
# for ex www.groupon.com -> groupon
|
|
|
|
domain_name = hostname
|
|
|
|
if "." in hostname:
|
|
|
|
parts = hostname.split(".")
|
|
|
|
domain_name = parts[-2]
|
|
|
|
domain_name = convert_to_id(domain_name)
|
|
|
|
ret["prefix_suggestion"] = domain_name
|
|
|
|
|
|
|
|
# maybe better to make sure the suffix is never used before
|
|
|
|
# but this is ok as there's a check when creating a new custom alias
|
2020-01-22 10:22:59 +01:00
|
|
|
for domain in ALIAS_DOMAINS:
|
2020-01-22 14:21:01 +01:00
|
|
|
if DISABLE_ALIAS_SUFFIX:
|
|
|
|
ret["suffixes"].append(f"@{domain}")
|
|
|
|
else:
|
|
|
|
ret["suffixes"].append(f".{random_word()}@{domain}")
|
2020-01-05 20:47:09 +01:00
|
|
|
|
|
|
|
for custom_domain in user.verified_custom_domains():
|
|
|
|
ret["suffixes"].append("@" + custom_domain.domain)
|
|
|
|
|
|
|
|
# custom domain should be put first
|
|
|
|
ret["suffixes"] = list(reversed(ret["suffixes"]))
|
|
|
|
|
|
|
|
return jsonify(ret)
|