2016-05-05 07:06:41 +02:00
|
|
|
# Webhooks for external integrations.
|
2024-07-12 02:30:17 +02:00
|
|
|
from typing import Optional
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-05-05 07:06:41 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import return_success_on_head_request, webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2020-01-14 22:06:24 +01:00
|
|
|
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
|
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
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
from .board_actions import SUPPORTED_BOARD_ACTIONS, process_board_action
|
2020-06-11 00:54:34 +02:00
|
|
|
from .card_actions import IGNORED_CARD_ACTIONS, SUPPORTED_CARD_ACTIONS, process_card_action
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Trello")
|
2016-11-15 17:20:22 +01:00
|
|
|
@return_success_on_head_request
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_trello_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-06-27 18:26:40 +02:00
|
|
|
action_type = payload["action"]["type"].tame(check_string)
|
2023-07-12 13:37:08 +02:00
|
|
|
message = get_topic_and_body(payload, action_type)
|
2020-08-31 22:10:21 +02:00
|
|
|
if message is None:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-08-31 22:10:21 +02:00
|
|
|
else:
|
2023-07-12 13:37:08 +02:00
|
|
|
topic, body = message
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2016-05-05 07:06:41 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_topic_and_body(payload: WildValue, action_type: str) -> Optional[tuple[str, str]]:
|
2016-05-05 07:06:41 +02:00
|
|
|
if action_type in SUPPORTED_CARD_ACTIONS:
|
|
|
|
return process_card_action(payload, action_type)
|
2020-08-31 22:10:21 +02:00
|
|
|
if action_type in IGNORED_CARD_ACTIONS:
|
|
|
|
return None
|
2016-05-05 07:06:41 +02:00
|
|
|
if action_type in SUPPORTED_BOARD_ACTIONS:
|
|
|
|
return process_board_action(payload, action_type)
|
2018-05-22 16:46:45 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(action_type)
|