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
@@ -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})
@@ -2,10 +2,12 @@
from __future__ import annotations
import asyncio
import logging
from typing import Any
from ...services import make_immich_provider
from notify_bridge_core.providers.immich.asset_utils import get_public_url
from ..handler import _render_cmd_template
_LOGGER = logging.getLogger(__name__)
@@ -17,6 +19,53 @@ _IMMICH_COMMANDS = {
}
async def fetch_albums_with_links(
client: Any,
album_ids: list[str],
ext_domain: str,
*,
include_failed: bool = True,
) -> list[dict[str, Any]]:
"""Fetch albums and their shared links concurrently.
Returns a list of album data dicts with keys: name, asset_count, id,
public_url, and ``_album`` (the raw album object for callers that need
asset-level access).
When *include_failed* is True, albums that fail to fetch are included
with placeholder data (``"?"`` for counts). When False, they are
silently skipped.
"""
album_results = await asyncio.gather(
*[client.get_album(aid) for aid in album_ids],
return_exceptions=True,
)
link_results = await asyncio.gather(
*[client.get_shared_links(aid) for aid in album_ids],
return_exceptions=True,
)
albums_data: list[dict[str, Any]] = []
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)
if include_failed:
albums_data.append({
"name": f"{album_id[:8]}...", "asset_count": "?",
"id": album_id, "public_url": "", "_album": None,
})
continue
if 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, "_album": result,
})
return albums_data
def build_asset_dict(
asset: Any,
*,
@@ -56,8 +105,14 @@ def _format_assets(
assets: list[dict[str, Any]], cmd: str, query: str,
locale: str, response_mode: str, client: Any,
cmd_templates: dict[str, dict[str, str]],
) -> str | list[dict[str, Any]]:
"""Format asset results as text or media payload."""
) -> str | dict[str, Any]:
"""Format asset results as text or a text-plus-media payload.
Returns:
str: rendered text when *response_mode* is ``"text"`` (or no assets).
dict: ``{"text": ..., "media": [...]}`` when *response_mode* is
``"media"`` and assets are present.
"""
if not assets:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": cmd, "query": query})
@@ -68,7 +123,7 @@ def _format_assets(
})
if response_mode == "media":
media_items = []
media_items: list[dict[str, Any]] = []
for asset in assets:
asset_id = asset.get("id", "")
media_items.append({
@@ -13,23 +13,22 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from ...database.engine import get_engine
from ...database.models import (
EventLog, NotificationTarget, NotificationTrackerTarget,
ServiceProvider, TelegramBot, TrackingConfig,
EventLog, NotificationTracker, NotificationTrackerTarget,
ServiceProvider, TrackingConfig,
)
from notify_bridge_core.providers.immich.asset_utils import get_public_url
from ..handler import _get_notification_trackers_for_providers, _render_cmd_template
from .common import _format_assets, build_asset_dict
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_events(
bot: TelegramBot, providers_map: dict[int, ServiceProvider],
provider: ServiceProvider,
count: int, 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)
tracker_ids = [t.id for t in trackers]
if not tracker_ids:
return {"events": []}
@@ -57,32 +56,21 @@ async def cmd_latest(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
external_domain: str = "",
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /latest command with concurrent album fetching."""
album_ids = all_album_ids[:10]
if not album_ids:
return _format_assets([], "latest", "", locale, response_mode, client, cmd_templates)
album_results = await asyncio.gather(
*[client.get_album(aid) for aid in album_ids],
return_exceptions=True,
)
link_results = await asyncio.gather(
*[client.get_shared_links(aid) for aid in album_ids],
return_exceptions=True,
)
ext = external_domain.rstrip("/")
fetched = await fetch_albums_with_links(client, album_ids, ext, include_failed=False)
latest_assets: list[dict[str, Any]] = []
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)
continue
if result:
pub_url = ""
if not isinstance(links, Exception) and ext:
pub_url = get_public_url(ext, links) or ""
for aid, asset in list(result.assets.items())[:count]:
for album_data in fetched:
pub_url = album_data.get("public_url", "")
album_obj = album_data.get("_album")
if album_obj:
for aid, asset in list(album_obj.assets.items())[:count]:
asset_pub = f"{pub_url}/photos/{asset.id}" if pub_url else ""
latest_assets.append(build_asset_dict(asset, public_url=asset_pub))
@@ -95,32 +83,21 @@ async def cmd_random(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
external_domain: str = "",
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /random command with concurrent album fetching."""
album_ids = all_album_ids[:10]
if not album_ids:
return _format_assets([], "random", "", locale, response_mode, client, cmd_templates)
album_results = await asyncio.gather(
*[client.get_album(aid) for aid in album_ids],
return_exceptions=True,
)
link_results = await asyncio.gather(
*[client.get_shared_links(aid) for aid in album_ids],
return_exceptions=True,
)
ext = external_domain.rstrip("/")
fetched = await fetch_albums_with_links(client, album_ids, ext, include_failed=False)
random_assets: list[dict[str, Any]] = []
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)
continue
if result:
pub_url = ""
if not isinstance(links, Exception) and ext:
pub_url = get_public_url(ext, links) or ""
asset_list = list(result.assets.values())
for album_data in fetched:
pub_url = album_data.get("public_url", "")
album_obj = album_data.get("_album")
if album_obj:
asset_list = list(album_obj.assets.values())
sampled = rng.sample(asset_list, min(count, len(asset_list)))
for asset in sampled:
asset_pub = f"{pub_url}/photos/{asset.id}" if pub_url else ""
@@ -130,40 +107,40 @@ async def cmd_random(
return _format_assets(random_assets[:count], "random", "", locale, response_mode, client, cmd_templates)
async def _check_native_memory(bot: TelegramBot) -> bool:
"""Check if any tracker-target linked to this bot uses native memory source."""
async def _check_native_memory(provider_id: int) -> bool:
"""Check if any notification tracker for this provider uses native memory source."""
engine = get_engine()
async with AsyncSession(engine) as session:
result = await session.exec(
select(NotificationTarget).where(
NotificationTarget.type == "telegram",
NotificationTarget.user_id == bot.user_id,
tracker_result = await session.exec(
select(NotificationTracker).where(
NotificationTracker.provider_id == provider_id,
)
)
targets = result.all()
bot_target_ids = {t.id for t in targets if t.config.get("bot_token") == bot.token}
if not bot_target_ids:
trackers = tracker_result.all()
tracker_ids = [t.id for t in trackers]
if not tracker_ids:
return False
tt_result = await session.exec(
select(NotificationTrackerTarget).where(
NotificationTrackerTarget.target_id.in_(bot_target_ids)
NotificationTrackerTarget.tracker_id.in_(tracker_ids)
)
)
for tt in tt_result.all():
if tt.tracking_config_id:
tc = await session.get(TrackingConfig, tt.tracking_config_id)
if tc and tc.memory_source == "native":
return True
return False
tc_ids = list({tt.tracking_config_id for tt in tt_result.all() if tt.tracking_config_id})
if not tc_ids:
return False
tc_result = await session.exec(
select(TrackingConfig).where(TrackingConfig.id.in_(tc_ids))
)
return any(tc.memory_source == "native" for tc in tc_result.all())
async def cmd_memory(
bot: TelegramBot, client: Any, all_album_ids: list[str], count: int,
provider_id: int, client: Any, all_album_ids: list[str], count: int,
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /memory command with concurrent album fetching."""
use_native = await _check_native_memory(bot)
use_native = await _check_native_memory(provider_id)
today = datetime.now(timezone.utc)
memory_assets: list[dict[str, Any]] = []
@@ -2,26 +2,21 @@
from __future__ import annotations
import asyncio
import logging
from typing import Any
import aiohttp
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
from ...database.engine import get_engine
from ...database.models import (
CommandConfig, CommandTracker, EventLog,
CommandConfig, CommandTracker,
ServiceProvider, TelegramBot,
)
from ...services import make_immich_provider
from ..base import ProviderCommandHandler
from ..handler import _get_notification_trackers_for_providers, _render_cmd_template
from notify_bridge_core.providers.immich.asset_utils import get_public_url
from ...services.http_session import get_http_session
from ..base import CommandResponse, ProviderCommandHandler
from ..command_utils import get_last_event_str, get_trackers_for_provider
from ..handler import _render_cmd_template
from .albums import _cmd_albums, cmd_favorites, cmd_summary
from .common import _IMMICH_COMMANDS
from .common import _IMMICH_COMMANDS, fetch_albums_with_links
from .events import _cmd_events, cmd_latest, cmd_memory, cmd_random
from .search import cmd_find, cmd_person, cmd_place, cmd_search
@@ -29,21 +24,15 @@ _LOGGER = logging.getLogger(__name__)
async def _cmd_status(
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)
active = sum(1 for t in trackers if t.enabled)
total = len(trackers)
total_albums = sum(len(t.collection_ids or []) for t in trackers)
engine = get_engine()
async with AsyncSession(engine) as session:
result = await session.exec(
select(EventLog).order_by(EventLog.created_at.desc()).limit(1)
)
last_event = result.first()
last_str = last_event.created_at.strftime("%Y-%m-%d %H:%M") if last_event else "-"
tracker_ids = [t.id for t in trackers]
last_str = await get_last_event_str(tracker_ids)
return {
"trackers_active": active, "trackers_total": total,
@@ -52,16 +41,13 @@ async def _cmd_status(
async def _cmd_people(
providers_map: dict[int, ServiceProvider], locale: str,
provider: ServiceProvider, locale: str,
) -> dict[str, Any]:
all_people: dict[str, str] = {}
async with aiohttp.ClientSession() as http:
for provider in providers_map.values():
if provider.type != "immich":
continue
immich = make_immich_provider(http, provider)
people = await immich.client.get_people()
all_people.update(people)
http = await get_http_session()
immich = make_immich_provider(http, provider)
people = await immich.client.get_people()
all_people.update(people)
names = sorted(all_people.values())
return {"people": names}
@@ -87,106 +73,92 @@ class ImmichCommandHandler(ProviderCommandHandler):
count: int,
locale: str,
response_mode: str,
providers_map: dict[int, ServiceProvider],
provider: ServiceProvider,
cmd_templates: dict[str, dict[str, str]],
bot: TelegramBot,
ctx_tuples: list[tuple[CommandTracker, CommandConfig, ServiceProvider]],
) -> str | list[dict[str, Any]] | None:
tracker: CommandTracker,
config: CommandConfig,
) -> CommandResponse | None:
if cmd == "status":
ctx = await _cmd_status(bot, providers_map, locale)
return _render_cmd_template(cmd_templates, "status", locale, ctx)
ctx = await _cmd_status(provider, locale)
return CommandResponse(text=_render_cmd_template(cmd_templates, "status", locale, ctx))
if cmd == "albums":
ctx = await _cmd_albums(bot, providers_map, locale)
return _render_cmd_template(cmd_templates, "albums", locale, ctx)
ctx = await _cmd_albums(provider, locale)
return CommandResponse(text=_render_cmd_template(cmd_templates, "albums", locale, ctx))
if cmd == "events":
ctx = await _cmd_events(bot, providers_map, count, locale)
return _render_cmd_template(cmd_templates, "events", locale, ctx)
ctx = await _cmd_events(provider, count, locale)
return CommandResponse(text=_render_cmd_template(cmd_templates, "events", locale, ctx))
if cmd == "people":
ctx = await _cmd_people(providers_map, locale)
return _render_cmd_template(cmd_templates, "people", locale, ctx)
ctx = await _cmd_people(provider, locale)
return CommandResponse(text=_render_cmd_template(cmd_templates, "people", locale, ctx))
if cmd in ("search", "find", "person", "place", "latest",
"random", "favorites", "summary", "memory"):
return await _cmd_immich(
bot, cmd, args, count, locale, response_mode,
providers_map, cmd_templates,
cmd, args, count, locale, response_mode,
provider, cmd_templates,
)
return None
async def _cmd_immich(
bot: TelegramBot, cmd: str, args: str, count: int, locale: str,
response_mode: str, providers_map: dict[int, ServiceProvider],
cmd: str, args: str, count: int, locale: str,
response_mode: str, provider: ServiceProvider,
cmd_templates: dict[str, dict[str, str]],
) -> str | list[dict[str, Any]]:
) -> CommandResponse | None:
"""Handle commands that need Immich API access and may return media."""
if not providers_map:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": cmd, "query": args})
provider_ids = set(providers_map.keys())
notification_trackers = await _get_notification_trackers_for_providers(provider_ids)
notification_trackers = await get_trackers_for_provider(provider.id)
all_album_ids: list[str] = []
for t in notification_trackers:
all_album_ids.extend(t.collection_ids or [])
provider: ServiceProvider | None = None
for p in providers_map.values():
if p.type == "immich":
provider = p
break
if not provider:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": cmd, "query": args})
ext_domain = (provider.config.get("external_domain") or provider.config.get("url", "")).rstrip("/")
async with aiohttp.ClientSession() as http:
immich = make_immich_provider(http, provider)
client = immich.client
http = await get_http_session()
immich = make_immich_provider(http, provider)
client = immich.client
# Build asset_id → public_url map from tracked albums' shared links
asset_public_urls: dict[str, str] = {}
if ext_domain and all_album_ids and cmd in ("search", "find", "person", "place", "favorites"):
link_results = await asyncio.gather(
*[client.get_shared_links(aid) for aid in all_album_ids],
return_exceptions=True,
)
album_results = await asyncio.gather(
*[client.get_album(aid) for aid in all_album_ids],
return_exceptions=True,
)
for album_id, links, album in zip(all_album_ids, link_results, album_results):
if isinstance(links, Exception) or isinstance(album, Exception):
continue
pub_url = get_public_url(ext_domain, links)
if pub_url and album:
for asset_id in album.assets:
asset_public_urls[asset_id] = f"{pub_url}/photos/{asset_id}"
# Build asset_id → public_url map from tracked albums' shared links
asset_public_urls: dict[str, str] = {}
if ext_domain and all_album_ids and cmd in ("search", "find", "person", "place", "favorites"):
fetched = await fetch_albums_with_links(client, all_album_ids, ext_domain, include_failed=False)
for album_data in fetched:
pub_url = album_data.get("public_url", "")
album_obj = album_data.get("_album")
if pub_url and album_obj:
for asset_id in album_obj.assets:
asset_public_urls[asset_id] = f"{pub_url}/photos/{asset_id}"
if cmd == "search":
return await cmd_search(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
# Wrap single-provider in a map for functions that still expect it
providers_map = {provider.id: provider}
if cmd == "find":
return await cmd_find(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
result: str | dict[str, Any] | None = None
if cmd == "person":
return await cmd_person(client, args, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
if cmd == "search":
result = await cmd_search(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
elif cmd == "find":
result = await cmd_find(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
elif cmd == "person":
result = await cmd_person(client, args, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
elif cmd == "place":
result = await cmd_place(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
elif cmd == "favorites":
result = await cmd_favorites(providers_map, all_album_ids, count, locale, response_mode, client, cmd_templates)
elif cmd == "latest":
result = await cmd_latest(client, all_album_ids, count, locale, response_mode, cmd_templates, external_domain=ext_domain)
elif cmd == "random":
result = await cmd_random(client, all_album_ids, count, locale, response_mode, cmd_templates, external_domain=ext_domain)
elif cmd == "summary":
result = await cmd_summary(client, all_album_ids, locale, cmd_templates, external_domain=ext_domain)
elif cmd == "memory":
result = await cmd_memory(provider.id, client, all_album_ids, count, locale, response_mode, cmd_templates)
if cmd == "place":
return await cmd_place(client, args, all_album_ids, count, locale, response_mode, cmd_templates, asset_public_urls=asset_public_urls)
if cmd == "favorites":
return await cmd_favorites(bot, providers_map, all_album_ids, count, locale, response_mode, client, cmd_templates)
if cmd == "latest":
return await cmd_latest(client, all_album_ids, count, locale, response_mode, cmd_templates, external_domain=ext_domain)
if cmd == "random":
return await cmd_random(client, all_album_ids, count, locale, response_mode, cmd_templates, external_domain=ext_domain)
if cmd == "summary":
return await cmd_summary(client, all_album_ids, locale, cmd_templates, external_domain=ext_domain)
if cmd == "memory":
return await cmd_memory(bot, client, all_album_ids, count, locale, response_mode, cmd_templates)
return None
if result is None:
return None
# _format_assets returns {"text": ..., "media": [...]} for media mode
if isinstance(result, dict):
return CommandResponse(
text=result.get("text"),
media=result.get("media", []),
)
return CommandResponse(text=result)
@@ -9,14 +9,15 @@ from .common import _format_assets
def _enrich_assets(assets: list[dict[str, Any]], asset_public_urls: dict[str, str]) -> list[dict[str, Any]]:
"""Add public_url to assets from the pre-built map."""
"""Add public_url to assets from the pre-built map. Returns new list without mutating inputs."""
if not asset_public_urls:
return assets
for asset in assets:
aid = asset.get("id", "")
if aid and aid in asset_public_urls and not asset.get("public_url"):
asset["public_url"] = asset_public_urls[aid]
return assets
return [
{**asset, "public_url": asset_public_urls.get(asset.get("id", ""), "")}
if asset.get("id", "") in asset_public_urls and not asset.get("public_url")
else asset
for asset in assets
]
async def cmd_search(
@@ -24,7 +25,7 @@ async def cmd_search(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
asset_public_urls: dict[str, str] | None = None,
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /search command."""
if not args:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "search", "query": ""})
@@ -38,7 +39,7 @@ async def cmd_find(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
asset_public_urls: dict[str, str] | None = None,
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /find command."""
if not args:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "find", "query": ""})
@@ -52,7 +53,7 @@ async def cmd_person(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
asset_public_urls: dict[str, str] | None = None,
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /person command."""
if not args:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "person", "query": ""})
@@ -74,7 +75,7 @@ async def cmd_place(
locale: str, response_mode: str,
cmd_templates: dict[str, dict[str, str]],
asset_public_urls: dict[str, str] | None = None,
) -> str | list[dict[str, Any]]:
) -> str | dict[str, Any]:
"""Handle /place command."""
if not args:
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "place", "query": ""})