2017-11-16 00:43:10 +01:00
|
|
|
import re
|
2019-04-10 06:46:29 +02:00
|
|
|
import string
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-03-14 20:04:11 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2021-12-17 07:03:22 +01:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-03-14 20:04:11 +01:00
|
|
|
|
|
|
|
from .support_event import SUPPORT_EVENTS
|
|
|
|
|
|
|
|
DOCUMENT_TEMPLATE = "{user_name} {verb} the document [{title}]({url})"
|
|
|
|
QUESTION_TEMPLATE = "{user_name} {verb} the question [{title}]({url})"
|
2021-02-12 08:19:30 +01:00
|
|
|
QUESTIONS_ANSWER_TEMPLATE = (
|
2023-01-03 02:16:53 +01:00
|
|
|
"{user_name} {verb} the [answer]({answer_url})"
|
|
|
|
" of the question [{question_title}]({question_url})"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
COMMENT_TEMPLATE = (
|
|
|
|
"{user_name} {verb} the [comment]({answer_url}) of the task [{task_title}]({task_url})"
|
|
|
|
)
|
2017-03-14 20:04:11 +01:00
|
|
|
MESSAGE_TEMPLATE = "{user_name} {verb} the message [{title}]({url})"
|
|
|
|
TODO_LIST_TEMPLATE = "{user_name} {verb} the todo list [{title}]({url})"
|
|
|
|
TODO_TEMPLATE = "{user_name} {verb} the todo task [{title}]({url})"
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"document",
|
|
|
|
"question_answer",
|
|
|
|
"question",
|
|
|
|
"message",
|
|
|
|
"todolist",
|
|
|
|
"todo",
|
|
|
|
"comment",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("Basecamp", all_event_types=ALL_EVENT_TYPES)
|
2017-03-14 20:04:11 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_basecamp_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-12-17 07:03:22 +01:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-03-14 20:04:11 +01:00
|
|
|
event = get_event_type(payload)
|
|
|
|
|
|
|
|
if event not in SUPPORT_EVENTS:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = get_project_name(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
if event.startswith("document_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_document_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "document"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("question_answer_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_questions_answer_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "question_answer"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("question_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_questions_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "question"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("message_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_message_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "message"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("todolist_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_todo_list_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "todolist"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("todo_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_todo_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "todo"
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event.startswith("comment_"):
|
2017-03-14 20:04:11 +01:00
|
|
|
body = get_comment_body(event, payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "comment"
|
2017-03-14 20:04:11 +01:00
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_project_name(payload: WildValue) -> str:
|
|
|
|
return payload["recording"]["bucket"]["name"].tame(check_string)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_event_type(payload: WildValue) -> str:
|
|
|
|
return payload["kind"].tame(check_string)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_event_creator(payload: WildValue) -> str:
|
|
|
|
return payload["creator"]["name"].tame(check_string)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_url(payload: WildValue) -> str:
|
2021-12-17 07:03:22 +01:00
|
|
|
return payload["recording"]["app_url"].tame(check_string)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_title(payload: WildValue) -> str:
|
2021-12-17 07:03:22 +01:00
|
|
|
return payload["recording"]["title"].tame(check_string)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_verb(event: str, prefix: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
verb = event.replace(prefix, "")
|
|
|
|
if verb == "active":
|
|
|
|
return "activated"
|
2017-03-14 20:04:11 +01:00
|
|
|
|
|
|
|
matched = re.match(r"(?P<subject>[A-z]*)_changed", verb)
|
|
|
|
if matched:
|
2021-02-12 08:20:45 +01:00
|
|
|
return "changed {} of".format(matched.group("subject"))
|
2017-03-14 20:04:11 +01:00
|
|
|
return verb
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-04-10 06:46:29 +02:00
|
|
|
def add_punctuation_if_necessary(body: str, title: str) -> str:
|
|
|
|
if title[-1] not in string.punctuation:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = f"{body}."
|
2019-04-10 06:46:29 +02:00
|
|
|
return body
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_document_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_generic_body(event, payload, "document_", DOCUMENT_TEMPLATE)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_questions_answer_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
verb = get_verb(event, "question_answer_")
|
|
|
|
question = payload["recording"]["parent"]
|
2021-12-17 07:03:22 +01:00
|
|
|
title = question["title"].tame(check_string)
|
2019-04-10 06:46:29 +02:00
|
|
|
template = add_punctuation_if_necessary(QUESTIONS_ANSWER_TEMPLATE, title)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2019-04-10 06:46:29 +02:00
|
|
|
return template.format(
|
2017-03-14 20:04:11 +01:00
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
2023-07-12 13:37:08 +02:00
|
|
|
answer_url=get_topic_url(payload),
|
2019-04-10 06:46:29 +02:00
|
|
|
question_title=title,
|
2021-12-17 07:03:22 +01:00
|
|
|
question_url=question["app_url"].tame(check_string),
|
2017-03-14 20:04:11 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_comment_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
verb = get_verb(event, "comment_")
|
|
|
|
task = payload["recording"]["parent"]
|
2021-12-17 07:03:22 +01:00
|
|
|
template = add_punctuation_if_necessary(COMMENT_TEMPLATE, task["title"].tame(check_string))
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2019-04-10 06:46:29 +02:00
|
|
|
return template.format(
|
2017-03-14 20:04:11 +01:00
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
2023-07-12 13:37:08 +02:00
|
|
|
answer_url=get_topic_url(payload),
|
2021-12-17 07:03:22 +01:00
|
|
|
task_title=task["title"].tame(check_string),
|
|
|
|
task_url=task["app_url"].tame(check_string),
|
2017-03-14 20:04:11 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_questions_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_generic_body(event, payload, "question_", QUESTION_TEMPLATE)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_message_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_generic_body(event, payload, "message_", MESSAGE_TEMPLATE)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_todo_list_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_generic_body(event, payload, "todolist_", TODO_LIST_TEMPLATE)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_todo_body(event: str, payload: WildValue) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_generic_body(event, payload, "todo_", TODO_TEMPLATE)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-17 07:03:22 +01:00
|
|
|
def get_generic_body(event: str, payload: WildValue, prefix: str, template: str) -> str:
|
2017-03-14 20:04:11 +01:00
|
|
|
verb = get_verb(event, prefix)
|
2023-07-12 13:37:08 +02:00
|
|
|
title = get_topic_title(payload)
|
2019-04-10 06:46:29 +02:00
|
|
|
template = add_punctuation_if_necessary(template, title)
|
2017-03-14 20:04:11 +01:00
|
|
|
|
|
|
|
return template.format(
|
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
2023-07-12 13:37:08 +02:00
|
|
|
title=get_topic_title(payload),
|
|
|
|
url=get_topic_url(payload),
|
2017-03-14 20:04:11 +01:00
|
|
|
)
|