mirror of https://github.com/zulip/zulip.git
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
|
from django.http import HttpRequest, HttpResponse
|
||
|
from django.utils.translation import ugettext as _
|
||
|
from zerver.lib.bot_storage import (
|
||
|
get_bot_state,
|
||
|
set_bot_state,
|
||
|
remove_bot_state,
|
||
|
get_keys_in_bot_state,
|
||
|
is_key_in_bot_state,
|
||
|
StateError,
|
||
|
)
|
||
|
from zerver.decorator import has_request_variables, REQ
|
||
|
from zerver.lib.response import json_success, json_error
|
||
|
from zerver.lib.validator import check_dict, check_list, check_string
|
||
|
from zerver.models import UserProfile
|
||
|
|
||
|
from typing import Dict, List, Optional
|
||
|
|
||
|
@has_request_variables
|
||
|
def update_state(request, user_profile, state=REQ(validator=check_dict([]))):
|
||
|
# type: (HttpRequest, UserProfile, Optional[Dict[str, str]]) -> HttpResponse
|
||
|
try:
|
||
|
set_bot_state(user_profile, list(state.items()))
|
||
|
except StateError as e:
|
||
|
return json_error(str(e))
|
||
|
return json_success()
|
||
|
|
||
|
@has_request_variables
|
||
|
def get_state(request, user_profile, keys=REQ(validator=check_list(check_string), default=None)):
|
||
|
# type: (HttpRequest, UserProfile, Optional[List[str]]) -> HttpResponse
|
||
|
keys = keys or get_keys_in_bot_state(user_profile)
|
||
|
try:
|
||
|
state = {key: get_bot_state(user_profile, key) for key in keys}
|
||
|
except StateError as e:
|
||
|
return json_error(str(e))
|
||
|
return json_success({'state': state})
|
||
|
|
||
|
@has_request_variables
|
||
|
def remove_state(request, user_profile, keys=REQ(validator=check_list(check_string), default=None)):
|
||
|
# type: (HttpRequest, UserProfile, Optional[List[str]]) -> HttpResponse
|
||
|
keys = keys or get_keys_in_bot_state(user_profile)
|
||
|
try:
|
||
|
remove_bot_state(user_profile, keys)
|
||
|
except StateError as e:
|
||
|
return json_error(str(e))
|
||
|
return json_success()
|