2017-11-20 14:40:51 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2024-06-06 14:38:28 +02:00
|
|
|
from pydantic import Json
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-11-20 14:40:51 +01:00
|
|
|
from zerver.lib.bot_storage import (
|
2020-06-11 00:54:34 +02:00
|
|
|
StateError,
|
2017-11-24 10:18:29 +01:00
|
|
|
get_bot_storage,
|
|
|
|
get_keys_in_bot_storage,
|
2020-06-11 00:54:34 +02:00
|
|
|
remove_bot_storage,
|
|
|
|
set_bot_storage,
|
2017-11-20 14:40:51 +01:00
|
|
|
)
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2024-06-06 14:38:28 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2017-11-20 14:40:51 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
2024-06-06 14:38:28 +02:00
|
|
|
@typed_endpoint
|
2020-06-21 03:23:55 +02:00
|
|
|
def update_storage(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-06 14:38:28 +02:00
|
|
|
*,
|
2024-07-12 02:30:17 +02:00
|
|
|
storage: Json[dict[str, str]],
|
2020-06-21 03:23:55 +02:00
|
|
|
) -> HttpResponse:
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
set_bot_storage(user_profile, list(storage.items()))
|
2020-06-21 03:23:55 +02:00
|
|
|
except StateError as e: # nocoverage
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(str(e))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-11-20 14:40:51 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-06 14:38:28 +02:00
|
|
|
@typed_endpoint
|
2018-03-12 00:47:54 +01:00
|
|
|
def get_storage(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-06 14:38:28 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
keys: Json[list[str] | None] = None,
|
2018-03-12 00:47:54 +01:00
|
|
|
) -> HttpResponse:
|
2021-04-12 23:41:38 +02:00
|
|
|
if keys is None:
|
|
|
|
keys = get_keys_in_bot_storage(user_profile)
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
storage = {key: get_bot_storage(user_profile, key) for key in keys}
|
2017-11-20 14:40:51 +01:00
|
|
|
except StateError as e:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(str(e))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"storage": storage})
|
2017-11-20 14:40:51 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-06 14:38:28 +02:00
|
|
|
@typed_endpoint
|
2018-03-12 00:47:54 +01:00
|
|
|
def remove_storage(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-06 14:38:28 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
keys: Json[list[str] | None] = None,
|
2018-03-12 00:47:54 +01:00
|
|
|
) -> HttpResponse:
|
2021-04-12 23:41:38 +02:00
|
|
|
if keys is None:
|
|
|
|
keys = get_keys_in_bot_storage(user_profile)
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
remove_bot_storage(user_profile, keys)
|
2017-11-20 14:40:51 +01:00
|
|
|
except StateError as e:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(str(e))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|