2018-06-15 20:56:36 +02:00
|
|
|
from typing import Any, Dict
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
from zerver.lib.actions import do_set_user_display_setting
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
|
2018-06-20 20:46:33 +02:00
|
|
|
def process_zcommands(content: str, user_profile: UserProfile) -> Dict[str, Any]:
|
2020-03-13 07:33:43 +01:00
|
|
|
def change_mode_setting(command: str, switch_command: str,
|
|
|
|
setting: str, setting_value: bool) -> str:
|
|
|
|
msg = 'Changed to {command} mode! To revert ' \
|
|
|
|
'{command} mode, type `/{switch_command}`.'.format(
|
|
|
|
command=command,
|
|
|
|
switch_command=switch_command
|
|
|
|
)
|
|
|
|
do_set_user_display_setting(user_profile=user_profile,
|
|
|
|
setting_name=setting,
|
|
|
|
setting_value=setting_value)
|
|
|
|
return msg
|
|
|
|
|
2018-06-21 19:23:35 +02:00
|
|
|
if not content.startswith('/'):
|
|
|
|
raise JsonableError(_('There should be a leading slash in the zcommand.'))
|
|
|
|
command = content[1:]
|
2018-06-15 20:56:36 +02:00
|
|
|
|
|
|
|
if command == 'ping':
|
2020-03-13 07:33:43 +01:00
|
|
|
return dict()
|
|
|
|
elif command == 'night':
|
2018-06-15 20:56:36 +02:00
|
|
|
if user_profile.night_mode:
|
2020-03-13 07:33:43 +01:00
|
|
|
return dict(msg='You are still in night mode.')
|
|
|
|
return dict(msg=change_mode_setting(command=command,
|
|
|
|
switch_command='day',
|
|
|
|
setting='night_mode',
|
|
|
|
setting_value=True))
|
|
|
|
elif command == 'day':
|
|
|
|
if not user_profile.night_mode:
|
|
|
|
return dict(msg='You are still in day mode.')
|
|
|
|
return dict(msg=change_mode_setting(command=command,
|
|
|
|
switch_command='night',
|
|
|
|
setting='night_mode',
|
|
|
|
setting_value=False))
|
2020-03-12 07:51:47 +01:00
|
|
|
elif command == 'fluid-width':
|
|
|
|
if user_profile.fluid_layout_width:
|
|
|
|
return dict(msg='You are still in fluid width mode.')
|
|
|
|
return dict(msg=change_mode_setting(command=command,
|
|
|
|
switch_command='fixed-width',
|
|
|
|
setting='fluid_layout_width',
|
|
|
|
setting_value=True))
|
|
|
|
elif command == 'fixed-width':
|
|
|
|
if not user_profile.fluid_layout_width:
|
|
|
|
return dict(msg='You are still in fixed width mode.')
|
|
|
|
return dict(msg=change_mode_setting(command=command,
|
|
|
|
switch_command='fluid-width',
|
|
|
|
setting='fluid_layout_width',
|
|
|
|
setting_value=False))
|
2018-06-15 20:56:36 +02:00
|
|
|
raise JsonableError(_('No such command: %s') % (command,))
|