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
|
|
|
|
from flask import render_template, request, flash, url_for, redirect
|
|
|
|
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
|
|
|
|
from app.log import LOG
|
2022-02-09 12:00:48 +01:00
|
|
|
from app.models import Mailbox
|
|
|
|
from app.config import ZENDESK_HOST
|
|
|
|
|
|
|
|
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 12:00:48 +01:00
|
|
|
mailbox = Mailbox.get(current_user.default_mailbox_id)
|
|
|
|
return render_template("dashboard/support.html", ticketEmail=mailbox.email)
|
|
|
|
|
|
|
|
|
|
|
|
def upload_file_to_zendesk(file: FileStorage) -> Union[None, str]:
|
|
|
|
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")
|
|
|
|
return None
|
|
|
|
escaped_filename = urllib.parse.urlencode({'filename': file.filename})
|
|
|
|
url = 'https://{}/api/v2/uploads?{}'.format(ZENDESK_HOST, escaped_filename)
|
|
|
|
headers = {'content-type': file.mimetype}
|
|
|
|
response = requests.post(url, headers=headers, data=file.stream)
|
|
|
|
if response.status_code != 201:
|
|
|
|
if response.status_code == 401 or 422:
|
|
|
|
LOG.debug('Could not authenticate')
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
LOG.debug('Problem with the request. Status ' + str(response.status_code))
|
|
|
|
return None
|
|
|
|
data = response.json()
|
|
|
|
return data['upload']['token']
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
|
2022-02-09 12:00:48 +01:00
|
|
|
def create_zendesk_request(email: str, contents: str, files: [FileStorage]) -> bool:
|
|
|
|
tokens = []
|
|
|
|
for file in files:
|
|
|
|
token = upload_file_to_zendesk(file)
|
|
|
|
if token is None:
|
|
|
|
return False
|
|
|
|
tokens.append(token)
|
2022-02-08 22:04:25 +01:00
|
|
|
data = {
|
|
|
|
'request': {
|
|
|
|
'subject': 'Ticket created for user {}'.format(current_user.id),
|
|
|
|
'comment': {
|
|
|
|
'type': 'Comment',
|
2022-02-09 12:00:48 +01:00
|
|
|
'body': contents,
|
|
|
|
'uploads': tokens
|
2022-02-08 22:04:25 +01:00
|
|
|
},
|
|
|
|
'requester': {
|
|
|
|
'name': "SimpleLogin user {}".format(current_user.id),
|
|
|
|
'email': email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 12:00:48 +01:00
|
|
|
url = 'https://{}/api/v2/requests.json'.format(ZENDESK_HOST)
|
2022-02-08 22:04:25 +01:00
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
r = requests.post(url, data=json.dumps(data), headers=headers)
|
|
|
|
if r.status_code != 201:
|
|
|
|
if r.status_code == 401 or 422:
|
|
|
|
LOG.debug('Could not authenticate')
|
2022-02-09 12:00:48 +01:00
|
|
|
return False
|
2022-02-08 22:04:25 +01:00
|
|
|
else:
|
|
|
|
LOG.debug('Problem with the request. Status ' + str(r.status_code))
|
2022-02-09 12:00:48 +01:00
|
|
|
return False
|
|
|
|
flash("Ticket was created. You should receive an email notification", "success")
|
|
|
|
LOG.debug('Ticket created')
|
|
|
|
return True
|
2022-02-08 22:04:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/support", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
def process_support_dialog():
|
|
|
|
contents = request.form.get("ticketContents") or ""
|
|
|
|
email = request.form.get("ticketEmail") or ""
|
|
|
|
if not contents:
|
|
|
|
flash("Please add a description", "warning")
|
|
|
|
return render_template("dashboard/support.html", ticketEmail=email)
|
|
|
|
if not email:
|
|
|
|
flash("Please add an email", "warning")
|
|
|
|
return render_template("dashboard/support.html", ticketContents=contents)
|
2022-02-09 12:00:48 +01:00
|
|
|
if create_zendesk_request(email, contents, request.files.getlist('ticketFiles')):
|
|
|
|
return render_template("dashboard/support_ticket_created.html", ticketEmail=email)
|
|
|
|
return render_template("dashboard/support.html", ticketEmail=email, ticketContents=contents)
|