2017-03-14 20:04:11 +01:00
|
|
|
import logging
|
2017-11-16 00:43:10 +01:00
|
|
|
import re
|
2017-03-14 20:04:11 +01:00
|
|
|
from typing import Any, Dict, Text
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-03-14 20:04:11 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
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
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-03-14 20:04:11 +01:00
|
|
|
|
|
|
|
from .support_event import SUPPORT_EVENTS
|
|
|
|
|
|
|
|
DOCUMENT_TEMPLATE = "{user_name} {verb} the document [{title}]({url})"
|
|
|
|
QUESTION_TEMPLATE = "{user_name} {verb} the question [{title}]({url})"
|
2017-10-27 02:36:54 +02:00
|
|
|
QUESTIONS_ANSWER_TEMPLATE = ("{user_name} {verb} the [answer]({answer_url}) " +
|
|
|
|
"of the question [{question_title}]({question_url})")
|
|
|
|
COMMENT_TEMPLATE = ("{user_name} {verb} the [comment]({answer_url}) "
|
|
|
|
"of the task [{task_title}]({task_url})")
|
2017-03-14 20:04:11 +01:00
|
|
|
MESSAGE_TEMPLATE = "{user_name} {verb} the message [{title}]({url})"
|
|
|
|
TODO_LIST_TEMPLATE = "{user_name} {verb} the todo list [{title}]({url})"
|
|
|
|
TODO_TEMPLATE = "{user_name} {verb} the todo task [{title}]({url})"
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Basecamp')
|
|
|
|
@has_request_variables
|
2017-12-06 19:38:47 +01:00
|
|
|
def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body'),
|
|
|
|
stream: Text=REQ(default='basecamp')) -> HttpResponse:
|
2017-03-14 20:04:11 +01:00
|
|
|
event = get_event_type(payload)
|
|
|
|
|
|
|
|
if event not in SUPPORT_EVENTS:
|
|
|
|
logging.warning("Basecamp {} event is not supported".format(event))
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
subject = get_project_name(payload)
|
|
|
|
if event.startswith('document_'):
|
|
|
|
body = get_document_body(event, payload)
|
|
|
|
elif event.startswith('question_answer_'):
|
|
|
|
body = get_questions_answer_body(event, payload)
|
|
|
|
elif event.startswith('question_'):
|
|
|
|
body = get_questions_body(event, payload)
|
|
|
|
elif event.startswith('message_'):
|
|
|
|
body = get_message_body(event, payload)
|
|
|
|
elif event.startswith('todolist_'):
|
|
|
|
body = get_todo_list_body(event, payload)
|
|
|
|
elif event.startswith('todo_'):
|
|
|
|
body = get_todo_body(event, payload)
|
|
|
|
elif event.startswith('comment_'):
|
|
|
|
body = get_comment_body(event, payload)
|
|
|
|
else:
|
|
|
|
logging.warning("Basecamp handling of {} event is not implemented".format(event))
|
|
|
|
return json_success()
|
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client,
|
|
|
|
stream, subject, body)
|
2017-03-14 20:04:11 +01:00
|
|
|
return json_success()
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_project_name(payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return payload['recording']['bucket']['name']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_event_type(payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return payload['kind']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_event_creator(payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return payload['creator']['name']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_subject_url(payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return payload['recording']['app_url']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_subject_title(payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return payload['recording']['title']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_verb(event: Text, prefix: Text) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
verb = event.replace(prefix, '')
|
|
|
|
if verb == 'active':
|
|
|
|
return 'activated'
|
|
|
|
|
|
|
|
matched = re.match(r"(?P<subject>[A-z]*)_changed", verb)
|
|
|
|
if matched:
|
|
|
|
return "changed {} of".format(matched.group('subject'))
|
|
|
|
return verb
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_document_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return get_generic_body(event, payload, 'document_', DOCUMENT_TEMPLATE)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_questions_answer_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
verb = get_verb(event, 'question_answer_')
|
|
|
|
question = payload['recording']['parent']
|
|
|
|
|
|
|
|
return QUESTIONS_ANSWER_TEMPLATE.format(
|
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
|
|
|
answer_url=get_subject_url(payload),
|
|
|
|
question_title=question['title'],
|
|
|
|
question_url=question['app_url']
|
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_comment_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
verb = get_verb(event, 'comment_')
|
|
|
|
task = payload['recording']['parent']
|
|
|
|
|
|
|
|
return COMMENT_TEMPLATE.format(
|
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
|
|
|
answer_url=get_subject_url(payload),
|
|
|
|
task_title=task['title'],
|
|
|
|
task_url=task['app_url']
|
|
|
|
)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_questions_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return get_generic_body(event, payload, 'question_', QUESTION_TEMPLATE)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_message_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return get_generic_body(event, payload, 'message_', MESSAGE_TEMPLATE)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_todo_list_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return get_generic_body(event, payload, 'todolist_', TODO_LIST_TEMPLATE)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_todo_body(event: Text, payload: Dict[str, Any]) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
return get_generic_body(event, payload, 'todo_', TODO_TEMPLATE)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_generic_body(event: Text, payload: Dict[str, Any], prefix: Text, template: Text) -> Text:
|
2017-03-14 20:04:11 +01:00
|
|
|
verb = get_verb(event, prefix)
|
|
|
|
|
|
|
|
return template.format(
|
|
|
|
user_name=get_event_creator(payload),
|
|
|
|
verb=verb,
|
|
|
|
title=get_subject_title(payload),
|
|
|
|
url=get_subject_url(payload),
|
|
|
|
)
|