mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 08:26:11 +00:00
wekan: Strengthen types using WildValue.
This commit is contained in:
committed by
Tim Abbott
parent
958b58f174
commit
537632d58c
@@ -1,10 +1,9 @@
|
|||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
|
|
||||||
from zerver.decorator import webhook_view
|
from zerver.decorator import webhook_view
|
||||||
from zerver.lib.request import REQ, has_request_variables
|
from zerver.lib.request import REQ, has_request_variables
|
||||||
from zerver.lib.response import json_success
|
from zerver.lib.response import json_success
|
||||||
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
||||||
from zerver.lib.webhooks.common import check_send_webhook_message
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
||||||
from zerver.models import UserProfile
|
from zerver.models import UserProfile
|
||||||
|
|
||||||
@@ -26,15 +25,15 @@ def clean_payload_text(text: str) -> str:
|
|||||||
return text.replace(url, "").replace("\n", "")
|
return text.replace(url, "").replace("\n", "")
|
||||||
|
|
||||||
|
|
||||||
def get_message_body(payload: Dict[str, Any], action: str) -> str:
|
def get_message_body(payload: WildValue) -> str:
|
||||||
footer = get_hyperlinked_url(payload["text"])
|
footer = get_hyperlinked_url(payload["text"].tame(check_string))
|
||||||
body = process_message_data(payload, action)
|
body = process_message_data(payload)
|
||||||
return MESSAGE_TEMPLATE.format(body=body, footer=footer)
|
return MESSAGE_TEMPLATE.format(body=body, footer=footer)
|
||||||
|
|
||||||
|
|
||||||
def process_message_data(payload: Dict[str, Any], action: str) -> str:
|
def process_message_data(payload: WildValue) -> str:
|
||||||
payload["text"] = clean_payload_text(payload["text"])
|
text = clean_payload_text(payload["text"].tame(check_string))
|
||||||
return "{text}.".format(**payload)
|
return f"{text}."
|
||||||
|
|
||||||
|
|
||||||
@webhook_view("Wekan")
|
@webhook_view("Wekan")
|
||||||
@@ -42,9 +41,9 @@ def process_message_data(payload: Dict[str, Any], action: str) -> str:
|
|||||||
def api_wekan_webhook(
|
def api_wekan_webhook(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
user_profile: UserProfile,
|
user_profile: UserProfile,
|
||||||
payload: Dict[str, Any] = REQ(argument_type="body"),
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
||||||
) -> HttpResponse:
|
) -> HttpResponse:
|
||||||
topic = "Wekan Notification"
|
topic = "Wekan Notification"
|
||||||
body = get_message_body(payload, payload["description"])
|
body = get_message_body(payload)
|
||||||
check_send_webhook_message(request, user_profile, topic, body)
|
check_send_webhook_message(request, user_profile, topic, body)
|
||||||
return json_success(request)
|
return json_success(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user