2024-07-12 02:30:17 +02:00
|
|
|
from typing import Any
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-06-15 20:56:36 +02:00
|
|
|
|
2022-04-14 23:49:26 +02:00
|
|
|
from zerver.actions.user_settings import do_change_user_setting
|
2018-06-15 20:56:36 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2018-06-15 20:56:36 +02:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def process_zcommands(content: str, user_profile: UserProfile) -> dict[str, Any]:
|
2021-02-12 08:19:30 +01:00
|
|
|
def change_mode_setting(
|
2021-11-22 19:26:13 +01:00
|
|
|
setting_name: str, switch_command: str, setting: str, setting_value: int
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> str:
|
2023-08-03 00:28:59 +02:00
|
|
|
msg = f"Changed to {setting_name}! To revert {setting_name}, type `/{switch_command}`."
|
2021-08-13 16:18:53 +02:00
|
|
|
do_change_user_setting(
|
2021-09-08 13:25:50 +02:00
|
|
|
user_profile=user_profile,
|
|
|
|
setting_name=setting,
|
|
|
|
setting_value=setting_value,
|
|
|
|
acting_user=user_profile,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-03-13 07:33:43 +01:00
|
|
|
return msg
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if not content.startswith("/"):
|
|
|
|
raise JsonableError(_("There should be a leading slash in the zcommand."))
|
2018-06-21 19:23:35 +02:00
|
|
|
command = content[1:]
|
2018-06-15 20:56:36 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if command == "ping":
|
2020-09-02 08:14:51 +02:00
|
|
|
return {}
|
2024-06-28 23:46:20 +02:00
|
|
|
elif command == "dark":
|
|
|
|
if user_profile.color_scheme == UserProfile.COLOR_SCHEME_DARK:
|
2021-11-18 11:44:31 +01:00
|
|
|
return dict(msg="You are still in dark theme.")
|
2021-02-12 08:19:30 +01:00
|
|
|
return dict(
|
|
|
|
msg=change_mode_setting(
|
2021-11-22 19:26:13 +01:00
|
|
|
setting_name="dark theme",
|
2021-09-12 00:39:46 +02:00
|
|
|
switch_command="light",
|
2021-02-12 08:20:45 +01:00
|
|
|
setting="color_scheme",
|
2024-06-28 23:46:20 +02:00
|
|
|
setting_value=UserProfile.COLOR_SCHEME_DARK,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2024-06-28 23:46:20 +02:00
|
|
|
elif command == "light":
|
2020-05-16 13:13:59 +02:00
|
|
|
if user_profile.color_scheme == UserProfile.COLOR_SCHEME_LIGHT:
|
2021-11-19 04:16:07 +01:00
|
|
|
return dict(msg="You are still in light theme.")
|
2021-02-12 08:19:30 +01:00
|
|
|
return dict(
|
|
|
|
msg=change_mode_setting(
|
2021-11-22 19:26:13 +01:00
|
|
|
setting_name="light theme",
|
2021-09-12 00:39:46 +02:00
|
|
|
switch_command="dark",
|
2021-02-12 08:20:45 +01:00
|
|
|
setting="color_scheme",
|
2021-02-12 08:19:30 +01:00
|
|
|
setting_value=UserProfile.COLOR_SCHEME_LIGHT,
|
|
|
|
)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif command == "fluid-width":
|
2020-03-12 07:51:47 +01:00
|
|
|
if user_profile.fluid_layout_width:
|
2021-02-12 08:20:45 +01:00
|
|
|
return dict(msg="You are still in fluid width mode.")
|
2021-02-12 08:19:30 +01:00
|
|
|
return dict(
|
|
|
|
msg=change_mode_setting(
|
2021-11-22 19:26:13 +01:00
|
|
|
setting_name="fluid-width mode",
|
2021-02-12 08:20:45 +01:00
|
|
|
switch_command="fixed-width",
|
|
|
|
setting="fluid_layout_width",
|
2021-02-12 08:19:30 +01:00
|
|
|
setting_value=True,
|
|
|
|
)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif command == "fixed-width":
|
2020-03-12 07:51:47 +01:00
|
|
|
if not user_profile.fluid_layout_width:
|
2021-02-12 08:20:45 +01:00
|
|
|
return dict(msg="You are still in fixed width mode.")
|
2021-02-12 08:19:30 +01:00
|
|
|
return dict(
|
|
|
|
msg=change_mode_setting(
|
2021-11-22 19:26:13 +01:00
|
|
|
setting_name="fixed-width mode",
|
2021-02-12 08:20:45 +01:00
|
|
|
switch_command="fluid-width",
|
|
|
|
setting="fluid_layout_width",
|
2021-02-12 08:19:30 +01:00
|
|
|
setting_value=False,
|
|
|
|
)
|
|
|
|
)
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("No such command: {command}").format(command=command))
|