aboutsummaryrefslogtreecommitdiff
path: root/libs/user.py
diff options
context:
space:
mode:
Diffstat (limited to 'libs/user.py')
-rw-r--r--libs/user.py30
1 files changed, 27 insertions, 3 deletions
diff --git a/libs/user.py b/libs/user.py
index b201ce9..d145da6 100644
--- a/libs/user.py
+++ b/libs/user.py
@@ -1,5 +1,29 @@
1from aiogram.types import Chat, User 1from aiogram import Bot
2 2
3from models import UserCache
4from shared import redis_users
3 5
4def mention(user: User | Chat) -> str: 6
5 return f'<a href="tg://user?id={user.id}">{user.full_name}</a>' 7async def set_user_cache(user_cache: UserCache) -> None:
8 await redis_users.set(
9 str(user_cache.id),
10 user_cache.model_dump_json(
11 exclude_defaults=True,
12 exclude_none=True,
13 ),
14 )
15
16
17async def get_user_cache(user_id: int) -> UserCache | None:
18 user_cache = await redis_users.get(str(user_id))
19 if user_cache is None:
20 return None
21 return UserCache.model_validate_json(user_cache)
22
23
24async def load_user_cache(bot: Bot, user_id: int) -> UserCache:
25 user_cache = await get_user_cache(user_id)
26 if user_cache is None:
27 user_cache = UserCache.from_chat(await bot.get_chat(user_id))
28 await set_user_cache(user_cache)
29 return user_cache