2019-03-28 21:32:28 +01:00
|
|
|
from functools import partial
|
|
|
|
from html.parser import HTMLParser
|
2020-01-14 22:06:24 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Tuple
|
2018-01-06 12:11:44 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-08-19 22:26:38 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
2018-01-06 12:11:44 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-01-06 12:11:44 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
COMPANY_CREATED = """
|
|
|
|
New company **{name}** created:
|
|
|
|
* **User count**: {user_count}
|
|
|
|
* **Monthly spending**: {monthly_spend}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_EMAIL_ADDED = "New email {email} added to contact."
|
|
|
|
|
|
|
|
CONTACT_CREATED = """
|
|
|
|
New contact created:
|
|
|
|
* **Name (or pseudonym)**: {name}
|
|
|
|
* **Email**: {email}
|
|
|
|
* **Location**: {location_info}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_SIGNED_UP = """
|
|
|
|
Contact signed up:
|
|
|
|
* **Email**: {email}
|
|
|
|
* **Location**: {location_info}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_TAG_CREATED = "Contact tagged with the `{name}` tag."
|
|
|
|
|
|
|
|
CONTACT_TAG_DELETED = "The tag `{name}` was removed from the contact."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_ASSIGNED = "{name} assigned to conversation."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_TEMPLATE = "{admin_name} {action} the conversation."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_REPLY_TEMPLATE = """
|
|
|
|
{admin_name} {action} the conversation:
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_INITIATED_CONVERSATION = """
|
|
|
|
{admin_name} initiated a conversation:
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
EVENT_CREATED = "New event **{event_name}** created."
|
|
|
|
|
|
|
|
USER_CREATED = """
|
|
|
|
New user created:
|
|
|
|
* **Name**: {name}
|
|
|
|
* **Email**: {email}
|
|
|
|
""".strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
class MLStripper(HTMLParser):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.reset()
|
|
|
|
self.strict = False
|
|
|
|
self.convert_charrefs = True
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.fed: List[str] = []
|
2019-03-28 21:32:28 +01:00
|
|
|
|
|
|
|
def handle_data(self, d: str) -> None:
|
|
|
|
self.fed.append(d)
|
|
|
|
|
|
|
|
def get_data(self) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return "".join(self.fed)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def strip_tags(html: str) -> str:
|
|
|
|
s = MLStripper()
|
|
|
|
s.feed(html)
|
|
|
|
return s.get_data()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_topic_for_contacts(user: Dict[str, Any]) -> str:
|
|
|
|
topic = "{type}: {name}".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
type=user["type"].capitalize(),
|
|
|
|
name=user.get("name") or user.get("pseudonym") or user.get("email"),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return topic
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_company_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = COMPANY_CREATED.format(**payload["data"]["item"])
|
|
|
|
return ("Companies", body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_contact_added_email_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
|
|
|
body = CONTACT_EMAIL_ADDED.format(email=user["email"])
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_contact_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONTACT_CREATED.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
name=contact.get("name") or contact.get("pseudonym"),
|
|
|
|
email=contact["email"],
|
2019-03-28 21:32:28 +01:00
|
|
|
location_info="{city_name}, {region_name}, {country_name}".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
**contact["location_data"],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(contact)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_contact_signed_up_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONTACT_SIGNED_UP.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
email=contact["email"],
|
2019-03-28 21:32:28 +01:00
|
|
|
location_info="{city_name}, {region_name}, {country_name}".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
**contact["location_data"],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(contact)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_contact_tag_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = CONTACT_TAG_CREATED.format(**payload["data"]["item"]["tag"])
|
|
|
|
contact = payload["data"]["item"]["contact"]
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(contact)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_contact_tag_deleted_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = CONTACT_TAG_DELETED.format(**payload["data"]["item"]["tag"])
|
|
|
|
contact = payload["data"]["item"]["contact"]
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(contact)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_admin_assigned_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = CONVERSATION_ADMIN_ASSIGNED.format(**payload["data"]["item"]["assignee"])
|
|
|
|
user = payload["data"]["item"]["user"]
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_admin_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
payload: Dict[str, Any],
|
|
|
|
action: str,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_name=assignee.get("name"),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
action=action,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_admin_reply_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
payload: Dict[str, Any],
|
|
|
|
action: str,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
note = payload["data"]["item"]["conversation_parts"]["conversation_parts"][0]
|
|
|
|
content = strip_tags(note["body"])
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_REPLY_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_name=assignee.get("name"),
|
2019-03-28 21:32:28 +01:00
|
|
|
action=action,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def get_conversation_admin_single_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
conversation_body = payload["data"]["item"]["conversation_message"]["body"]
|
2019-03-28 21:32:28 +01:00
|
|
|
content = strip_tags(conversation_body)
|
|
|
|
body = CONVERSATION_ADMIN_INITIATED_CONVERSATION.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_name=assignee.get("name"),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_user_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
conversation_body = payload["data"]["item"]["conversation_message"]["body"]
|
2019-03-28 21:32:28 +01:00
|
|
|
content = strip_tags(conversation_body)
|
|
|
|
body = CONVERSATION_ADMIN_INITIATED_CONVERSATION.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_name=user.get("name"),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_user_replied_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
note = payload["data"]["item"]["conversation_parts"]["conversation_parts"][0]
|
|
|
|
content = strip_tags(note["body"])
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_REPLY_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_name=user.get("name"),
|
|
|
|
action="replied to",
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_event_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
event = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = EVENT_CREATED.format(**event)
|
2021-02-12 08:20:45 +01:00
|
|
|
return ("Events", body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_created_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = USER_CREATED.format(**user)
|
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_deleted_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
return (topic, "User deleted.")
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_email_updated_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
|
|
|
body = "User's email was updated to {}.".format(user["email"])
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_tagged_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
payload: Dict[str, Any],
|
|
|
|
action: str,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
tag = payload["data"]["item"]["tag"]
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
body = "The tag `{tag_name}` was {action} the user.".format(
|
|
|
|
tag_name=tag["name"],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
action=action,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_unsubscribed_message(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
|
|
|
body = "User unsubscribed from emails."
|
2019-03-28 21:32:28 +01:00
|
|
|
topic = get_topic_for_contacts(user)
|
|
|
|
return (topic, body)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-16 00:17:17 +01:00
|
|
|
EVENT_TO_FUNCTION_MAPPER: Dict[str, Callable[[Dict[str, Any]], Tuple[str, str]]] = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"company.created": get_company_created_message,
|
|
|
|
"contact.added_email": get_contact_added_email_message,
|
|
|
|
"contact.created": get_contact_created_message,
|
|
|
|
"contact.signed_up": get_contact_signed_up_message,
|
|
|
|
"contact.tag.created": get_contact_tag_created_message,
|
|
|
|
"contact.tag.deleted": get_contact_tag_deleted_message,
|
|
|
|
"conversation.admin.assigned": get_conversation_admin_assigned_message,
|
|
|
|
"conversation.admin.closed": partial(get_conversation_admin_message, action="closed"),
|
|
|
|
"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"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"conversation.admin.noted": partial(
|
|
|
|
get_conversation_admin_reply_message, action="added a note to"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"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,
|
|
|
|
"event.created": get_event_created_message,
|
|
|
|
"user.created": get_user_created_message,
|
|
|
|
"user.deleted": get_user_deleted_message,
|
|
|
|
"user.email.updated": get_user_email_updated_message,
|
|
|
|
"user.tag.created": partial(get_user_tagged_message, action="added to"),
|
|
|
|
"user.tag.deleted": partial(get_user_tagged_message, action="removed from"),
|
|
|
|
"user.unsubscribed": get_user_unsubscribed_message,
|
2019-03-28 21:32:28 +01:00
|
|
|
# Note that we do not have a payload for visitor.signed_up
|
|
|
|
# but it should be identical to contact.signed_up
|
2021-02-12 08:20:45 +01:00
|
|
|
"visitor.signed_up": get_contact_signed_up_message,
|
2019-03-28 21:32:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = list(EVENT_TO_FUNCTION_MAPPER.keys())
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Intercom", all_event_types=ALL_EVENT_TYPES)
|
2018-01-06 12:11:44 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_intercom_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
event_type = payload["topic"]
|
|
|
|
if event_type == "ping":
|
2019-03-28 21:32:28 +01:00
|
|
|
return json_success()
|
|
|
|
|
2021-02-16 00:17:17 +01:00
|
|
|
handler = EVENT_TO_FUNCTION_MAPPER.get(event_type)
|
|
|
|
if handler is None:
|
|
|
|
raise UnsupportedWebhookEventType(event_type)
|
|
|
|
topic, body = handler(payload)
|
2018-01-06 12:11:44 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event_type)
|
2018-01-06 12:11:44 +01:00
|
|
|
return json_success()
|