2018-06-28 21:17:55 +02:00
|
|
|
from typing import MutableMapping, Any, Optional, List, Tuple
|
2018-02-23 12:26:56 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
2018-05-16 19:28:39 +02:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
|
|
|
from zerver.models import SubMessage
|
|
|
|
|
|
|
|
|
2018-06-28 21:17:55 +02:00
|
|
|
def get_widget_data(content: str) -> Tuple[Optional[str], Optional[str]]:
|
2018-07-02 19:57:01 +02:00
|
|
|
valid_widget_types = ['tictactoe', 'poll', 'todo']
|
2018-06-28 21:17:55 +02:00
|
|
|
tokens = content.split(' ')
|
|
|
|
|
2018-08-23 14:51:17 +02:00
|
|
|
# tokens[0] will always exist
|
2018-06-28 21:17:55 +02:00
|
|
|
if tokens[0].startswith('/'):
|
|
|
|
widget_type = tokens[0][1:]
|
|
|
|
if widget_type in valid_widget_types:
|
2019-01-28 18:37:28 +01:00
|
|
|
remaining_content = content.replace(tokens[0], '', 1).strip()
|
|
|
|
extra_data = get_extra_data_from_widget_type(remaining_content, widget_type)
|
2018-06-28 21:17:55 +02:00
|
|
|
return widget_type, extra_data
|
|
|
|
|
|
|
|
return None, None
|
|
|
|
|
2019-01-28 18:37:28 +01:00
|
|
|
def get_extra_data_from_widget_type(content: str,
|
2018-06-28 21:17:55 +02:00
|
|
|
widget_type: Optional[str]) -> Any:
|
|
|
|
if widget_type == 'poll':
|
|
|
|
# This is used to extract the question from the poll command.
|
|
|
|
# The command '/poll question' will pre-set the question in the poll
|
2019-01-28 18:37:28 +01:00
|
|
|
lines = content.splitlines()
|
|
|
|
question = ''
|
|
|
|
options = []
|
|
|
|
if lines and lines[0]:
|
|
|
|
question = lines.pop(0).strip()
|
|
|
|
for line in lines:
|
|
|
|
# If someone is using the list syntax, we remove it
|
|
|
|
# before adding an option.
|
2019-01-28 19:13:56 +01:00
|
|
|
option = re.sub(r'(\s*[-*]?\s*)', '', line.strip(), 1)
|
2019-01-28 18:37:28 +01:00
|
|
|
if len(option) > 0:
|
|
|
|
options.append(option)
|
|
|
|
extra_data = {
|
|
|
|
'question': question,
|
|
|
|
'options': options,
|
|
|
|
}
|
2018-06-28 21:17:55 +02:00
|
|
|
return extra_data
|
|
|
|
return None
|
|
|
|
|
2018-02-23 12:26:56 +01:00
|
|
|
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
|
|
|
|
'''
|
2019-01-29 16:57:45 +01:00
|
|
|
This code works with the webapp; mobile and other
|
|
|
|
clients should also start supporting this soon.
|
2018-02-23 12:26:56 +01:00
|
|
|
'''
|
2018-05-16 19:28:39 +02:00
|
|
|
content = message['message'].content
|
|
|
|
sender_id = message['message'].sender_id
|
|
|
|
message_id = message['message'].id
|
|
|
|
|
|
|
|
widget_type = None
|
|
|
|
extra_data = None
|
|
|
|
|
2018-06-28 21:17:55 +02:00
|
|
|
widget_type, extra_data = get_widget_data(content)
|
2018-05-21 15:23:46 +02:00
|
|
|
widget_content = message.get('widget_content')
|
|
|
|
if widget_content is not None:
|
|
|
|
# Note that we validate this data in check_message,
|
|
|
|
# so we can trust it here.
|
|
|
|
widget_type = widget_content['widget_type']
|
|
|
|
extra_data = widget_content['extra_data']
|
|
|
|
|
2018-05-16 19:28:39 +02:00
|
|
|
if widget_type:
|
|
|
|
content = dict(
|
|
|
|
widget_type=widget_type,
|
|
|
|
extra_data=extra_data
|
|
|
|
)
|
|
|
|
submessage = SubMessage(
|
|
|
|
sender_id=sender_id,
|
|
|
|
message_id=message_id,
|
|
|
|
msg_type='widget',
|
|
|
|
content=json.dumps(content),
|
|
|
|
)
|
|
|
|
submessage.save()
|
|
|
|
message['submessages'] = SubMessage.get_raw_db_rows([message_id])
|