2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Dict, Tuple
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_error, json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-02-08 19:46:15 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_message_data(payload: Dict[str, Any]) -> Tuple[str, str, str, str]:
|
2018-03-12 18:46:42 +01:00
|
|
|
link = "https://app.frontapp.com/open/" + payload['target']['data']['id']
|
|
|
|
outbox = payload['conversation']['recipient']['handle']
|
|
|
|
inbox = payload['source']['data'][0]['address']
|
|
|
|
subject = payload['conversation']['subject']
|
2018-02-08 19:46:15 +01:00
|
|
|
return link, outbox, inbox, subject
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_source_name(payload: Dict[str, Any]) -> str:
|
2018-03-12 18:46:42 +01:00
|
|
|
first_name = payload['source']['data']['first_name']
|
|
|
|
last_name = payload['source']['data']['last_name']
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"{first_name} {last_name}"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_target_name(payload: Dict[str, Any]) -> str:
|
2018-03-12 18:46:42 +01:00
|
|
|
first_name = payload['target']['data']['first_name']
|
|
|
|
last_name = payload['target']['data']['last_name']
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"{first_name} {last_name}"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_inbound_message_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2019-05-09 02:23:38 +02:00
|
|
|
return "[Inbound message]({link}) from **{outbox}** to **{inbox}**:\n" \
|
2018-03-13 20:14:58 +01:00
|
|
|
"```quote\n*Subject*: {subject}\n```" \
|
|
|
|
.format(link=link, outbox=outbox, inbox=inbox, subject=subject)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_outbound_message_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2019-05-09 02:23:38 +02:00
|
|
|
return "[Outbound message]({link}) from **{inbox}** to **{outbox}**:\n" \
|
2018-03-13 20:14:58 +01:00
|
|
|
"```quote\n*Subject*: {subject}\n```" \
|
|
|
|
.format(link=link, inbox=inbox, outbox=outbox, subject=subject)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_outbound_reply_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
|
|
|
return "[Outbound reply]({link}) from **{inbox}** to **{outbox}**." \
|
|
|
|
.format(link=link, inbox=inbox, outbox=outbox)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_comment_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
comment = payload['target']['data']['body']
|
|
|
|
return "**{name}** left a comment:\n```quote\n{comment}\n```" \
|
|
|
|
.format(name=name, comment=comment)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_assigned_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
source_name = get_source_name(payload)
|
|
|
|
target_name = get_target_name(payload)
|
|
|
|
|
|
|
|
if source_name == target_name:
|
|
|
|
return "**{source_name}** assigned themselves." \
|
|
|
|
.format(source_name=source_name)
|
|
|
|
|
|
|
|
return "**{source_name}** assigned **{target_name}**." \
|
|
|
|
.format(source_name=source_name, target_name=target_name)
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_unassigned_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"Unassigned by **{name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_archived_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"Archived by **{name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_reopened_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"Reopened by **{name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_deleted_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"Deleted by **{name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_restored_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"Restored by **{name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_tagged_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
tag = payload['target']['data']['name']
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"**{name}** added tag **{tag}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_conversation_untagged_body(payload: Dict[str, Any]) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
|
|
|
tag = payload['target']['data']['name']
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"**{name}** removed tag **{tag}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
|
|
|
EVENT_FUNCTION_MAPPER = {
|
|
|
|
'inbound': get_inbound_message_body,
|
|
|
|
'outbound': get_outbound_message_body,
|
|
|
|
'out_reply': get_outbound_reply_body,
|
|
|
|
'comment': get_comment_body,
|
|
|
|
'mention': get_comment_body,
|
|
|
|
'assign': get_conversation_assigned_body,
|
|
|
|
'unassign': get_conversation_unassigned_body,
|
|
|
|
'archive': get_conversation_archived_body,
|
|
|
|
'reopen': get_conversation_reopened_body,
|
|
|
|
'trash': get_conversation_deleted_body,
|
|
|
|
'restore': get_conversation_restored_body,
|
|
|
|
'tag': get_conversation_tagged_body,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
'untag': get_conversation_untagged_body,
|
2018-03-13 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_body_based_on_event(event: str) -> Any:
|
2018-03-13 20:14:58 +01:00
|
|
|
return EVENT_FUNCTION_MAPPER[event]
|
|
|
|
|
2018-02-08 19:46:15 +01:00
|
|
|
@api_key_only_webhook_view('Front')
|
|
|
|
@has_request_variables
|
|
|
|
def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-05-10 19:34:01 +02:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2018-03-12 18:46:42 +01:00
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
event = payload['type']
|
|
|
|
if event not in EVENT_FUNCTION_MAPPER:
|
2018-02-08 19:46:15 +01:00
|
|
|
return json_error(_("Unknown webhook request"))
|
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
topic = payload['conversation']['id']
|
|
|
|
body = get_body_based_on_event(event)(payload)
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
return json_success()
|