2016-11-29 22:48:22 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-10-31 04:25:48 +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-08-12 09:35:14 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2019-04-17 02:09:09 +02:00
|
|
|
PUBLISH_POST_OR_PAGE_TEMPLATE = """
|
|
|
|
New {type} published:
|
|
|
|
* [{title}]({url})
|
|
|
|
""".strip()
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"publish_post",
|
|
|
|
"publish_page",
|
|
|
|
]
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("WordPress", notify_bot_owner_on_invalid_json=False, all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:35:14 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_wordpress_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:35:14 +02:00
|
|
|
*,
|
|
|
|
hook: str = "WordPress action",
|
|
|
|
post_title: str = "New WordPress post",
|
|
|
|
post_type: str = "post",
|
|
|
|
post_url: str = "WordPress post URL",
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2016-11-29 22:48:22 +01:00
|
|
|
# remove trailing whitespace (issue for some test fixtures)
|
|
|
|
hook = hook.rstrip()
|
|
|
|
|
2023-07-22 01:15:10 +02:00
|
|
|
if hook in ("publish_post", "publish_page"):
|
2016-11-29 22:48:22 +01:00
|
|
|
data = PUBLISH_POST_OR_PAGE_TEMPLATE.format(type=post_type, title=post_title, url=post_url)
|
|
|
|
else:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("Unknown WordPress webhook action: {hook}").format(hook=hook))
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = "WordPress notification"
|
2018-03-16 22:53:50 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, data, hook)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|