2020-06-11 00:54:34 +02:00
|
|
|
from typing import List, Optional, Tuple
|
|
|
|
|
2017-10-31 11:20:18 +01:00
|
|
|
from django.conf import settings
|
2017-10-27 17:25:51 +02:00
|
|
|
from django.db.models import Sum
|
|
|
|
from django.db.models.functions import Length
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.db.models.query import F
|
|
|
|
|
2019-02-02 23:45:30 +01:00
|
|
|
from zerver.models import BotStorageData, UserProfile
|
2017-10-27 17:25:51 +02:00
|
|
|
|
|
|
|
|
2017-10-31 11:10:56 +01:00
|
|
|
class StateError(Exception):
|
2017-10-31 11:04:46 +01:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_bot_storage(bot_profile: UserProfile, key: str) -> str:
|
2017-10-30 16:21:01 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
return BotStorageData.objects.get(bot_profile=bot_profile, key=key).value
|
|
|
|
except BotStorageData.DoesNotExist:
|
2017-11-20 14:49:25 +01:00
|
|
|
raise StateError("Key does not exist.")
|
2017-10-27 17:25:51 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def get_bot_storage_size(bot_profile: UserProfile, key: Optional[str] = None) -> int:
|
2017-10-27 17:25:51 +02:00
|
|
|
if key is None:
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
|
|
|
BotStorageData.objects.filter(bot_profile=bot_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
.annotate(key_size=Length("key"), value_size=Length("value"))
|
|
|
|
.aggregate(sum=Sum(F("key_size") + F("value_size")))["sum"]
|
2021-02-12 08:19:30 +01:00
|
|
|
or 0
|
|
|
|
)
|
2017-10-27 17:25:51 +02:00
|
|
|
else:
|
|
|
|
try:
|
2021-02-12 08:19:30 +01:00
|
|
|
return len(key) + len(
|
|
|
|
BotStorageData.objects.get(bot_profile=bot_profile, key=key).value
|
|
|
|
)
|
2017-11-24 10:18:29 +01:00
|
|
|
except BotStorageData.DoesNotExist:
|
2017-10-27 17:25:51 +02:00
|
|
|
return 0
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def set_bot_storage(bot_profile: UserProfile, entries: List[Tuple[str, str]]) -> None:
|
2017-11-24 10:18:29 +01:00
|
|
|
storage_size_limit = settings.USER_STATE_SIZE_LIMIT
|
|
|
|
storage_size_difference = 0
|
2017-11-20 14:54:41 +01:00
|
|
|
for key, value in entries:
|
2020-07-01 01:50:08 +02:00
|
|
|
assert isinstance(key, str), "Key type should be str."
|
|
|
|
assert isinstance(value, str), "Value type should be str."
|
2017-11-24 10:18:29 +01:00
|
|
|
storage_size_difference += (len(key) + len(value)) - get_bot_storage_size(bot_profile, key)
|
|
|
|
new_storage_size = get_bot_storage_size(bot_profile) + storage_size_difference
|
|
|
|
if new_storage_size > storage_size_limit:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise StateError(
|
|
|
|
"Request exceeds storage limit by {} characters. The limit is {} characters.".format(
|
|
|
|
new_storage_size - storage_size_limit, storage_size_limit
|
|
|
|
)
|
|
|
|
)
|
2017-10-31 11:04:46 +01:00
|
|
|
else:
|
2017-11-20 14:54:41 +01:00
|
|
|
for key, value in entries:
|
2021-02-12 08:19:30 +01:00
|
|
|
BotStorageData.objects.update_or_create(
|
2021-02-12 08:20:45 +01:00
|
|
|
bot_profile=bot_profile, key=key, defaults={"value": value}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def remove_bot_storage(bot_profile: UserProfile, keys: List[str]) -> None:
|
2017-11-24 10:18:29 +01:00
|
|
|
queryset = BotStorageData.objects.filter(bot_profile=bot_profile, key__in=keys)
|
2017-11-20 14:54:41 +01:00
|
|
|
if len(queryset) < len(keys):
|
|
|
|
raise StateError("Key does not exist.")
|
|
|
|
queryset.delete()
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def is_key_in_bot_storage(bot_profile: UserProfile, key: str) -> bool:
|
2017-11-24 10:18:29 +01:00
|
|
|
return BotStorageData.objects.filter(bot_profile=bot_profile, key=key).exists()
|
2017-11-20 14:40:51 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_keys_in_bot_storage(bot_profile: UserProfile) -> List[str]:
|
2021-02-12 08:19:30 +01:00
|
|
|
return list(
|
2021-02-12 08:20:45 +01:00
|
|
|
BotStorageData.objects.filter(bot_profile=bot_profile).values_list("key", flat=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|