zulip/zerver/lib/widget.py
Steve Howell 47b4dd6bdb slash commands: Refine /day and /night.
These two slash commands now use zcommand to talk to
the server, so we have no Message overhead, and if you're
on a stream, you no longer spam people by accident.

The commands now also give reasonable messages
if you are already in the mode you ask for.

It should be noted that by moving these commands out of
widget.py, they are no longer behind the ALLOW_SUB_MESSAGES
setting guard.
2018-06-02 09:40:12 -07:00

56 lines
1.5 KiB
Python

from typing import MutableMapping, Any
from django.conf import settings
import re
import json
from zerver.models import SubMessage
def do_widget_pre_save_actions(message: MutableMapping[str, Any]) -> None:
if not settings.ALLOW_SUB_MESSAGES:
return
content = message['message'].content
if content == '/stats':
message['message'].content = 'We are running **1 server**.'
return
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
if content in ['/poll', '/tictactoe']:
widget_type = content[1:]
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])