b803d004e1
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
87 lines
3.5 KiB
Python
87 lines
3.5 KiB
Python
"""Search-related Immich bot commands: search, find, person, place."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from ..handler import _render_cmd_template
|
|
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. Returns new list without mutating inputs."""
|
|
if not asset_public_urls:
|
|
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(
|
|
client: Any, args: str, all_album_ids: list[str], count: int,
|
|
locale: str, response_mode: str,
|
|
cmd_templates: dict[str, dict[str, str]],
|
|
asset_public_urls: dict[str, str] | None = None,
|
|
) -> str | dict[str, Any]:
|
|
"""Handle /search command."""
|
|
if not args:
|
|
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "search", "query": ""})
|
|
assets = await client.search_smart(args, album_ids=all_album_ids, limit=count)
|
|
_enrich_assets(assets, asset_public_urls or {})
|
|
return _format_assets(assets, "search", args, locale, response_mode, client, cmd_templates)
|
|
|
|
|
|
async def cmd_find(
|
|
client: Any, args: str, all_album_ids: list[str], count: int,
|
|
locale: str, response_mode: str,
|
|
cmd_templates: dict[str, dict[str, str]],
|
|
asset_public_urls: dict[str, str] | None = None,
|
|
) -> str | dict[str, Any]:
|
|
"""Handle /find command."""
|
|
if not args:
|
|
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "find", "query": ""})
|
|
assets = await client.search_metadata(args, album_ids=all_album_ids, limit=count)
|
|
_enrich_assets(assets, asset_public_urls or {})
|
|
return _format_assets(assets, "find", args, locale, response_mode, client, cmd_templates)
|
|
|
|
|
|
async def cmd_person(
|
|
client: Any, args: str, count: int,
|
|
locale: str, response_mode: str,
|
|
cmd_templates: dict[str, dict[str, str]],
|
|
asset_public_urls: dict[str, str] | None = None,
|
|
) -> str | dict[str, Any]:
|
|
"""Handle /person command."""
|
|
if not args:
|
|
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "person", "query": ""})
|
|
people = await client.get_people()
|
|
person_id = None
|
|
for pid, pname in people.items():
|
|
if args.lower() in pname.lower():
|
|
person_id = pid
|
|
break
|
|
if not person_id:
|
|
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "person", "query": args})
|
|
assets = await client.search_by_person(person_id, limit=count)
|
|
_enrich_assets(assets, asset_public_urls or {})
|
|
return _format_assets(assets, "person", args, locale, response_mode, client, cmd_templates)
|
|
|
|
|
|
async def cmd_place(
|
|
client: Any, args: str, all_album_ids: list[str], count: int,
|
|
locale: str, response_mode: str,
|
|
cmd_templates: dict[str, dict[str, str]],
|
|
asset_public_urls: dict[str, str] | None = None,
|
|
) -> str | dict[str, Any]:
|
|
"""Handle /place command."""
|
|
if not args:
|
|
return _render_cmd_template(cmd_templates, "no_results", locale, {"command": "place", "query": ""})
|
|
assets = await client.search_smart(
|
|
f"photos taken in {args}", album_ids=all_album_ids, limit=count
|
|
)
|
|
_enrich_assets(assets, asset_public_urls or {})
|
|
return _format_assets(assets, "place", args, locale, response_mode, client, cmd_templates)
|