2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Callable
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string
|
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
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_message_data(payload: WildValue) -> tuple[str, str, str, str]:
|
2022-04-02 11:33:41 +02:00
|
|
|
link = "https://app.frontapp.com/open/" + payload["target"]["data"]["id"].tame(check_string)
|
|
|
|
outbox = payload["conversation"]["recipient"]["handle"].tame(check_string)
|
|
|
|
inbox = payload["source"]["data"][0]["address"].tame(check_string)
|
|
|
|
subject = payload["conversation"]["subject"].tame(check_string)
|
2018-02-08 19:46:15 +01:00
|
|
|
return link, outbox, inbox, subject
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_source_name(payload: WildValue) -> str:
|
2023-05-11 17:38:16 +02:00
|
|
|
type = payload["source"]["_meta"]["type"].tame(check_string)
|
|
|
|
if type == "teammate":
|
|
|
|
first_name = payload["source"]["data"]["first_name"].tame(check_string)
|
|
|
|
last_name = payload["source"]["data"]["last_name"].tame(check_string)
|
|
|
|
return f"{first_name} {last_name}"
|
|
|
|
elif type == "rule":
|
|
|
|
name = payload["source"]["data"]["name"].tame(check_string)
|
|
|
|
return f"'{name}' rule"
|
|
|
|
return f"({type})"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_target_name(payload: WildValue) -> str:
|
|
|
|
first_name = payload["target"]["data"]["first_name"].tame(check_string)
|
|
|
|
last_name = payload["target"]["data"]["last_name"].tame(check_string)
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"{first_name} {last_name}"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_inbound_message_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
2023-08-03 00:28:59 +02:00
|
|
|
f"[Inbound message]({link}) from **{outbox}** to **{inbox}**:\n"
|
|
|
|
f"```quote\n*Subject*: {subject}\n```"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_outbound_message_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
2023-08-03 00:28:59 +02:00
|
|
|
f"[Outbound message]({link}) from **{inbox}** to **{outbox}**:\n"
|
|
|
|
f"```quote\n*Subject*: {subject}\n```"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_outbound_reply_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
link, outbox, inbox, subject = get_message_data(payload)
|
2023-07-19 23:06:38 +02:00
|
|
|
return f"[Outbound reply]({link}) from **{inbox}** to **{outbox}**."
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_comment_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2022-04-02 11:33:41 +02:00
|
|
|
comment = payload["target"]["data"]["body"].tame(check_string)
|
2021-08-02 23:36:06 +02:00
|
|
|
return f"**{name}** left a comment:\n```quote\n{comment}\n```"
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_assigned_body(payload: WildValue) -> 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:
|
2021-08-02 23:36:06 +02:00
|
|
|
return f"**{source_name}** assigned themselves."
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-19 23:06:38 +02:00
|
|
|
return f"**{source_name}** assigned **{target_name}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_unassigned_body(payload: WildValue) -> 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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_archived_body(payload: WildValue) -> 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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_reopened_body(payload: WildValue) -> 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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_deleted_body(payload: WildValue) -> 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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_restored_body(payload: WildValue) -> 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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_tagged_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2022-04-02 11:33:41 +02:00
|
|
|
tag = payload["target"]["data"]["name"].tame(check_string)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"**{name}** added tag **{tag}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 11:33:41 +02:00
|
|
|
def get_conversation_untagged_body(payload: WildValue) -> str:
|
2018-03-13 20:14:58 +01:00
|
|
|
name = get_source_name(payload)
|
2022-04-02 11:33:41 +02:00
|
|
|
tag = payload["target"]["data"]["name"].tame(check_string)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"**{name}** removed tag **{tag}**."
|
2018-03-13 20:14:58 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-13 20:14:58 +01:00
|
|
|
EVENT_FUNCTION_MAPPER = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"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,
|
|
|
|
"untag": get_conversation_untagged_body,
|
2018-03-13 20:14:58 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-11 08:25:55 +02:00
|
|
|
def get_body_based_on_event(event: str) -> Callable[[WildValue], str]:
|
2018-03-13 20:14:58 +01:00
|
|
|
return EVENT_FUNCTION_MAPPER[event]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("Front", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_front_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2022-04-02 11:33:41 +02:00
|
|
|
event = payload["type"].tame(check_string)
|
2018-03-13 20:14:58 +01:00
|
|
|
if event not in EVENT_FUNCTION_MAPPER:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Unknown webhook request"))
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = payload["conversation"]["id"].tame(check_string)
|
2018-03-13 20:14:58 +01:00
|
|
|
body = get_body_based_on_event(event)(payload)
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, event)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|