More refactoring

This commit is contained in:
2025-05-14 23:27:50 +02:00
parent ebd8529c41
commit 4a3d2746fb
3 changed files with 25 additions and 10 deletions

6
bot.py
View File

@@ -1,5 +1,6 @@
from telegram import Update from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes
from datetime import datetime
from config import BotConfig from config import BotConfig
from gates import Gates from gates import Gates
from users import Users, Credential, Role from users import Users, Credential, Role
@@ -10,11 +11,10 @@ gates = Gates()
users = Users() users = Users()
async def post_init(application: Application) -> None: async def post_init(application: Application) -> None:
bot_commands = bot_config.commands
await application.bot.set_my_commands(bot_commands)
await application.bot.set_my_name(bot_config.name) await application.bot.set_my_name(bot_config.name)
await application.bot.set_my_description(bot_config.description) await application.bot.set_my_description(bot_config.description)
await application.bot.set_my_short_description(bot_config.short_description) await application.bot.set_my_short_description(bot_config.short_description)
await application.bot.set_my_commands(bot_config.commands)
async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE): async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = str(update.effective_user.id) user_id = str(update.effective_user.id)
@@ -94,7 +94,7 @@ async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = context.args[0] user_id = context.args[0]
gate = context.args[1] gate = context.args[1]
expires_at = context.args[2] expires_at = context.args[2]
users.grant_access(user_id, gate, expires_at, grantor_id=approver_id) users.grant_access(user_id, gate, datetime.fromisoformat(expires_at.replace("Z", "+00:00")), grantor_id=approver_id)
await update.message.reply_text(f"Access to {gate} granted to user {user_id}.") await update.message.reply_text(f"Access to {gate} granted to user {user_id}.")
try: try:
await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}") await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}")

View File

@@ -30,7 +30,7 @@ class Gates:
self._json_path: str = json_path self._json_path: str = json_path
self._gates: dict[str, _Gate] = self._load_gates() self._gates: dict[str, _Gate] = self._load_gates()
def _load_gates(self): def _load_gates(self) -> dict[str, _Gate]:
try: try:
with open(self._json_path, "r") as file: with open(self._json_path, "r") as file:
gates_data = json.load(file) gates_data = json.load(file)

View File

@@ -9,7 +9,7 @@ class Role(Enum):
GUEST = "guest" GUEST = "guest"
class _Grant: class _Grant:
def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: int | Status = Status.ENABLED): def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: Status = Status.ENABLED):
self._grantor: str = grantor self._grantor: str = grantor
self._granted_at: datetime = granted_at self._granted_at: datetime = granted_at
self._expires_at = expires_at self._expires_at = expires_at
@@ -50,14 +50,14 @@ class _Grant:
return self._status return self._status
class _User: class _User:
def __init__(self, id: str, username: str, fullname: str, role: str | Role = Role.GUEST, credentials: dict = None, grants: dict = None, status: int | Status = Status.ENABLED): def __init__(self, id: str, username: str, fullname: str, role: Role = Role.GUEST, credentials: Credential = Credential("", ""), grants: dict = None, status: Status = Status.ENABLED):
self._id: str = id self._id: str = id
self._username: str = username self._username: str = username
self._fullname: str = fullname self._fullname: str = fullname
self._role: Role = role if isinstance(role, Role) else Role(role) self._role: Role = role if isinstance(role, Role) else Role(role)
self._credentials: Credential = Credential(**credentials) self._credentials: Credential = credentials
self._grants: dict[str, _Grant] = {gate: _Grant(**grant) for gate, grant in grants.items()} if grants else {} self._grants: dict[str, _Grant] = {gate:_Grant(grant.get("grantor", ""), datetime.fromisoformat(grant.get("expires_at").replace("Z", "+00:00"))) for gate, grant in grants.items()} if grants else {}
self._status: Status = status if isinstance(status, Status) else Status(status) self._status: Status = status
def __dict__(self): def __dict__(self):
return { return {
@@ -133,7 +133,22 @@ class Users:
def _load_users(self) -> dict[str, _User]: def _load_users(self) -> dict[str, _User]:
try: try:
with open(self._json_path, "r") as f: with open(self._json_path, "r") as f:
return {uid: _User(**info) for uid, info in json.load(f)} users = {}
for uid, info in json.load(f).items():
try:
user = _User(
id=uid,
username=info.get("username", ""),
fullname=info.get("fullname", ""),
role=info.get("role", Role.GUEST),
credentials=Credential(info.get("credentials", {}).get("username", ""), info.get("credentials", {}).get("password", "")),
grants={gate: info for gate, info in info.get("grants", {}).items()},
status=info.get("status", Status.ENABLED)
)
users[uid] = user
except:
continue
return users
except Exception: except Exception:
return {} return {}