2022-02-08 22:04:25 +01:00
|
|
|
import json
|
2022-02-09 12:00:48 +01:00
|
|
|
import urllib.parse
|
|
|
|
from typing import Union
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
import requests
|
2022-02-10 12:34:46 +01:00
|
|
|
from flask import render_template, request, flash, url_for, redirect, g
|
2022-02-08 22:04:25 +01:00
|
|
|
from flask_login import login_required, current_user
|
2022-02-09 12:00:48 +01:00
|
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
|
2022-02-08 22:04:25 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2022-02-10 12:34:46 +01:00
|
|
|
from app.extensions import limiter
|
2022-02-08 22:04:25 +01:00
|
|
|
from app.log import LOG
|
2022-02-09 12:00:48 +01:00
|
|
|
from app.models import Mailbox
|
|
|
|
from app.config import ZENDESK_HOST
|
|
|
|
|
2022-02-10 10:30:28 +01:00
|
|
|
VALID_MIME_TYPES = ["text/plain", "message/rfc822"]
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/support", methods=["GET"])
|
|
|
|
@login_required
|
|
|
|
def show_support_dialog():
|
2022-02-09 16:20:55 +01:00
|
|
|
if not ZENDESK_HOST:
|
2022-02-10 11:04:36 +01:00
|
|
|
flash("Support form is not enabled", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
2022-02-09 16:41:04 +01:00
|
|
|
return render_template("dashboard/support.html", ticket_email=current_user.email)
|
2022-02-09 16:20:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_zendesk_response_status(response_code: int) -> bool:
|
|
|
|
if response_code != 201:
|
|
|
|
if response_code in (401 or 422):
|
2022-02-10 12:47:31 +01:00
|
|
|
LOG.error("Could not authenticate")
|
2022-02-09 16:20:55 +01:00
|
|
|
else:
|
2022-02-10 12:47:31 +01:00
|
|
|
LOG.error("Problem with the request. Status {}".format(response_code))
|
2022-02-09 16:20:55 +01:00
|
|
|
return False
|
|
|
|
return True
|
2022-02-09 12:00:48 +01:00
|
|
|
|
|
|
|
|
2022-02-09 16:41:04 +01:00
|
|
|
def upload_file_to_zendesk_and_get_upload_token(file: FileStorage) -> Union[None, str]:
|
2022-02-10 10:30:28 +01:00
|
|
|
if file.mimetype not in VALID_MIME_TYPES and not file.mimetype.startswith("image/"):
|
|
|
|
flash(
|
|
|
|
"File {} is not an image, text or an email".format(file.filename), "warning"
|
|
|
|
)
|
2022-02-09 16:20:55 +01:00
|
|
|
return
|
2022-02-10 10:30:28 +01:00
|
|
|
escaped_filename = urllib.parse.urlencode({"filename": file.filename})
|
|
|
|
url = "https://{}/api/v2/uploads?{}".format(ZENDESK_HOST, escaped_filename)
|
|
|
|
headers = {"content-type": file.mimetype}
|
2022-02-09 12:00:48 +01:00
|
|
|
response = requests.post(url, headers=headers, data=file.stream)
|
2022-02-09 16:20:55 +01:00
|
|
|
if not check_zendesk_response_status(response.status_code):
|
|
|
|
return
|
2022-02-09 12:00:48 +01:00
|
|
|
data = response.json()
|
2022-02-10 10:30:28 +01:00
|
|
|
return data["upload"]["token"]
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
|
2022-02-09 16:41:04 +01:00
|
|
|
def create_zendesk_request(email: str, content: str, files: [FileStorage]) -> bool:
|
2022-02-09 12:00:48 +01:00
|
|
|
tokens = []
|
|
|
|
for file in files:
|
2022-02-09 16:41:04 +01:00
|
|
|
if not file.filename:
|
|
|
|
continue
|
|
|
|
token = upload_file_to_zendesk_and_get_upload_token(file)
|
2022-02-09 12:00:48 +01:00
|
|
|
if token is None:
|
|
|
|
return False
|
|
|
|
tokens.append(token)
|
2022-02-08 22:04:25 +01:00
|
|
|
data = {
|
2022-02-10 10:30:28 +01:00
|
|
|
"request": {
|
|
|
|
"subject": "Ticket created for user {}".format(current_user.id),
|
|
|
|
"comment": {"type": "Comment", "body": content, "uploads": tokens},
|
|
|
|
"requester": {
|
|
|
|
"name": "SimpleLogin user {}".format(current_user.id),
|
|
|
|
"email": email,
|
2022-02-08 22:04:25 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 10:30:28 +01:00
|
|
|
url = "https://{}/api/v2/requests.json".format(ZENDESK_HOST)
|
|
|
|
headers = {"content-type": "application/json"}
|
2022-02-09 16:20:55 +01:00
|
|
|
response = requests.post(url, data=json.dumps(data), headers=headers)
|
|
|
|
if not check_zendesk_response_status(response.status_code):
|
|
|
|
return False
|
2022-02-10 10:30:28 +01:00
|
|
|
LOG.debug("Ticket created")
|
2022-02-09 12:00:48 +01:00
|
|
|
return True
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/support", methods=["POST"])
|
|
|
|
@login_required
|
2022-02-10 12:34:46 +01:00
|
|
|
@limiter.limit(
|
|
|
|
"2/hour", deduct_when=lambda r: hasattr(g, "deduct_limit") and g.deduct_limit
|
|
|
|
)
|
2022-02-08 22:04:25 +01:00
|
|
|
def process_support_dialog():
|
2022-02-09 16:20:55 +01:00
|
|
|
if not ZENDESK_HOST:
|
|
|
|
return render_template("dashboard/support_disabled.html")
|
2022-02-09 16:41:04 +01:00
|
|
|
content = request.form.get("ticket_content") or ""
|
|
|
|
email = request.form.get("ticket_email") or ""
|
|
|
|
if not content:
|
2022-02-08 22:04:25 +01:00
|
|
|
flash("Please add a description", "warning")
|
2022-02-09 16:41:04 +01:00
|
|
|
return render_template("dashboard/support.html", ticket_email=email)
|
2022-02-08 22:04:25 +01:00
|
|
|
if not email:
|
|
|
|
flash("Please add an email", "warning")
|
2022-02-09 16:41:04 +01:00
|
|
|
return render_template("dashboard/support.html", ticket_content=content)
|
2022-02-10 12:34:46 +01:00
|
|
|
if not create_zendesk_request(
|
|
|
|
email, content, request.files.getlist("ticket_files")
|
|
|
|
):
|
2022-02-10 10:30:28 +01:00
|
|
|
return render_template(
|
2022-02-10 12:34:46 +01:00
|
|
|
"dashboard/support.html", ticket_email=email, ticket_content=content
|
2022-02-10 10:30:28 +01:00
|
|
|
)
|
2022-02-10 12:34:46 +01:00
|
|
|
g.deduct_limit = True
|
2022-02-10 12:38:56 +01:00
|
|
|
flash("Ticket created. You should have received an email notification.", "success")
|
|
|
|
return redirect(url_for("dashboard.index"))
|