2024-12-02 12:58:16 +00:00
|
|
|
from typing import List, Dict
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
from aiodynamo.client import Table
|
|
|
|
from aiodynamo.errors import ItemNotFound
|
|
|
|
|
|
|
|
import token_bot.persistant_database as pdb
|
2024-12-02 12:58:16 +00:00
|
|
|
from token_bot.token_database.region import Region
|
|
|
|
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
class User:
|
2024-12-09 01:07:26 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
user_id: int,
|
|
|
|
region: Region = None,
|
|
|
|
subscribed_alerts: List["pdb.Alert"] = None,
|
|
|
|
) -> None:
|
2024-11-30 11:27:32 +00:00
|
|
|
self.user_id: int = user_id
|
|
|
|
self._loaded: bool = False
|
|
|
|
self.region: Region = region
|
|
|
|
self.subscribed_alerts: List[pdb.Alert] = subscribed_alerts
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.user_id == other.user_id
|
|
|
|
|
2024-12-01 07:42:35 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.user_id)
|
|
|
|
|
2024-11-30 11:27:32 +00:00
|
|
|
@classmethod
|
2024-12-09 01:07:26 +00:00
|
|
|
def from_item(
|
|
|
|
cls, primary_key: int, region: Region, subscribed_alerts: List[str]
|
|
|
|
) -> "User":
|
2024-11-30 11:27:32 +00:00
|
|
|
alerts = [pdb.Alert.from_str(alert_str) for alert_str in subscribed_alerts]
|
|
|
|
return cls(primary_key, region, alerts)
|
|
|
|
|
|
|
|
@property
|
2024-12-01 07:42:35 +00:00
|
|
|
def primary_key(self) -> str:
|
|
|
|
return str(self.user_id)
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def primary_key_name(self) -> str:
|
2024-12-09 01:07:26 +00:00
|
|
|
return "user_id"
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
@property
|
2024-12-01 07:42:35 +00:00
|
|
|
def key(self) -> Dict[str, str]:
|
2024-12-09 01:07:26 +00:00
|
|
|
return {self.primary_key_name: self.primary_key}
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
def _subscribed_alerts_as_trinity_list(self) -> List[str]:
|
2024-12-09 01:07:26 +00:00
|
|
|
return (
|
|
|
|
[str(alert) for alert in self.subscribed_alerts]
|
|
|
|
if self.subscribed_alerts
|
|
|
|
else []
|
|
|
|
)
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
async def _lazy_load(self, table: Table, consistent: bool = False) -> None:
|
|
|
|
if consistent or not self._loaded:
|
|
|
|
await self.get(table, consistent=consistent)
|
|
|
|
|
|
|
|
async def put(self, table: Table) -> None:
|
|
|
|
await table.put_item(
|
|
|
|
item={
|
|
|
|
self.primary_key_name: self.primary_key,
|
2024-12-09 01:07:26 +00:00
|
|
|
"region": self.region,
|
|
|
|
"subscribed_alerts": self._subscribed_alerts_as_trinity_list(),
|
2024-11-30 11:27:32 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
async def delete(self, table: Table) -> None:
|
|
|
|
if not self._loaded:
|
|
|
|
await self._lazy_load(table, consistent=True)
|
|
|
|
await table.delete_item(
|
|
|
|
key={self.primary_key_name: self.primary_key},
|
|
|
|
)
|
|
|
|
|
|
|
|
async def get(self, table: Table, consistent: bool = False) -> bool:
|
|
|
|
try:
|
2024-12-09 01:07:26 +00:00
|
|
|
response = await table.get_item(key=self.key, consistent_read=consistent)
|
2024-11-30 11:27:32 +00:00
|
|
|
except ItemNotFound:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.subscribed_alerts = []
|
2024-12-09 01:07:26 +00:00
|
|
|
for string_trinity in response["subscribed_alerts"]:
|
2024-11-30 11:27:32 +00:00
|
|
|
self.subscribed_alerts.append(pdb.Alert.from_str(string_trinity))
|
2024-12-09 01:07:26 +00:00
|
|
|
self.region = Region(response["region"])
|
2024-11-30 11:27:32 +00:00
|
|
|
return True
|
2024-12-04 00:59:13 +00:00
|
|
|
|
2024-12-09 01:07:26 +00:00
|
|
|
async def add_alert(
|
|
|
|
self, table: Table, alert: "pdb.Alert", consistent: bool = False
|
|
|
|
) -> None:
|
2024-12-04 00:59:13 +00:00
|
|
|
await self._lazy_load(table, consistent=consistent)
|
|
|
|
if alert not in self.subscribed_alerts:
|
|
|
|
self.subscribed_alerts.append(alert)
|
|
|
|
await self.put(table)
|
|
|
|
|
2024-12-09 01:07:26 +00:00
|
|
|
async def remove_alert(
|
|
|
|
self, table: Table, alert: "pdb.Alert", consistent: bool = True
|
|
|
|
) -> None:
|
2024-12-04 00:59:13 +00:00
|
|
|
await self._lazy_load(table, consistent=consistent)
|
|
|
|
if alert in self.subscribed_alerts:
|
|
|
|
self.subscribed_alerts.remove(alert)
|
|
|
|
await self.put(table)
|