refactor: comprehensive codebase review — security, performance, quality, UX

Security:
- Fix NUT protocol command injection (validate names against safe regex)
- Enable Jinja2 autoescape=True to prevent HTML injection via external data
- Add WebhookProviderConfig validation model

Performance:
- Shared aiohttp.ClientSession singleton (replaces 40+ per-request sessions)
- Fix 4 N+1 queries with batch IN loads (poller, scheduler, memory, broadcast)
- asyncio.gather for Gitea commands and notification dispatcher
- Add DB indexes on NotificationTrackerState.tracker_id, CommandTrackerListener
- LRU cache for compiled Jinja2 templates
- Daily EventLog cleanup job (90-day retention)
- 30s HTTP timeout on all external calls
- GROUP BY for target type counts (replaces 7 sequential queries)

Code quality:
- Extract get_owned_entity() helper (replaces 11 duplicate functions)
- Extract slot_helpers.py (load_slots, save_slots, render_template_preview)
- Extract command_utils.py (tracker lookup, last event, collection IDs)
- Extract http_session.py (shared session lifecycle)
- Provider connection validation dedup (3x → 1 helper)
- Command dispatch tables replacing if/elif chains
- Album+links fetch helper (fetch_albums_with_links)
- Provider dispatch polymorphism (list_provider_collections)
- Immutable _enrich_assets (no longer mutates in-place)
- Fix _format_assets return type + handler unpacking

Frontend:
- Fix 18+ hardcoded English strings → t() with new i18n keys (en + ru)
- Mobile "More" nav panel with provider filter and search
- Shared Button.svelte component (4 variants, 2 sizes)
- Shared ErrorBanner.svelte component (8 pages updated)
- SvelteKit goto() replacing window.location.href
- Dashboard grid fixed for 4 cards, paginator opacity consistency

Functionality:
- max_instances=1 on scheduler jobs (prevents duplicate events)
- Webhook provider in watcher (prevents error spam)
- Fix stale SQLModel reference in poller
- Gitea get_repo() direct API call
This commit is contained in:
2026-03-28 13:22:26 +03:00
parent 616b221c92
commit b803d004e1
65 changed files with 1934 additions and 1498 deletions
@@ -13,7 +13,6 @@ from __future__ import annotations
import logging
from typing import Any
import aiohttp
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -47,10 +46,18 @@ async def _get_bot_ids_with_active_listeners() -> set[int]:
listeners = result.all()
active_bot_ids: set[int] = set()
for listener in listeners:
tracker = await session.get(CommandTracker, listener.command_tracker_id)
if tracker and tracker.enabled:
active_bot_ids.add(listener.listener_id)
tracker_ids = list({l.command_tracker_id for l in listeners})
if tracker_ids:
tracker_result = await session.exec(
select(CommandTracker).where(
CommandTracker.id.in_(tracker_ids),
CommandTracker.enabled == True, # noqa: E712
)
)
enabled_tracker_ids = {t.id for t in tracker_result.all()}
for listener in listeners:
if listener.command_tracker_id in enabled_tracker_ids:
active_bot_ids.add(listener.listener_id)
return active_bot_ids
@@ -145,21 +152,23 @@ async def _poll_bot(bot_id: int) -> None:
if not bot or bot.update_mode != "polling":
unschedule_bot_polling(bot_id)
return
# Extract what we need before closing session
# Copy attributes before session closes to avoid detached-instance errors
from types import SimpleNamespace
bot_token = bot.token
bot_obj = bot
bot_obj = SimpleNamespace(id=bot.id, name=bot.name, token=bot.token)
offset = _last_update_id.get(bot_id, 0)
try:
async with aiohttp.ClientSession() as http:
client = TelegramClient(http, bot_token)
result = await client.get_updates(
offset=offset + 1 if offset else None, limit=50,
)
if not result.get("success"):
return
updates = result.get("result", [])
from .http_session import get_http_session
http = await get_http_session()
client = TelegramClient(http, bot_token)
result = await client.get_updates(
offset=offset + 1 if offset else None, limit=50,
)
if not result.get("success"):
return
updates = result.get("result", [])
except Exception as e:
_LOGGER.debug("Polling error for bot %d: %s", bot_id, e)
return
@@ -209,17 +218,13 @@ async def _poll_bot(bot_id: int) -> None:
continue
effective_lang = chat_row.language_override or msg_language
message_id = message.get("message_id")
cmd_response = await handle_command(bot_obj, chat_id, text, language_code=effective_lang)
if cmd_response is not None:
if isinstance(cmd_response, dict) and "media" in cmd_response:
# Text + media: send text first, media as reply
from ..commands.handler import send_reply as _reply
await _reply(bot_token, chat_id, cmd_response["text"], reply_to_message_id=message_id)
await send_media_group(bot_token, chat_id, cmd_response["media"], reply_to_message_id=message_id)
elif isinstance(cmd_response, list):
await send_media_group(bot_token, chat_id, cmd_response, reply_to_message_id=message_id)
else:
await send_reply(bot_token, chat_id, cmd_response, reply_to_message_id=message_id)
responses = await handle_command(bot_obj, chat_id, text, language_code=effective_lang)
if responses:
for resp in responses:
if resp.text:
await send_reply(bot_token, chat_id, resp.text, reply_to_message_id=message_id)
if resp.media:
await send_media_group(bot_token, chat_id, resp.media, reply_to_message_id=message_id)
except Exception:
_LOGGER.error("Error handling command from bot %d", bot_id, exc_info=True)