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,14 +24,18 @@ New company **Kandra Labs** created:
|
||||
* **Monthly spending**: 0
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"company_created", expected_topic, expected_message,
|
||||
"company_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_contact_added_email(self) -> None:
|
||||
expected_topic = "Contact: Azure Bus from St. John's"
|
||||
expected_message = "New email jerryguitarist@gmail.com added to contact."
|
||||
self.check_webhook(
|
||||
"contact_added_email", expected_topic, expected_message,
|
||||
"contact_added_email",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_contact_created(self) -> None:
|
||||
@@ -43,7 +47,9 @@ New contact created:
|
||||
* **Location**: St. John's, Newfoundland and Labrador, Canada
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"contact_created", expected_topic, expected_message,
|
||||
"contact_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_contact_signed_up(self) -> None:
|
||||
@@ -54,56 +60,72 @@ Contact signed up:
|
||||
* **Location**: St. John's, Newfoundland and Labrador, Canada
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"contact_signed_up", expected_topic, expected_message,
|
||||
"contact_signed_up",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_contact_tag_created(self) -> None:
|
||||
expected_topic = "Contact: Eeshan Garg"
|
||||
expected_message = "Contact tagged with the `developer` tag."
|
||||
self.check_webhook(
|
||||
"contact_tag_created", expected_topic, expected_message,
|
||||
"contact_tag_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_contact_tag_deleted(self) -> None:
|
||||
expected_topic = "Contact: Eeshan Garg"
|
||||
expected_message = "The tag `developer` was removed from the contact."
|
||||
self.check_webhook(
|
||||
"contact_tag_deleted", expected_topic, expected_message,
|
||||
"contact_tag_deleted",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_assigned(self) -> None:
|
||||
expected_topic = "Lead: Eeshan Garg"
|
||||
expected_message = "Tim Abbott assigned to conversation."
|
||||
self.check_webhook(
|
||||
"conversation_admin_assigned", expected_topic, expected_message,
|
||||
"conversation_admin_assigned",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_opened(self) -> None:
|
||||
expected_topic = "Lead: Cordelia Lear"
|
||||
expected_message = "Eeshan Garg opened the conversation."
|
||||
self.check_webhook(
|
||||
"conversation_admin_opened", expected_topic, expected_message,
|
||||
"conversation_admin_opened",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_closed(self) -> None:
|
||||
expected_topic = "Lead: Eeshan Garg"
|
||||
expected_message = "Cordelia Lear closed the conversation."
|
||||
self.check_webhook(
|
||||
"conversation_admin_closed", expected_topic, expected_message,
|
||||
"conversation_admin_closed",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_snoozed(self) -> None:
|
||||
expected_topic = "Lead: Eeshan Garg"
|
||||
expected_message = "Cordelia Lear snoozed the conversation."
|
||||
self.check_webhook(
|
||||
"conversation_admin_snoozed", expected_topic, expected_message,
|
||||
"conversation_admin_snoozed",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_unsnoozed(self) -> None:
|
||||
expected_topic = "Lead: Eeshan Garg"
|
||||
expected_message = "Cordelia Lear unsnoozed the conversation."
|
||||
self.check_webhook(
|
||||
"conversation_admin_unsnoozed", expected_topic, expected_message,
|
||||
"conversation_admin_unsnoozed",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_replied(self) -> None:
|
||||
@@ -116,7 +138,9 @@ Hey Eeshan! How can I help?
|
||||
```
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"conversation_admin_replied", expected_topic, expected_message,
|
||||
"conversation_admin_replied",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_noted(self) -> None:
|
||||
@@ -129,7 +153,9 @@ Talk to Tim about this user's query.
|
||||
```
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"conversation_admin_noted", expected_topic, expected_message,
|
||||
"conversation_admin_noted",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_admin_single_created(self) -> None:
|
||||
@@ -142,7 +168,9 @@ Hi Eeshan, What's up
|
||||
```
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"conversation_admin_single_created", expected_topic, expected_message,
|
||||
"conversation_admin_single_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_user_created(self) -> None:
|
||||
@@ -155,7 +183,9 @@ Hello everyone!
|
||||
```
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"conversation_user_created", expected_topic, expected_message,
|
||||
"conversation_user_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_conversation_user_replied(self) -> None:
|
||||
@@ -168,14 +198,18 @@ Well, I need some help getting access to a developer account.
|
||||
```
|
||||
""".strip()
|
||||
self.check_webhook(
|
||||
"conversation_user_replied", expected_topic, expected_message,
|
||||
"conversation_user_replied",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_event_created(self) -> None:
|
||||
expected_topic = "Events"
|
||||
expected_message = "New event **invited-friend** created."
|
||||
self.check_webhook(
|
||||
"event_created", expected_topic, expected_message,
|
||||
"event_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_user_created(self) -> None:
|
||||
@@ -187,12 +221,16 @@ New user created:
|
||||
""".strip()
|
||||
|
||||
self.check_webhook(
|
||||
"user_created", expected_topic, expected_message,
|
||||
"user_created",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_user_deleted(self) -> None:
|
||||
self.check_webhook(
|
||||
"user_deleted", "User: jerryguitarist@gmail.com", "User deleted.",
|
||||
"user_deleted",
|
||||
"User: jerryguitarist@gmail.com",
|
||||
"User deleted.",
|
||||
)
|
||||
|
||||
def test_user_email_updated(self) -> None:
|
||||
@@ -204,18 +242,26 @@ New user created:
|
||||
|
||||
def test_user_tag_created(self) -> None:
|
||||
self.check_webhook(
|
||||
"user_tag_created", "User: eeshangarg", "The tag `developer` was added to the user.",
|
||||
"user_tag_created",
|
||||
"User: eeshangarg",
|
||||
"The tag `developer` was added to the user.",
|
||||
)
|
||||
|
||||
def test_user_tag_deleted(self) -> None:
|
||||
expected_topic = 'User: eeshangarg'
|
||||
expected_message = 'The tag `CSV Import - 2019-03-26 22:46:04 UTC` was removed from the user.'
|
||||
expected_message = (
|
||||
'The tag `CSV Import - 2019-03-26 22:46:04 UTC` was removed from the user.'
|
||||
)
|
||||
|
||||
self.check_webhook(
|
||||
"user_tag_deleted", expected_topic, expected_message,
|
||||
"user_tag_deleted",
|
||||
expected_topic,
|
||||
expected_message,
|
||||
)
|
||||
|
||||
def test_user_unsubscribed(self) -> None:
|
||||
self.check_webhook(
|
||||
"user_unsubscribed", "Contact: Eeshan Garg", "User unsubscribed from emails.",
|
||||
"user_unsubscribed",
|
||||
"Contact: Eeshan Garg",
|
||||
"User unsubscribed from emails.",
|
||||
)
|
||||
|
||||
@@ -64,6 +64,7 @@ New user created:
|
||||
* **Email**: {email}
|
||||
""".strip()
|
||||
|
||||
|
||||
class MLStripper(HTMLParser):
|
||||
def __init__(self) -> None:
|
||||
self.reset()
|
||||
@@ -77,11 +78,13 @@ class MLStripper(HTMLParser):
|
||||
def get_data(self) -> str:
|
||||
return ''.join(self.fed)
|
||||
|
||||
|
||||
def strip_tags(html: str) -> str:
|
||||
s = MLStripper()
|
||||
s.feed(html)
|
||||
return s.get_data()
|
||||
|
||||
|
||||
def get_topic_for_contacts(user: Dict[str, Any]) -> str:
|
||||
topic = "{type}: {name}".format(
|
||||
type=user['type'].capitalize(),
|
||||
@@ -90,16 +93,19 @@ def get_topic_for_contacts(user: Dict[str, Any]) -> str:
|
||||
|
||||
return topic
|
||||
|
||||
|
||||
def get_company_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
body = COMPANY_CREATED.format(**payload['data']['item'])
|
||||
return ('Companies', body)
|
||||
|
||||
|
||||
def get_contact_added_email_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']
|
||||
body = CONTACT_EMAIL_ADDED.format(email=user['email'])
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_contact_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
contact = payload['data']['item']
|
||||
body = CONTACT_CREATED.format(
|
||||
@@ -112,6 +118,7 @@ def get_contact_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
topic = get_topic_for_contacts(contact)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_contact_signed_up_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
contact = payload['data']['item']
|
||||
body = CONTACT_SIGNED_UP.format(
|
||||
@@ -123,27 +130,31 @@ def get_contact_signed_up_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
topic = get_topic_for_contacts(contact)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_contact_tag_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
body = CONTACT_TAG_CREATED.format(**payload['data']['item']['tag'])
|
||||
contact = payload['data']['item']['contact']
|
||||
topic = get_topic_for_contacts(contact)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_contact_tag_deleted_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
body = CONTACT_TAG_DELETED.format(**payload['data']['item']['tag'])
|
||||
contact = payload['data']['item']['contact']
|
||||
topic = get_topic_for_contacts(contact)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_conversation_admin_assigned_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
body = CONVERSATION_ADMIN_ASSIGNED.format(**payload['data']['item']['assignee'])
|
||||
user = payload['data']['item']['user']
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_conversation_admin_message(
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
) -> Tuple[str, str]:
|
||||
assignee = payload['data']['item']['assignee']
|
||||
user = payload['data']['item']['user']
|
||||
@@ -154,9 +165,10 @@ def get_conversation_admin_message(
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_conversation_admin_reply_message(
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
) -> Tuple[str, str]:
|
||||
assignee = payload['data']['item']['assignee']
|
||||
user = payload['data']['item']['user']
|
||||
@@ -170,8 +182,8 @@ def get_conversation_admin_reply_message(
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
def get_conversation_admin_single_created_message(
|
||||
payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
|
||||
def get_conversation_admin_single_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
assignee = payload['data']['item']['assignee']
|
||||
user = payload['data']['item']['user']
|
||||
conversation_body = payload['data']['item']['conversation_message']['body']
|
||||
@@ -183,6 +195,7 @@ def get_conversation_admin_single_created_message(
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_conversation_user_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']['user']
|
||||
conversation_body = payload['data']['item']['conversation_message']['body']
|
||||
@@ -194,6 +207,7 @@ def get_conversation_user_created_message(payload: Dict[str, Any]) -> Tuple[str,
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_conversation_user_replied_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']['user']
|
||||
note = payload['data']['item']['conversation_parts']['conversation_parts'][0]
|
||||
@@ -206,31 +220,36 @@ def get_conversation_user_replied_message(payload: Dict[str, Any]) -> Tuple[str,
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_event_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
event = payload['data']['item']
|
||||
body = EVENT_CREATED.format(**event)
|
||||
return ('Events', body)
|
||||
|
||||
|
||||
def get_user_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']
|
||||
body = USER_CREATED.format(**user)
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_user_deleted_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, 'User deleted.')
|
||||
|
||||
|
||||
def get_user_email_updated_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']
|
||||
body = 'User\'s email was updated to {}.'.format(user['email'])
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_user_tagged_message(
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
payload: Dict[str, Any],
|
||||
action: str,
|
||||
) -> Tuple[str, str]:
|
||||
user = payload['data']['item']['user']
|
||||
tag = payload['data']['item']['tag']
|
||||
@@ -241,12 +260,14 @@ def get_user_tagged_message(
|
||||
)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
def get_user_unsubscribed_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
||||
user = payload['data']['item']
|
||||
body = 'User unsubscribed from emails.'
|
||||
topic = get_topic_for_contacts(user)
|
||||
return (topic, body)
|
||||
|
||||
|
||||
EVENT_TO_FUNCTION_MAPPER = {
|
||||
'company.created': get_company_created_message,
|
||||
'contact.added_email': get_contact_added_email_message,
|
||||
@@ -259,8 +280,12 @@ EVENT_TO_FUNCTION_MAPPER = {
|
||||
'conversation.admin.opened': partial(get_conversation_admin_message, action='opened'),
|
||||
'conversation.admin.snoozed': partial(get_conversation_admin_message, action='snoozed'),
|
||||
'conversation.admin.unsnoozed': partial(get_conversation_admin_message, action='unsnoozed'),
|
||||
'conversation.admin.replied': partial(get_conversation_admin_reply_message, action='replied to'),
|
||||
'conversation.admin.noted': partial(get_conversation_admin_reply_message, action='added a note to'),
|
||||
'conversation.admin.replied': partial(
|
||||
get_conversation_admin_reply_message, action='replied to'
|
||||
),
|
||||
'conversation.admin.noted': partial(
|
||||
get_conversation_admin_reply_message, action='added a note to'
|
||||
),
|
||||
'conversation.admin.single.created': get_conversation_admin_single_created_message,
|
||||
'conversation.user.created': get_conversation_user_created_message,
|
||||
'conversation.user.replied': get_conversation_user_replied_message,
|
||||
@@ -276,16 +301,21 @@ EVENT_TO_FUNCTION_MAPPER = {
|
||||
'visitor.signed_up': get_contact_signed_up_message,
|
||||
}
|
||||
|
||||
|
||||
def get_event_handler(event_type: str) -> Callable[..., Tuple[str, str]]:
|
||||
handler: Any = EVENT_TO_FUNCTION_MAPPER.get(event_type)
|
||||
if handler is None:
|
||||
raise UnsupportedWebhookEventType(event_type)
|
||||
return handler
|
||||
|
||||
|
||||
@webhook_view('Intercom')
|
||||
@has_request_variables
|
||||
def api_intercom_webhook(request: HttpRequest, user_profile: UserProfile,
|
||||
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
||||
def api_intercom_webhook(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
payload: Dict[str, Any] = REQ(argument_type='body'),
|
||||
) -> HttpResponse:
|
||||
event_type = payload['topic']
|
||||
if event_type == 'ping':
|
||||
return json_success()
|
||||
|
||||
Reference in New Issue
Block a user