2021-10-18 11:45:48 +02:00
|
|
|
import tldextract
|
2019-12-04 00:48:30 +01:00
|
|
|
from flask import jsonify, request, g
|
2019-12-05 00:23:16 +01:00
|
|
|
from sqlalchemy import desc
|
2019-12-04 00:48:30 +01:00
|
|
|
|
2022-07-27 17:40:22 +02:00
|
|
|
from app.alias_suffix import get_alias_suffixes
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2019-12-04 00:48:30 +01:00
|
|
|
from app.log import LOG
|
2020-03-17 11:51:40 +01:00
|
|
|
from app.models import AliasUsedOn, Alias, User
|
2021-06-05 17:41:18 +02:00
|
|
|
from app.utils import convert_to_id
|
2020-05-02 16:21:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/v4/alias/options")
|
|
|
|
@require_api_auth
|
|
|
|
def options_v4():
|
|
|
|
"""
|
|
|
|
Return what options user has when creating new alias.
|
|
|
|
Same as v3 but return time-based signed-suffix in addition to suffix. To be used with /v2/alias/custom/new
|
|
|
|
Input:
|
|
|
|
a valid api-key in "Authentication" header and
|
|
|
|
optional "hostname" in args
|
|
|
|
Output: cf README
|
|
|
|
can_create: bool
|
|
|
|
suffixes: [[suffix, signed_suffix]]
|
|
|
|
prefix_suggestion: str
|
|
|
|
recommendation: Optional dict
|
|
|
|
alias: str
|
|
|
|
hostname: str
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
ret = {
|
|
|
|
"can_create": user.can_create_new_alias(),
|
|
|
|
"suffixes": [],
|
|
|
|
"prefix_suggestion": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
# recommendation alias if exist
|
|
|
|
if hostname:
|
|
|
|
# put the latest used alias first
|
|
|
|
q = (
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(AliasUsedOn, Alias, User)
|
2020-05-02 16:21:18 +02:00
|
|
|
.filter(
|
|
|
|
AliasUsedOn.alias_id == Alias.id,
|
|
|
|
Alias.user_id == user.id,
|
|
|
|
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
|
2021-10-18 11:45:48 +02:00
|
|
|
ext = tldextract.extract(hostname)
|
|
|
|
prefix_suggestion = ext.domain
|
|
|
|
prefix_suggestion = convert_to_id(prefix_suggestion)
|
|
|
|
ret["prefix_suggestion"] = prefix_suggestion
|
2020-05-02 16:21:18 +02:00
|
|
|
|
2022-07-27 17:40:22 +02:00
|
|
|
suffixes = get_alias_suffixes(user)
|
2020-05-02 16:21:18 +02:00
|
|
|
|
|
|
|
# custom domain should be put first
|
2020-12-31 14:25:44 +01:00
|
|
|
ret["suffixes"] = list([suffix.suffix, suffix.signed_suffix] for suffix in suffixes)
|
2020-05-02 16:21:18 +02:00
|
|
|
|
|
|
|
return jsonify(ret)
|
2020-11-14 16:45:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/v5/alias/options")
|
|
|
|
@require_api_auth
|
|
|
|
def options_v5():
|
|
|
|
"""
|
|
|
|
Return what options user has when creating new alias.
|
|
|
|
Same as v4 but uses a better format. To be used with /v2/alias/custom/new
|
|
|
|
Input:
|
|
|
|
a valid api-key in "Authentication" header and
|
|
|
|
optional "hostname" in args
|
|
|
|
Output: cf README
|
|
|
|
can_create: bool
|
|
|
|
suffixes: [
|
|
|
|
{
|
|
|
|
suffix: "suffix",
|
2022-04-14 17:28:40 +02:00
|
|
|
signed_suffix: "signed_suffix",
|
|
|
|
is_custom: true,
|
|
|
|
is_premium: false
|
2020-11-14 16:45:22 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
prefix_suggestion: str
|
|
|
|
recommendation: Optional dict
|
|
|
|
alias: str
|
|
|
|
hostname: str
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
ret = {
|
|
|
|
"can_create": user.can_create_new_alias(),
|
|
|
|
"suffixes": [],
|
|
|
|
"prefix_suggestion": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
# recommendation alias if exist
|
|
|
|
if hostname:
|
|
|
|
# put the latest used alias first
|
|
|
|
q = (
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(AliasUsedOn, Alias, User)
|
2020-11-14 16:45:22 +01:00
|
|
|
.filter(
|
|
|
|
AliasUsedOn.alias_id == Alias.id,
|
|
|
|
Alias.user_id == user.id,
|
|
|
|
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
|
2021-10-18 11:45:48 +02:00
|
|
|
ext = tldextract.extract(hostname)
|
|
|
|
prefix_suggestion = ext.domain
|
|
|
|
prefix_suggestion = convert_to_id(prefix_suggestion)
|
|
|
|
ret["prefix_suggestion"] = prefix_suggestion
|
2020-11-14 16:45:22 +01:00
|
|
|
|
2022-07-27 17:40:22 +02:00
|
|
|
suffixes = get_alias_suffixes(user)
|
2020-11-14 16:45:22 +01:00
|
|
|
|
|
|
|
# custom domain should be put first
|
|
|
|
ret["suffixes"] = [
|
2022-04-14 17:28:40 +02:00
|
|
|
{
|
|
|
|
"suffix": suffix.suffix,
|
|
|
|
"signed_suffix": suffix.signed_suffix,
|
|
|
|
"is_custom": suffix.is_custom,
|
|
|
|
"is_premium": suffix.is_premium,
|
|
|
|
}
|
2020-12-31 14:25:44 +01:00
|
|
|
for suffix in suffixes
|
2020-11-14 16:45:22 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
return jsonify(ret)
|