mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 04:53:36 +00:00
python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
5028c081cb
commit
11741543da
@@ -24,10 +24,12 @@ class FrontHookTests(WebhookTestCase):
|
||||
|
||||
def test_outbound_message(self) -> None:
|
||||
expected_topic = 'cnv_keo696'
|
||||
expected_message = "[Outbound message](https://app.frontapp.com/open/msg_1176ie2) " \
|
||||
"from **support@planet-express.com** " \
|
||||
"to **calculon@momsbot.com**:\n" \
|
||||
"```quote\n*Subject*: Your next delivery is on Epsilon 96Z\n```"
|
||||
expected_message = (
|
||||
"[Outbound message](https://app.frontapp.com/open/msg_1176ie2) "
|
||||
"from **support@planet-express.com** "
|
||||
"to **calculon@momsbot.com**:\n"
|
||||
"```quote\n*Subject*: Your next delivery is on Epsilon 96Z\n```"
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"outbound_message",
|
||||
@@ -93,8 +95,10 @@ class FrontHookTests(WebhookTestCase):
|
||||
|
||||
def test_mention_all(self) -> None:
|
||||
expected_topic = 'cnv_keo696'
|
||||
expected_message = "**Leela Turanga** left a comment:\n" \
|
||||
"```quote\n@all Could someone else take this?\n```"
|
||||
expected_message = (
|
||||
"**Leela Turanga** left a comment:\n"
|
||||
"```quote\n@all Could someone else take this?\n```"
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"mention_all",
|
||||
@@ -107,10 +111,12 @@ class FrontHookTests(WebhookTestCase):
|
||||
|
||||
def test_inbound_message(self) -> None:
|
||||
expected_topic = 'cnv_keocka'
|
||||
expected_message = "[Inbound message](https://app.frontapp.com/open/msg_1176r8y) " \
|
||||
"from **calculon@momsbot.com** " \
|
||||
"to **support@planet-express.com**:\n" \
|
||||
"```quote\n*Subject*: Being a robot is great, but...\n```"
|
||||
expected_message = (
|
||||
"[Inbound message](https://app.frontapp.com/open/msg_1176r8y) "
|
||||
"from **calculon@momsbot.com** "
|
||||
"to **support@planet-express.com**:\n"
|
||||
"```quote\n*Subject*: Being a robot is great, but...\n```"
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"inbound_message",
|
||||
@@ -144,9 +150,11 @@ class FrontHookTests(WebhookTestCase):
|
||||
|
||||
def test_outbound_reply(self) -> None:
|
||||
expected_topic = 'cnv_keocka'
|
||||
expected_message = "[Outbound reply](https://app.frontapp.com/open/msg_1176ryy) " \
|
||||
"from **support@planet-express.com** " \
|
||||
"to **calculon@momsbot.com**."
|
||||
expected_message = (
|
||||
"[Outbound reply](https://app.frontapp.com/open/msg_1176ryy) "
|
||||
"from **support@planet-express.com** "
|
||||
"to **calculon@momsbot.com**."
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"outbound_reply",
|
||||
@@ -168,8 +176,10 @@ class FrontHookTests(WebhookTestCase):
|
||||
|
||||
def test_mention(self) -> None:
|
||||
expected_topic = 'cnv_keocka'
|
||||
expected_message = "**Leela Turanga** left a comment:\n" \
|
||||
"```quote\n@bender Could you take it from here?\n```"
|
||||
expected_message = (
|
||||
"**Leela Turanga** left a comment:\n"
|
||||
"```quote\n@bender Could you take it from here?\n```"
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"mention",
|
||||
@@ -205,7 +215,8 @@ class FrontHookTests(WebhookTestCase):
|
||||
payload = self.get_body('conversation_assigned')
|
||||
payload_json = orjson.loads(payload)
|
||||
payload_json['type'] = 'qwerty'
|
||||
result = self.client_post(self.url, orjson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
result = self.client_post(
|
||||
self.url, orjson.dumps(payload_json), content_type="application/x-www-form-urlencoded"
|
||||
)
|
||||
|
||||
self.assert_json_error(result, "Unknown webhook request")
|
||||
|
||||
@@ -17,80 +17,101 @@ def get_message_data(payload: Dict[str, Any]) -> Tuple[str, str, str, str]:
|
||||
subject = payload['conversation']['subject']
|
||||
return link, outbox, inbox, subject
|
||||
|
||||
|
||||
def get_source_name(payload: Dict[str, Any]) -> str:
|
||||
first_name = payload['source']['data']['first_name']
|
||||
last_name = payload['source']['data']['last_name']
|
||||
return f"{first_name} {last_name}"
|
||||
|
||||
|
||||
def get_target_name(payload: Dict[str, Any]) -> str:
|
||||
first_name = payload['target']['data']['first_name']
|
||||
last_name = payload['target']['data']['last_name']
|
||||
return f"{first_name} {last_name}"
|
||||
|
||||
|
||||
def get_inbound_message_body(payload: Dict[str, Any]) -> str:
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
return "[Inbound message]({link}) from **{outbox}** to **{inbox}**:\n" \
|
||||
"```quote\n*Subject*: {subject}\n```" \
|
||||
.format(link=link, outbox=outbox, inbox=inbox, subject=subject)
|
||||
return (
|
||||
"[Inbound message]({link}) from **{outbox}** to **{inbox}**:\n"
|
||||
"```quote\n*Subject*: {subject}\n```".format(
|
||||
link=link, outbox=outbox, inbox=inbox, subject=subject
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_outbound_message_body(payload: Dict[str, Any]) -> str:
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
return "[Outbound message]({link}) from **{inbox}** to **{outbox}**:\n" \
|
||||
"```quote\n*Subject*: {subject}\n```" \
|
||||
.format(link=link, inbox=inbox, outbox=outbox, subject=subject)
|
||||
return (
|
||||
"[Outbound message]({link}) from **{inbox}** to **{outbox}**:\n"
|
||||
"```quote\n*Subject*: {subject}\n```".format(
|
||||
link=link, inbox=inbox, outbox=outbox, subject=subject
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_outbound_reply_body(payload: Dict[str, Any]) -> str:
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
return "[Outbound reply]({link}) from **{inbox}** to **{outbox}**." \
|
||||
.format(link=link, inbox=inbox, outbox=outbox)
|
||||
return "[Outbound reply]({link}) from **{inbox}** to **{outbox}**.".format(
|
||||
link=link, inbox=inbox, outbox=outbox
|
||||
)
|
||||
|
||||
|
||||
def get_comment_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
comment = payload['target']['data']['body']
|
||||
return "**{name}** left a comment:\n```quote\n{comment}\n```" \
|
||||
.format(name=name, comment=comment)
|
||||
return "**{name}** left a comment:\n```quote\n{comment}\n```".format(name=name, comment=comment)
|
||||
|
||||
|
||||
def get_conversation_assigned_body(payload: Dict[str, Any]) -> str:
|
||||
source_name = get_source_name(payload)
|
||||
target_name = get_target_name(payload)
|
||||
|
||||
if source_name == target_name:
|
||||
return "**{source_name}** assigned themselves." \
|
||||
.format(source_name=source_name)
|
||||
return "**{source_name}** assigned themselves.".format(source_name=source_name)
|
||||
|
||||
return "**{source_name}** assigned **{target_name}**.".format(
|
||||
source_name=source_name, target_name=target_name
|
||||
)
|
||||
|
||||
return "**{source_name}** assigned **{target_name}**." \
|
||||
.format(source_name=source_name, target_name=target_name)
|
||||
|
||||
def get_conversation_unassigned_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
return f"Unassigned by **{name}**."
|
||||
|
||||
|
||||
def get_conversation_archived_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
return f"Archived by **{name}**."
|
||||
|
||||
|
||||
def get_conversation_reopened_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
return f"Reopened by **{name}**."
|
||||
|
||||
|
||||
def get_conversation_deleted_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
return f"Deleted by **{name}**."
|
||||
|
||||
|
||||
def get_conversation_restored_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
return f"Restored by **{name}**."
|
||||
|
||||
|
||||
def get_conversation_tagged_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
tag = payload['target']['data']['name']
|
||||
return f"**{name}** added tag **{tag}**."
|
||||
|
||||
|
||||
def get_conversation_untagged_body(payload: Dict[str, Any]) -> str:
|
||||
name = get_source_name(payload)
|
||||
tag = payload['target']['data']['name']
|
||||
return f"**{name}** removed tag **{tag}**."
|
||||
|
||||
|
||||
EVENT_FUNCTION_MAPPER = {
|
||||
'inbound': get_inbound_message_body,
|
||||
'outbound': get_outbound_message_body,
|
||||
@@ -107,13 +128,18 @@ EVENT_FUNCTION_MAPPER = {
|
||||
'untag': get_conversation_untagged_body,
|
||||
}
|
||||
|
||||
|
||||
def get_body_based_on_event(event: str) -> Any:
|
||||
return EVENT_FUNCTION_MAPPER[event]
|
||||
|
||||
|
||||
@webhook_view('Front')
|
||||
@has_request_variables
|
||||
def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
||||
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
||||
def api_front_webhook(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
payload: Dict[str, Any] = REQ(argument_type='body'),
|
||||
) -> HttpResponse:
|
||||
|
||||
event = payload['type']
|
||||
if event not in EVENT_FUNCTION_MAPPER:
|
||||
|
||||
Reference in New Issue
Block a user