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:
@@ -6,70 +6,48 @@ import asyncio
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
from notify_bridge_core.providers.immich.asset_utils import get_public_url
|
||||
|
||||
from ...database.models import ServiceProvider, TelegramBot
|
||||
from ...database.models import ServiceProvider
|
||||
from ...services import make_immich_provider
|
||||
from ..handler import _get_notification_trackers_for_providers, _render_cmd_template
|
||||
from .common import _format_assets, build_asset_dict
|
||||
from ...services.http_session import get_http_session
|
||||
from ..command_utils import get_trackers_for_provider
|
||||
from ..handler import _render_cmd_template
|
||||
from .common import _format_assets, build_asset_dict, fetch_albums_with_links
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _cmd_albums(
|
||||
bot: TelegramBot, providers_map: dict[int, ServiceProvider], locale: str,
|
||||
provider: ServiceProvider, locale: str,
|
||||
) -> dict[str, Any]:
|
||||
provider_ids = set(providers_map.keys())
|
||||
trackers = await _get_notification_trackers_for_providers(provider_ids)
|
||||
trackers = await get_trackers_for_provider(provider.id)
|
||||
if not trackers:
|
||||
return {"albums": []}
|
||||
|
||||
albums_data: list[dict] = []
|
||||
async with aiohttp.ClientSession() as http:
|
||||
for tracker in trackers:
|
||||
provider = providers_map.get(tracker.provider_id)
|
||||
if not provider or provider.type != "immich":
|
||||
continue
|
||||
immich = make_immich_provider(http, provider)
|
||||
album_ids = tracker.collection_ids or []
|
||||
if not album_ids:
|
||||
continue
|
||||
# Deduplicate album IDs while preserving order
|
||||
seen: set[str] = set()
|
||||
album_ids: list[str] = []
|
||||
for tracker in trackers:
|
||||
for aid in tracker.collection_ids or []:
|
||||
if aid not in seen:
|
||||
seen.add(aid)
|
||||
album_ids.append(aid)
|
||||
if not album_ids:
|
||||
return {"albums": []}
|
||||
|
||||
ext_domain = (provider.config.get("external_domain") or provider.config.get("url", "")).rstrip("/")
|
||||
album_results = await asyncio.gather(
|
||||
*[immich.client.get_album(aid) for aid in album_ids],
|
||||
return_exceptions=True,
|
||||
)
|
||||
link_results = await asyncio.gather(
|
||||
*[immich.client.get_shared_links(aid) for aid in album_ids],
|
||||
return_exceptions=True,
|
||||
)
|
||||
for album_id, result, links in zip(album_ids, album_results, link_results):
|
||||
if isinstance(result, Exception):
|
||||
_LOGGER.warning("Failed to fetch album %s: %s", album_id, result)
|
||||
albums_data.append({
|
||||
"name": f"{album_id[:8]}...", "asset_count": "?", "id": album_id,
|
||||
})
|
||||
elif result:
|
||||
pub_url = ""
|
||||
if not isinstance(links, Exception) and ext_domain:
|
||||
pub_url = get_public_url(ext_domain, links) or ""
|
||||
albums_data.append({
|
||||
"name": result.name, "asset_count": result.asset_count,
|
||||
"id": album_id, "public_url": pub_url,
|
||||
})
|
||||
ext_domain = (provider.config.get("external_domain") or provider.config.get("url", "")).rstrip("/")
|
||||
http = await get_http_session()
|
||||
immich = make_immich_provider(http, provider)
|
||||
albums_data = await fetch_albums_with_links(immich.client, album_ids, ext_domain)
|
||||
|
||||
return {"albums": albums_data}
|
||||
|
||||
|
||||
async def cmd_favorites(
|
||||
bot: TelegramBot, providers_map: dict[int, ServiceProvider],
|
||||
providers_map: dict[int, ServiceProvider],
|
||||
all_album_ids: list[str], count: int, locale: str,
|
||||
response_mode: str, client: Any,
|
||||
cmd_templates: dict[str, dict[str, str]],
|
||||
) -> str | list[dict[str, Any]]:
|
||||
) -> str | dict[str, Any]:
|
||||
"""Handle /favorites command with concurrent album fetching."""
|
||||
album_ids = all_album_ids[:10]
|
||||
if not album_ids:
|
||||
@@ -104,28 +82,6 @@ async def cmd_summary(
|
||||
if not all_album_ids:
|
||||
return _render_cmd_template(cmd_templates, "summary", locale, {"albums": []})
|
||||
|
||||
album_results = await asyncio.gather(
|
||||
*[client.get_album(aid) for aid in all_album_ids],
|
||||
return_exceptions=True,
|
||||
)
|
||||
link_results = await asyncio.gather(
|
||||
*[client.get_shared_links(aid) for aid in all_album_ids],
|
||||
return_exceptions=True,
|
||||
)
|
||||
ext = external_domain.rstrip("/")
|
||||
|
||||
albums_data: list[dict] = []
|
||||
for album_id, result, links in zip(all_album_ids, album_results, link_results):
|
||||
if isinstance(result, Exception):
|
||||
_LOGGER.warning("Failed to fetch album %s: %s", album_id, result)
|
||||
continue
|
||||
if result:
|
||||
pub_url = ""
|
||||
if not isinstance(links, Exception) and ext:
|
||||
pub_url = get_public_url(ext, links) or ""
|
||||
albums_data.append({
|
||||
"name": result.name, "asset_count": result.asset_count,
|
||||
"id": album_id, "public_url": pub_url,
|
||||
})
|
||||
|
||||
albums_data = await fetch_albums_with_links(client, all_album_ids, ext, include_failed=False)
|
||||
return _render_cmd_template(cmd_templates, "summary", locale, {"albums": albums_data})
|
||||
|
||||
Reference in New Issue
Block a user