mirror of
https://github.com/zulip/zulip.git
synced 2025-11-21 23:19:10 +00:00
The "/stats" command doesn't actually do anything interesting yet, and it also writes to the message feed instead of replying directly to the user. The history of this command was that it was written during a PyCon sprint. It was mainly intended as an example for subsequent slash commands. The ones we built after "/stats" have sort of outgrown "/stats" and don't follow the original structure for "/stats". (The "/day", "/ping", and "/settings" commands were built shortly after.)j We probably want to ressurect "/stats" fairly soon, after figuring out some useful stats and refining the UI. As you can see from this commit, resurrecting the code here shouldn't be too difficult, but it may actually be pretty rare that we just translate slash commands into fleshed out messages.
71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
from typing import MutableMapping, Any, Optional, List, Tuple
|
|
from django.conf import settings
|
|
|
|
import re
|
|
import json
|
|
|
|
from zerver.models import SubMessage
|
|
|
|
|
|
def get_widget_data(content: str) -> Tuple[Optional[str], Optional[str]]:
|
|
valid_widget_types = ['tictactoe', 'poll', 'todo']
|
|
tokens = content.split(' ')
|
|
if not tokens:
|
|
return None, None
|
|
|
|
if tokens[0].startswith('/'):
|
|
widget_type = tokens[0][1:]
|
|
if widget_type in valid_widget_types:
|
|
extra_data = get_extra_data_from_widget_type(tokens, widget_type)
|
|
return widget_type, extra_data
|
|
|
|
return None, None
|
|
|
|
def get_extra_data_from_widget_type(tokens: List[str],
|
|
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
|
|
question = ' '.join(tokens[1:])
|
|
if not question:
|
|
question = ''
|
|
extra_data = {'question': question}
|
|
return extra_data
|
|
return None
|
|
|
|
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
|
|
'''
|
|
This is experimental code that only works with the
|
|
webapp for now.
|
|
'''
|
|
if not settings.ALLOW_SUB_MESSAGES:
|
|
return
|
|
content = message['message'].content
|
|
sender_id = message['message'].sender_id
|
|
message_id = message['message'].id
|
|
|
|
widget_type = None
|
|
extra_data = None
|
|
|
|
widget_type, extra_data = get_widget_data(content)
|
|
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']
|
|
|
|
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])
|