2022-02-03 11:16:42 +01:00
|
|
|
from deprecated import deprecated
|
2020-02-04 17:26:59 +01:00
|
|
|
from flask import g
|
2020-03-15 22:32:48 +01:00
|
|
|
from flask import jsonify
|
|
|
|
from flask import request
|
2020-02-04 17:26:59 +01:00
|
|
|
|
2020-05-07 22:27:27 +02:00
|
|
|
from app import alias_utils
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2020-04-05 16:32:38 +02:00
|
|
|
from app.api.serializer import (
|
2020-03-17 20:16:20 +01:00
|
|
|
AliasInfo,
|
2020-04-05 16:32:38 +02:00
|
|
|
serialize_alias_info,
|
|
|
|
serialize_contact,
|
|
|
|
get_alias_infos_with_pagination,
|
|
|
|
get_alias_contacts,
|
2020-04-06 22:26:35 +02:00
|
|
|
serialize_alias_info_v2,
|
2020-05-23 19:18:24 +02:00
|
|
|
get_alias_info_v2,
|
2020-08-21 19:51:48 +02:00
|
|
|
get_alias_infos_with_pagination_v3,
|
2020-03-17 20:16:20 +01:00
|
|
|
)
|
2022-04-21 18:40:24 +02:00
|
|
|
from app.dashboard.views.alias_contact_manager import create_contact
|
2020-04-05 16:32:38 +02:00
|
|
|
from app.dashboard.views.alias_log import get_alias_log
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2022-04-21 18:40:24 +02:00
|
|
|
from app.errors import (
|
|
|
|
CannotCreateContactForReverseAlias,
|
|
|
|
ErrContactErrorUpgradeNeeded,
|
|
|
|
ErrContactAlreadyExists,
|
|
|
|
ErrAddressInvalid,
|
2021-01-11 12:27:02 +01:00
|
|
|
)
|
2023-11-21 14:42:24 +01:00
|
|
|
from app.extensions import limiter
|
2024-06-07 15:36:18 +02:00
|
|
|
from app.log import LOG
|
2020-05-03 15:56:45 +02:00
|
|
|
from app.models import Alias, Contact, Mailbox, AliasMailbox
|
2020-02-04 17:26:59 +01:00
|
|
|
|
|
|
|
|
2022-02-03 11:16:42 +01:00
|
|
|
@deprecated
|
2020-03-17 19:32:45 +01:00
|
|
|
@api_bp.route("/aliases", methods=["GET", "POST"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2023-12-19 17:27:55 +01:00
|
|
|
@limiter.limit("10/minute", key_func=lambda: g.user.id)
|
2020-02-04 17:26:59 +01:00
|
|
|
def get_aliases():
|
|
|
|
"""
|
|
|
|
Get aliases
|
|
|
|
Input:
|
|
|
|
page_id: in query
|
|
|
|
Output:
|
|
|
|
- aliases: list of alias:
|
|
|
|
- id
|
|
|
|
- email
|
|
|
|
- creation_date
|
|
|
|
- creation_timestamp
|
|
|
|
- nb_forward
|
|
|
|
- nb_block
|
|
|
|
- nb_reply
|
2020-03-11 12:13:38 +01:00
|
|
|
- note
|
2020-02-04 17:26:59 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
try:
|
|
|
|
page_id = int(request.args.get("page_id"))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return jsonify(error="page_id must be provided in request query"), 400
|
|
|
|
|
2020-03-17 19:32:45 +01:00
|
|
|
query = None
|
|
|
|
data = request.get_json(silent=True)
|
|
|
|
if data:
|
|
|
|
query = data.get("query")
|
|
|
|
|
2020-03-17 20:16:20 +01:00
|
|
|
alias_infos: [AliasInfo] = get_alias_infos_with_pagination(
|
|
|
|
user, page_id=page_id, query=query
|
|
|
|
)
|
2020-02-04 17:26:59 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
jsonify(
|
2020-03-26 19:35:44 +01:00
|
|
|
aliases=[serialize_alias_info(alias_info) for alias_info in alias_infos]
|
2020-02-04 17:26:59 +01:00
|
|
|
),
|
|
|
|
200,
|
|
|
|
)
|
2020-02-05 12:21:17 +01:00
|
|
|
|
|
|
|
|
2020-04-06 22:26:35 +02:00
|
|
|
@api_bp.route("/v2/aliases", methods=["GET", "POST"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2023-12-27 16:42:58 +01:00
|
|
|
@limiter.limit("50/minute", key_func=lambda: g.user.id)
|
2020-04-06 22:26:35 +02:00
|
|
|
def get_aliases_v2():
|
|
|
|
"""
|
|
|
|
Get aliases
|
|
|
|
Input:
|
|
|
|
page_id: in query
|
2022-02-03 11:16:42 +01:00
|
|
|
pinned: in query
|
2022-04-25 09:22:29 +02:00
|
|
|
disabled: in query
|
2022-04-28 17:24:35 +02:00
|
|
|
enabled: in query
|
2020-04-06 22:26:35 +02:00
|
|
|
Output:
|
|
|
|
- aliases: list of alias:
|
|
|
|
- id
|
|
|
|
- email
|
|
|
|
- creation_date
|
|
|
|
- creation_timestamp
|
|
|
|
- nb_forward
|
|
|
|
- nb_block
|
|
|
|
- nb_reply
|
|
|
|
- note
|
2020-05-03 15:56:45 +02:00
|
|
|
- mailbox
|
|
|
|
- mailboxes
|
2020-08-04 20:09:42 +02:00
|
|
|
- support_pgp
|
2020-08-06 14:22:28 +02:00
|
|
|
- disable_pgp
|
2020-11-01 12:32:20 +01:00
|
|
|
- latest_activity: null if no activity.
|
2020-04-06 22:26:35 +02:00
|
|
|
- timestamp
|
|
|
|
- action: forward|reply|block|bounced
|
|
|
|
- contact:
|
|
|
|
- email
|
|
|
|
- name
|
|
|
|
- reverse_alias
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
try:
|
|
|
|
page_id = int(request.args.get("page_id"))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return jsonify(error="page_id must be provided in request query"), 400
|
|
|
|
|
2022-02-03 11:16:42 +01:00
|
|
|
pinned = "pinned" in request.args
|
2022-04-25 09:22:29 +02:00
|
|
|
disabled = "disabled" in request.args
|
2022-04-28 17:24:35 +02:00
|
|
|
enabled = "enabled" in request.args
|
2022-04-25 09:22:29 +02:00
|
|
|
|
|
|
|
if pinned:
|
|
|
|
alias_filter = "pinned"
|
|
|
|
elif disabled:
|
|
|
|
alias_filter = "disabled"
|
2022-04-28 17:24:35 +02:00
|
|
|
elif enabled:
|
|
|
|
alias_filter = "enabled"
|
2022-04-25 09:22:29 +02:00
|
|
|
else:
|
|
|
|
alias_filter = None
|
2022-02-03 11:16:42 +01:00
|
|
|
|
2020-04-06 22:26:35 +02:00
|
|
|
query = None
|
|
|
|
data = request.get_json(silent=True)
|
|
|
|
if data:
|
|
|
|
query = data.get("query")
|
|
|
|
|
2020-08-21 19:51:48 +02:00
|
|
|
alias_infos: [AliasInfo] = get_alias_infos_with_pagination_v3(
|
2022-04-25 09:22:29 +02:00
|
|
|
user, page_id=page_id, query=query, alias_filter=alias_filter
|
2020-04-06 22:26:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
aliases=[serialize_alias_info_v2(alias_info) for alias_info in alias_infos]
|
|
|
|
),
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-05 12:21:17 +01:00
|
|
|
@api_bp.route("/aliases/<int:alias_id>", methods=["DELETE"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-02-05 12:21:17 +01:00
|
|
|
def delete_alias(alias_id):
|
|
|
|
"""
|
|
|
|
Delete alias
|
|
|
|
Input:
|
|
|
|
alias_id: in url
|
|
|
|
Output:
|
|
|
|
200 if deleted successfully
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.get(alias_id)
|
2020-02-05 12:21:17 +01:00
|
|
|
|
2020-06-28 09:47:56 +02:00
|
|
|
if not alias or alias.user_id != user.id:
|
2020-02-05 12:21:17 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2020-05-07 22:27:27 +02:00
|
|
|
alias_utils.delete_alias(alias, user)
|
2020-02-05 12:21:17 +01:00
|
|
|
|
|
|
|
return jsonify(deleted=True), 200
|
2020-02-05 12:28:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/aliases/<int:alias_id>/toggle", methods=["POST"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-02-05 12:28:54 +01:00
|
|
|
def toggle_alias(alias_id):
|
|
|
|
"""
|
|
|
|
Enable/disable alias
|
|
|
|
Input:
|
|
|
|
alias_id: in url
|
|
|
|
Output:
|
|
|
|
200 along with new status:
|
|
|
|
- enabled
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
2020-03-17 11:51:40 +01:00
|
|
|
alias: Alias = Alias.get(alias_id)
|
2020-02-05 12:28:54 +01:00
|
|
|
|
2020-09-05 20:54:08 +02:00
|
|
|
if not alias or alias.user_id != user.id:
|
2020-02-05 12:28:54 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2024-05-23 10:27:08 +02:00
|
|
|
alias_utils.change_alias_status(alias, enabled=not alias.enabled)
|
2024-06-07 15:36:18 +02:00
|
|
|
LOG.i(f"User {user} changed alias {alias} enabled status to {alias.enabled}")
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-02-05 12:28:54 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
return jsonify(enabled=alias.enabled), 200
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/aliases/<int:alias_id>/activities")
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-02-05 12:57:11 +01:00
|
|
|
def get_alias_activities(alias_id):
|
|
|
|
"""
|
|
|
|
Get aliases
|
|
|
|
Input:
|
|
|
|
page_id: in query
|
|
|
|
Output:
|
|
|
|
- activities: list of activity:
|
|
|
|
- from
|
|
|
|
- to
|
|
|
|
- timestamp
|
2020-04-06 22:26:35 +02:00
|
|
|
- action: forward|reply|block|bounced
|
2020-04-04 19:18:07 +02:00
|
|
|
- reverse_alias
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
try:
|
|
|
|
page_id = int(request.args.get("page_id"))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return jsonify(error="page_id must be provided in request query"), 400
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias: Alias = Alias.get(alias_id)
|
2020-02-05 12:57:11 +01:00
|
|
|
|
2020-06-07 12:50:30 +02:00
|
|
|
if not alias or alias.user_id != user.id:
|
2020-02-05 12:57:11 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias_logs = get_alias_log(alias, page_id)
|
2020-02-05 12:57:11 +01:00
|
|
|
|
|
|
|
activities = []
|
|
|
|
for alias_log in alias_logs:
|
2020-04-04 19:18:07 +02:00
|
|
|
activity = {
|
|
|
|
"timestamp": alias_log.when.timestamp,
|
|
|
|
"reverse_alias": alias_log.reverse_alias,
|
2020-11-17 09:27:30 +01:00
|
|
|
"reverse_alias_address": alias_log.contact.reply_email,
|
2020-04-04 19:18:07 +02:00
|
|
|
}
|
2020-02-05 12:57:11 +01:00
|
|
|
if alias_log.is_reply:
|
|
|
|
activity["from"] = alias_log.alias
|
2020-04-05 12:58:06 +02:00
|
|
|
activity["to"] = alias_log.website_email
|
2020-02-05 12:57:11 +01:00
|
|
|
activity["action"] = "reply"
|
|
|
|
else:
|
|
|
|
activity["to"] = alias_log.alias
|
2020-04-05 12:58:06 +02:00
|
|
|
activity["from"] = alias_log.website_email
|
2020-02-05 12:57:11 +01:00
|
|
|
|
2020-02-22 07:27:22 +01:00
|
|
|
if alias_log.bounced:
|
|
|
|
activity["action"] = "bounced"
|
|
|
|
elif alias_log.blocked:
|
2020-02-05 12:57:11 +01:00
|
|
|
activity["action"] = "block"
|
|
|
|
else:
|
|
|
|
activity["action"] = "forward"
|
|
|
|
|
|
|
|
activities.append(activity)
|
|
|
|
|
2020-03-14 12:22:43 +01:00
|
|
|
return jsonify(activities=activities), 200
|
2020-03-14 11:38:39 +01:00
|
|
|
|
|
|
|
|
2020-11-15 18:45:58 +01:00
|
|
|
@api_bp.route("/aliases/<int:alias_id>", methods=["PUT", "PATCH"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-03-14 11:38:39 +01:00
|
|
|
def update_alias(alias_id):
|
|
|
|
"""
|
|
|
|
Update alias note
|
|
|
|
Input:
|
|
|
|
alias_id: in url
|
2020-05-03 14:24:17 +02:00
|
|
|
note (optional): in body
|
|
|
|
name (optional): in body
|
|
|
|
mailbox_id (optional): in body
|
2020-05-16 12:55:21 +02:00
|
|
|
disable_pgp (optional): in body
|
2020-03-14 11:38:39 +01:00
|
|
|
Output:
|
|
|
|
200
|
|
|
|
"""
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
|
|
|
user = g.user
|
2020-03-17 11:51:40 +01:00
|
|
|
alias: Alias = Alias.get(alias_id)
|
2020-03-14 11:38:39 +01:00
|
|
|
|
2020-06-28 09:47:09 +02:00
|
|
|
if not alias or alias.user_id != user.id:
|
2020-03-14 11:38:39 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2020-04-25 13:10:00 +02:00
|
|
|
changed = False
|
|
|
|
if "note" in data:
|
|
|
|
new_note = data.get("note")
|
|
|
|
alias.note = new_note
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if "mailbox_id" in data:
|
|
|
|
mailbox_id = int(data.get("mailbox_id"))
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if not mailbox or mailbox.user_id != user.id or not mailbox.verified:
|
|
|
|
return jsonify(error="Forbidden"), 400
|
|
|
|
|
|
|
|
alias.mailbox_id = mailbox_id
|
|
|
|
changed = True
|
|
|
|
|
2020-05-03 16:02:46 +02:00
|
|
|
if "mailbox_ids" in data:
|
|
|
|
mailbox_ids = [int(m_id) for m_id in data.get("mailbox_ids")]
|
|
|
|
mailboxes: [Mailbox] = []
|
|
|
|
|
|
|
|
# check if all mailboxes belong to user
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if not mailbox or mailbox.user_id != user.id or not mailbox.verified:
|
|
|
|
return jsonify(error="Forbidden"), 400
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
return jsonify(error="Must choose at least one mailbox"), 400
|
|
|
|
|
|
|
|
# <<< update alias mailboxes >>>
|
|
|
|
# first remove all existing alias-mailboxes links
|
2021-10-12 14:36:47 +02:00
|
|
|
AliasMailbox.filter_by(alias_id=alias.id).delete()
|
|
|
|
Session.flush()
|
2020-05-03 16:02:46 +02:00
|
|
|
|
|
|
|
# then add all new mailboxes
|
|
|
|
for i, mailbox in enumerate(mailboxes):
|
|
|
|
if i == 0:
|
|
|
|
alias.mailbox_id = mailboxes[0].id
|
|
|
|
else:
|
2020-05-15 16:35:57 +02:00
|
|
|
AliasMailbox.create(alias_id=alias.id, mailbox_id=mailbox.id)
|
2020-05-03 16:02:46 +02:00
|
|
|
# <<< END update alias mailboxes >>>
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
|
2020-04-26 10:38:16 +02:00
|
|
|
if "name" in data:
|
2020-11-20 18:39:23 +01:00
|
|
|
# to make sure alias name doesn't contain linebreak
|
2020-11-22 13:50:57 +01:00
|
|
|
new_name = data.get("name")
|
2021-11-08 11:52:41 +01:00
|
|
|
if new_name and len(new_name) > 128:
|
2021-11-08 11:26:10 +01:00
|
|
|
return jsonify(error="Name can't be longer than 128 characters"), 400
|
|
|
|
|
2020-11-22 12:10:19 +01:00
|
|
|
if new_name:
|
|
|
|
new_name = new_name.replace("\n", "")
|
2020-04-26 10:38:16 +02:00
|
|
|
alias.name = new_name
|
|
|
|
changed = True
|
|
|
|
|
2020-05-16 12:55:21 +02:00
|
|
|
if "disable_pgp" in data:
|
|
|
|
alias.disable_pgp = data.get("disable_pgp")
|
|
|
|
changed = True
|
|
|
|
|
2020-11-15 18:45:58 +01:00
|
|
|
if "pinned" in data:
|
|
|
|
alias.pinned = data.get("pinned")
|
|
|
|
changed = True
|
|
|
|
|
2020-04-25 13:10:00 +02:00
|
|
|
if changed:
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-04-25 13:10:00 +02:00
|
|
|
|
|
|
|
return jsonify(ok=True), 200
|
2020-03-14 12:22:43 +01:00
|
|
|
|
|
|
|
|
2020-03-26 19:44:00 +01:00
|
|
|
@api_bp.route("/aliases/<int:alias_id>", methods=["GET"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-03-26 19:44:00 +01:00
|
|
|
def get_alias(alias_id):
|
|
|
|
"""
|
|
|
|
Get alias
|
|
|
|
Input:
|
|
|
|
alias_id: in url
|
|
|
|
Output:
|
|
|
|
Alias info, same as in get_aliases
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
alias: Alias = Alias.get(alias_id)
|
|
|
|
|
2020-06-19 23:44:16 +02:00
|
|
|
if not alias:
|
|
|
|
return jsonify(error="Unknown error"), 400
|
|
|
|
|
2020-03-26 19:44:00 +01:00
|
|
|
if alias.user_id != user.id:
|
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2020-05-23 19:18:24 +02:00
|
|
|
return jsonify(**serialize_alias_info_v2(get_alias_info_v2(alias))), 200
|
2020-03-26 19:44:00 +01:00
|
|
|
|
|
|
|
|
2020-03-14 12:22:43 +01:00
|
|
|
@api_bp.route("/aliases/<int:alias_id>/contacts")
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-03-14 12:22:43 +01:00
|
|
|
def get_alias_contacts_route(alias_id):
|
|
|
|
"""
|
|
|
|
Get alias contacts
|
|
|
|
Input:
|
|
|
|
page_id: in query
|
|
|
|
Output:
|
|
|
|
- contacts: list of contacts:
|
|
|
|
- creation_date
|
|
|
|
- creation_timestamp
|
|
|
|
- last_email_sent_date
|
|
|
|
- last_email_sent_timestamp
|
|
|
|
- contact
|
|
|
|
- reverse_alias
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
try:
|
|
|
|
page_id = int(request.args.get("page_id"))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return jsonify(error="page_id must be provided in request query"), 400
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias: Alias = Alias.get(alias_id)
|
2020-03-14 12:22:43 +01:00
|
|
|
|
2022-01-18 09:40:50 +01:00
|
|
|
if not alias:
|
|
|
|
return jsonify(error="No such alias"), 404
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
if alias.user_id != user.id:
|
2020-03-14 12:22:43 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
contacts = get_alias_contacts(alias, page_id)
|
2020-03-14 12:22:43 +01:00
|
|
|
|
|
|
|
return jsonify(contacts=contacts), 200
|
2020-03-14 12:55:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/aliases/<int:alias_id>/contacts", methods=["POST"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-03-14 12:55:38 +01:00
|
|
|
def create_contact_route(alias_id):
|
|
|
|
"""
|
|
|
|
Create contact for an alias
|
|
|
|
Input:
|
|
|
|
alias_id: in url
|
|
|
|
contact: in body
|
|
|
|
Output:
|
|
|
|
201 if success
|
|
|
|
409 if contact already added
|
|
|
|
"""
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias: Alias = Alias.get(alias_id)
|
2020-03-14 12:55:38 +01:00
|
|
|
|
2022-04-21 18:40:24 +02:00
|
|
|
if alias.user_id != g.user.id:
|
2020-03-14 12:55:38 +01:00
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
2022-04-21 18:40:24 +02:00
|
|
|
contact_address = data.get("contact")
|
2020-03-14 12:55:38 +01:00
|
|
|
|
2022-01-07 10:47:36 +01:00
|
|
|
try:
|
2022-04-21 18:40:24 +02:00
|
|
|
contact = create_contact(g.user, alias, contact_address)
|
|
|
|
except ErrContactErrorUpgradeNeeded as err:
|
|
|
|
return jsonify(error=err.error_for_user()), 403
|
|
|
|
except (ErrAddressInvalid, CannotCreateContactForReverseAlias) as err:
|
|
|
|
return jsonify(error=err.error_for_user()), 400
|
|
|
|
except ErrContactAlreadyExists as err:
|
|
|
|
return jsonify(**serialize_contact(err.contact, existed=True)), 200
|
2020-03-14 12:55:38 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
return jsonify(**serialize_contact(contact)), 201
|
2020-03-17 19:18:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/contacts/<int:contact_id>", methods=["DELETE"])
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-03-17 19:18:26 +01:00
|
|
|
def delete_contact(contact_id):
|
|
|
|
"""
|
|
|
|
Delete contact
|
|
|
|
Input:
|
|
|
|
contact_id: in url
|
|
|
|
Output:
|
|
|
|
200
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
contact = Contact.get(contact_id)
|
|
|
|
|
|
|
|
if not contact or contact.alias.user_id != user.id:
|
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
|
|
|
Contact.delete(contact_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-03-17 19:18:26 +01:00
|
|
|
|
|
|
|
return jsonify(deleted=True), 200
|
2021-10-28 10:14:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/contacts/<int:contact_id>/toggle", methods=["POST"])
|
|
|
|
@require_api_auth
|
|
|
|
def toggle_contact(contact_id):
|
|
|
|
"""
|
|
|
|
Block/Unblock contact
|
|
|
|
Input:
|
|
|
|
contact_id: in url
|
|
|
|
Output:
|
|
|
|
200
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
contact = Contact.get(contact_id)
|
|
|
|
|
|
|
|
if not contact or contact.alias.user_id != user.id:
|
|
|
|
return jsonify(error="Forbidden"), 403
|
|
|
|
|
|
|
|
contact.block_forward = not contact.block_forward
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
return jsonify(block_forward=contact.block_forward), 200
|