Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks: Add support for gmail and rules sources to Front integration.
Alex Vandiver6e842f54c11d
diff --git a/zerver/webhooks/front/fixtures/gmail.json b/zerver/webhooks/front/fixtures/gmail.jsonnew file mode 100644index 00000000000..2452965aac9--- /dev/null+++ b/zerver/webhooks/front/fixtures/gmail.json@@ -0,0 +1,109 @@+{+ "_links": {+ "self": "https://api2.frontapp.com/events/evt_28du7y2"+ },+ "id": "evt_28du7y2",+ "type": "archive",+ "emitted_at": 1518307966.678,+ "conversation": {+ "_links": {+ "self": "https://api2.frontapp.com/conversations/cnv_keo696",+ "related": {+ "events": "https://api2.frontapp.com/conversations/cnv_keo696/events",+ "followers": "https://api2.frontapp.com/conversations/cnv_keo696/followers",+ "messages": "https://api2.frontapp.com/conversations/cnv_keo696/messages",+ "comments": "https://api2.frontapp.com/conversations/cnv_keo696/comments",+ "inboxes": "https://api2.frontapp.com/conversations/cnv_keo696/inboxes"+ }+ },+ "id": "cnv_keo696",+ "subject": "Your next delivery is on Epsilon 96Z",+ "status": "archived",+ "assignee": {+ "_links": {+ "self": "https://api2.frontapp.com/teammates/tea_40yq",+ "related": {+ "inboxes": "https://api2.frontapp.com/teammates/tea_40yq/inboxes",+ "conversations": "https://api2.frontapp.com/teammates/tea_40yq/conversations"+ }+ },+ "id": "tea_40yq",+ "email": "[email protected]",+ "username": "leela",+ "first_name": "Leela",+ "last_name": "Turanga",+ "is_admin": true,+ "is_available": true+ },+ "recipient": {+ "_links": {+ "related": {+ "contact": "https://api2.frontapp.com/contacts/crd_62euuy"+ }+ },+ "handle": "[email protected]",+ "role": "to"+ },+ "tags": [],+ "last_message": {+ "_links": {+ "self": "https://api2.frontapp.com/messages/msg_1176ie2",+ "related": {+ "conversation": "https://api2.frontapp.com/conversations/cnv_keo696"+ }+ },+ "id": "msg_1176ie2",+ "type": "email",+ "is_inbound": false,+ "created_at": 1518307909.541,+ "blurb": "There will be plenty of time to discuss your objections when and if you return. ",+ "body": "<div>There will be plenty of time to discuss your objections when and if you return.</div>",+ "text": "There will be plenty of time to discuss your objections when and if you return.\n",+ "metadata": {},+ "author": {+ "_links": {+ "self": "https://api2.frontapp.com/teammates/tea_40yq",+ "related": {+ "inboxes": "https://api2.frontapp.com/teammates/tea_40yq/inboxes",+ "conversations": "https://api2.frontapp.com/teammates/tea_40yq/conversations"+ }+ },+ "id": "tea_40yq",+ "email": "[email protected]",+ "username": "leela",+ "first_name": "Leela",+ "last_name": "Turanga",+ "is_admin": true,+ "is_available": true+ },+ "recipients": [+ {+ "_links": {+ "related": {+ "contact": null+ }+ },+ "handle": "[email protected]",+ "role": "from"+ },+ {+ "_links": {+ "related": {+ "contact": "https://api2.frontapp.com/contacts/crd_62euuy"+ }+ },+ "handle": "[email protected]",+ "role": "to"+ }+ ],+ "attachments": []+ },+ "created_at": 1518307874.673+ },+ "source": {+ "_meta": {+ "type": "gmail"+ },+ "data": null+ }+}diff --git a/zerver/webhooks/front/fixtures/rule.json b/zerver/webhooks/front/fixtures/rule.jsonnew file mode 100644index 00000000000..f18bd31f3ed--- /dev/null+++ b/zerver/webhooks/front/fixtures/rule.json@@ -0,0 +1,144 @@+{+ "_links": {+ "self": "https://api2.frontapp.com/events/evt_28duxju"+ },+ "id": "evt_28duxju",+ "type": "assign",+ "emitted_at": 1518309907.474,+ "conversation": {+ "_links": {+ "self": "https://api2.frontapp.com/conversations/cnv_keocka",+ "related": {+ "events": "https://api2.frontapp.com/conversations/cnv_keocka/events",+ "followers": "https://api2.frontapp.com/conversations/cnv_keocka/followers",+ "messages": "https://api2.frontapp.com/conversations/cnv_keocka/messages",+ "comments": "https://api2.frontapp.com/conversations/cnv_keocka/comments",+ "inboxes": "https://api2.frontapp.com/conversations/cnv_keocka/inboxes"+ }+ },+ "id": "cnv_keocka",+ "subject": "Re: Being a robot is great, but...",+ "status": "assigned",+ "assignee": {+ "_links": {+ "self": "https://api2.frontapp.com/teammates/tea_40z6",+ "related": {+ "inboxes": "https://api2.frontapp.com/teammates/tea_40z6/inboxes",+ "conversations": "https://api2.frontapp.com/teammates/tea_40z6/conversations"+ }+ },+ "id": "tea_40z6",+ "email": "[email protected]",+ "username": "bender",+ "first_name": "Bender",+ "last_name": "Rodriguez",+ "is_admin": false,+ "is_available": true+ },+ "recipient": {+ "_links": {+ "related": {+ "contact": "https://api2.frontapp.com/contacts/crd_62euuy"+ }+ },+ "handle": "[email protected]",+ "role": "to"+ },+ "tags": [],+ "last_message": {+ "_links": {+ "self": "https://api2.frontapp.com/messages/msg_1176ryy",+ "related": {+ "conversation": "https://api2.frontapp.com/conversations/cnv_keocka",+ "message_replied_to": "https://api2.frontapp.com/messages/msg_1176r8y"+ }+ },+ "id": "msg_1176ryy",+ "type": "email",+ "is_inbound": false,+ "created_at": 1518309342.896,+ "blurb": "Emotions are dumb and should be hated. ",+ "body": "<div>Emotions are dumb and should be hated.</div>",+ "text": "Emotions are dumb and should be hated.\n",+ "metadata": {},+ "author": {+ "_links": {+ "self": "https://api2.frontapp.com/teammates/tea_40yq",+ "related": {+ "inboxes": "https://api2.frontapp.com/teammates/tea_40yq/inboxes",+ "conversations": "https://api2.frontapp.com/teammates/tea_40yq/conversations"+ }+ },+ "id": "tea_40yq",+ "email": "[email protected]",+ "username": "leela",+ "first_name": "Leela",+ "last_name": "Turanga",+ "is_admin": true,+ "is_available": true+ },+ "recipients": [+ {+ "_links": {+ "related": {+ "contact": null+ }+ },+ "handle": "[email protected]",+ "role": "from"+ },+ {+ "_links": {+ "related": {+ "contact": "https://api2.frontapp.com/contacts/crd_62euuy"+ }+ },+ "handle": "[email protected]",+ "role": "to"+ }+ ],+ "attachments": []+ },+ "created_at": 1518309203.872+ },+ "source": {+ "_meta": {+ "type": "rule"+ },+ "data": {+ "_links": {+ "self": "https://api2.frontapp.com/rules/rul_55c8c149",+ "related": {+ "owner": "https://api2.frontapp.com/teams/tim_55c8c149"+ }+ },+ "id": "rul_55c8c149",+ "name": "Important deliveries",+ "actions": [+ "Assign to Bender Rodriguez"+ ],+ "is_private": false+ }+ },+ "target": {+ "_meta": {+ "type": "teammate"+ },+ "data": {+ "_links": {+ "self": "https://api2.frontapp.com/teammates/tea_40z6",+ "related": {+ "inboxes": "https://api2.frontapp.com/teammates/tea_40z6/inboxes",+ "conversations": "https://api2.frontapp.com/teammates/tea_40z6/conversations"+ }+ },+ "id": "tea_40z6",+ "email": "[email protected]",+ "username": "bender",+ "first_name": "Bender",+ "last_name": "Rodriguez",+ "is_admin": false,+ "is_available": true+ }+ }+}diff --git a/zerver/webhooks/front/tests.py b/zerver/webhooks/front/tests.pyindex a94ca775d9b..32976c6d3b8 100644--- a/zerver/webhooks/front/tests.py+++ b/zerver/webhooks/front/tests.py@@ -211,6 +211,28 @@ class FrontHookTests(WebhookTestCase):content_type="application/x-www-form-urlencoded",)+ def test_conversation_assigned_by_rule(self) -> None:+ expected_topic = "cnv_keocka"+ expected_message = "**'Important deliveries' rule** assigned **Bender Rodriguez**."++ self.check_webhook(+ "rule",+ expected_topic,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )++ def test_conversation_assigned_by_gmail(self) -> None:+ expected_topic = "cnv_keo696"+ expected_message = "Archived by **(gmail)**."++ self.check_webhook(+ "gmail",+ expected_topic,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )+def test_unknown_webhook_request(self) -> None:payload = self.get_body("conversation_assigned")payload_json = orjson.loads(payload)diff --git a/zerver/webhooks/front/view.py b/zerver/webhooks/front/view.pyindex bf43456de6e..94cfe9e98c7 100644--- a/zerver/webhooks/front/view.py+++ b/zerver/webhooks/front/view.py@@ -21,9 +21,15 @@ def get_message_data(payload: WildValue) -> Tuple[str, str, str, str]:def get_source_name(payload: WildValue) -> str:- first_name = payload["source"]["data"]["first_name"].tame(check_string)- last_name = payload["source"]["data"]["last_name"].tame(check_string)- return f"{first_name} {last_name}"+ type = payload["source"]["_meta"]["type"].tame(check_string)+ if type == "teammate":+ first_name = payload["source"]["data"]["first_name"].tame(check_string)+ last_name = payload["source"]["data"]["last_name"].tame(check_string)+ return f"{first_name} {last_name}"+ elif type == "rule":+ name = payload["source"]["data"]["name"].tame(check_string)+ return f"'{name}' rule"+ return f"({type})"def get_target_name(payload: WildValue) -> str: