Restructure data model: TrackingConfig + TemplateConfig entities
Some checks failed
Validate / Hassfest (push) Has been cancelled
Some checks failed
Validate / Hassfest (push) Has been cancelled
Major model restructuring for clean separation of concerns: New entities: - TrackingConfig: What to react to (event types, asset filters, periodic/scheduled/memory mode config) - reusable across targets - TemplateConfig: All ~15 template slots from blueprint (event messages, asset formatting, date/location, scheduled messages) with full defaults - separate entities per locale Changed entities: - AlbumTracker: Simplified to album selection + polling + target_ids (removed event_types, template_id, all filter fields) - NotificationTarget: Extended with tracking_config_id and template_config_id FKs (many-to-one, reusable configs) Removed entities: - MessageTemplate (replaced by TemplateConfig) - ScheduledJob (absorbed into TrackingConfig) Updated services: - watcher.py: Each target checked against its own tracking_config for event filtering before sending notification - notifier.py: Uses target's template_config to select the right template slot based on event type New API routes: - /api/tracking-configs/* (CRUD) - /api/template-configs/* (CRUD + per-slot preview) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ from jinja2.sandbox import SandboxedEnvironment
|
||||
|
||||
from immich_watcher_core.telegram.client import TelegramClient
|
||||
|
||||
from ..database.models import MessageTemplate, NotificationTarget
|
||||
from ..database.models import NotificationTarget, TemplateConfig
|
||||
from ..webhook.client import WebhookClient
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -33,7 +33,7 @@ def render_template(template_body: str, context: dict[str, Any]) -> str:
|
||||
async def send_notification(
|
||||
target: NotificationTarget,
|
||||
event_data: dict[str, Any],
|
||||
template: MessageTemplate | None = None,
|
||||
template_config: TemplateConfig | None = None,
|
||||
use_ai_caption: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Send a notification to a target using event data.
|
||||
@@ -41,7 +41,7 @@ async def send_notification(
|
||||
Args:
|
||||
target: Notification destination (telegram or webhook)
|
||||
event_data: Album change event data (album_name, added_count, etc.)
|
||||
template: Optional message template (uses default if None)
|
||||
template_config: Optional template config with per-event templates
|
||||
use_ai_caption: If True, generate caption with Claude AI instead of template
|
||||
"""
|
||||
message = None
|
||||
@@ -54,7 +54,18 @@ async def send_notification(
|
||||
|
||||
# Fall back to template rendering
|
||||
if message is None:
|
||||
template_body = template.body if template else DEFAULT_TEMPLATE
|
||||
template_body = DEFAULT_TEMPLATE
|
||||
if template_config:
|
||||
# Select the right template slot based on event type
|
||||
change_type = event_data.get("change_type", "")
|
||||
slot_map = {
|
||||
"assets_added": "message_assets_added",
|
||||
"assets_removed": "message_assets_removed",
|
||||
"album_renamed": "message_album_renamed",
|
||||
"album_deleted": "message_album_deleted",
|
||||
}
|
||||
slot = slot_map.get(change_type, "message_assets_added")
|
||||
template_body = getattr(template_config, slot, DEFAULT_TEMPLATE) or DEFAULT_TEMPLATE
|
||||
try:
|
||||
message = render_template(template_body, event_data)
|
||||
except jinja2.TemplateError as e:
|
||||
|
||||
@@ -22,8 +22,9 @@ from ..database.models import (
|
||||
AlbumTracker,
|
||||
EventLog,
|
||||
ImmichServer,
|
||||
MessageTemplate,
|
||||
NotificationTarget,
|
||||
TemplateConfig,
|
||||
TrackingConfig,
|
||||
)
|
||||
from .notifier import send_notification
|
||||
|
||||
@@ -58,9 +59,7 @@ async def check_tracker_with_session(
|
||||
# Eagerly read all needed data before entering aiohttp context
|
||||
# (SQLAlchemy async greenlet context doesn't survive across other async CMs)
|
||||
album_ids = list(tracker.album_ids)
|
||||
event_types = list(tracker.event_types)
|
||||
target_ids = list(tracker.target_ids)
|
||||
template_id = tracker.template_id
|
||||
tracker_db_id = tracker_id
|
||||
server_url = server.url
|
||||
server_api_key = server.api_key
|
||||
@@ -69,14 +68,13 @@ async def check_tracker_with_session(
|
||||
async with aiohttp.ClientSession() as http_session:
|
||||
client = ImmichClient(http_session, server_url, server_api_key)
|
||||
|
||||
# Fetch server config for external domain
|
||||
await client.get_server_config()
|
||||
users_cache = await client.get_users()
|
||||
|
||||
for album_id in album_ids:
|
||||
result = await _check_album(
|
||||
session, http_session, client, tracker_db_id,
|
||||
album_id, users_cache, event_types, target_ids, template_id,
|
||||
album_id, users_cache, target_ids,
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
@@ -91,9 +89,7 @@ async def _check_album(
|
||||
tracker_id: int,
|
||||
album_id: str,
|
||||
users_cache: dict[str, str],
|
||||
event_types: list[str],
|
||||
target_ids: list[int],
|
||||
template_id: int | None,
|
||||
) -> dict[str, Any]:
|
||||
"""Check a single album for changes."""
|
||||
try:
|
||||
@@ -157,10 +153,6 @@ async def _check_album(
|
||||
if change is None:
|
||||
return {"album_id": album_id, "status": "no_changes"}
|
||||
|
||||
# Check if this event type is tracked
|
||||
if change.change_type not in event_types and "changed" not in event_types:
|
||||
return {"album_id": album_id, "status": "filtered", "change_type": change.change_type}
|
||||
|
||||
# Log the event
|
||||
shared_links = await client.get_shared_links(album_id)
|
||||
event_data = _build_event_data(change, album, client.external_url, shared_links)
|
||||
@@ -174,19 +166,41 @@ async def _check_album(
|
||||
)
|
||||
session.add(event_log)
|
||||
|
||||
# Send notifications to all configured targets
|
||||
# Send notifications to each target, filtered by its tracking config
|
||||
for target_id in target_ids:
|
||||
target = await session.get(NotificationTarget, target_id)
|
||||
if not target:
|
||||
continue
|
||||
|
||||
template = None
|
||||
if template_id:
|
||||
template = await session.get(MessageTemplate, template_id)
|
||||
# Check target's tracking config for event filtering
|
||||
tracking_config = None
|
||||
if target.tracking_config_id:
|
||||
tracking_config = await session.get(TrackingConfig, target.tracking_config_id)
|
||||
|
||||
if tracking_config:
|
||||
# Filter by event type
|
||||
should_notify = False
|
||||
if change.change_type == "assets_added" and tracking_config.track_assets_added:
|
||||
should_notify = True
|
||||
elif change.change_type == "assets_removed" and tracking_config.track_assets_removed:
|
||||
should_notify = True
|
||||
elif change.change_type == "album_renamed" and tracking_config.track_album_renamed:
|
||||
should_notify = True
|
||||
elif change.change_type == "album_deleted" and tracking_config.track_album_deleted:
|
||||
should_notify = True
|
||||
elif change.change_type == "changed":
|
||||
should_notify = True # "changed" = mixed, always notify
|
||||
if not should_notify:
|
||||
continue
|
||||
|
||||
# Get target's template config
|
||||
template_config = None
|
||||
if target.template_config_id:
|
||||
template_config = await session.get(TemplateConfig, target.template_config_id)
|
||||
|
||||
try:
|
||||
use_ai = target.config.get("ai_captions", False)
|
||||
await send_notification(target, event_data, template, use_ai_caption=use_ai)
|
||||
await send_notification(target, event_data, template_config, use_ai_caption=use_ai)
|
||||
except Exception:
|
||||
_LOGGER.exception("Failed to send notification to target %d", target_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user