2016-11-29 22:48:22 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-10-31 04:25:48 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_error, json_success
|
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()
|
|
|
|
USER_REGISTER_TEMPLATE = """
|
|
|
|
New blog user registered:
|
|
|
|
* **Name**: {name}
|
|
|
|
* **Email**: {email}
|
|
|
|
""".strip()
|
2021-02-12 08:20:45 +01:00
|
|
|
WP_LOGIN_TEMPLATE = "User {name} logged in."
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
@webhook_view("WordPress", notify_bot_owner_on_invalid_json=False)
|
2016-11-29 22:48:22 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_wordpress_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
hook: str = REQ(default="WordPress Action"),
|
|
|
|
post_title: str = REQ(default="New WordPress Post"),
|
|
|
|
post_type: str = REQ(default="post"),
|
|
|
|
post_url: str = REQ(default="WordPress Post URL"),
|
|
|
|
display_name: str = REQ(default="New User Name"),
|
|
|
|
user_email: str = REQ(default="New User Email"),
|
|
|
|
user_login: str = REQ(default="Logged in User"),
|
|
|
|
) -> HttpResponse:
|
2016-11-29 22:48:22 +01:00
|
|
|
# remove trailing whitespace (issue for some test fixtures)
|
|
|
|
hook = hook.rstrip()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if hook == "publish_post" or hook == "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)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
elif hook == "user_register":
|
2016-11-29 22:48:22 +01:00
|
|
|
data = USER_REGISTER_TEMPLATE.format(name=display_name, email=user_email)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
elif hook == "wp_login":
|
2016-11-29 22:48:22 +01:00
|
|
|
data = WP_LOGIN_TEMPLATE.format(name=user_login)
|
|
|
|
|
|
|
|
else:
|
2020-06-15 23:22:24 +02:00
|
|
|
return json_error(_("Unknown WordPress webhook action: {}").format(hook))
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "WordPress Notification"
|
2018-03-16 22:53:50 +01:00
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, data)
|
2016-11-29 22:48:22 +01:00
|
|
|
return json_success()
|