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
+85
View File
@@ -0,0 +1,85 @@
<script lang="ts">
import type { Snippet } from 'svelte';
let {
variant = 'primary',
size = 'md',
disabled = false,
type = 'button',
href,
onclick,
children,
class: extraClass = '',
}: {
variant?: 'primary' | 'secondary' | 'danger' | 'ghost';
size?: 'sm' | 'md';
disabled?: boolean;
type?: 'button' | 'submit';
href?: string;
onclick?: (e: MouseEvent) => void;
children: Snippet;
class?: string;
} = $props();
const baseClasses = 'inline-flex items-center justify-center gap-1.5 rounded-md text-sm font-medium transition-colors disabled:opacity-50';
const sizeClasses: Record<string, string> = {
sm: 'px-2.5 py-1 text-xs',
md: 'px-4 py-2',
};
const variantClasses: Record<string, string> = {
primary: 'btn-primary',
secondary: 'btn-secondary',
danger: 'btn-danger',
ghost: 'btn-ghost',
};
const classes = $derived(
`${baseClasses} ${sizeClasses[size]} ${variantClasses[variant]} ${extraClass}`.trim()
);
</script>
{#if href && !disabled}
<a {href} class={classes} onclick={onclick}>
{@render children()}
</a>
{:else}
<button {type} {disabled} class={classes} onclick={onclick}>
{@render children()}
</button>
{/if}
<style>
.btn-primary {
background: var(--color-primary);
color: var(--color-primary-foreground);
}
.btn-primary:hover:not(:disabled) {
opacity: 0.9;
}
.btn-secondary {
background: var(--color-muted);
color: var(--color-foreground);
border: 1px solid var(--color-border);
}
.btn-secondary:hover:not(:disabled) {
opacity: 0.8;
}
.btn-danger {
background: var(--color-error-fg);
color: white;
}
.btn-danger:hover:not(:disabled) {
opacity: 0.9;
}
.btn-ghost {
background: transparent;
color: var(--color-muted-foreground);
}
.btn-ghost:hover:not(:disabled) {
background: var(--color-muted);
color: var(--color-foreground);
}
</style>
@@ -1,5 +1,6 @@
<script lang="ts">
import MdiIcon from './MdiIcon.svelte';
import { t } from '$lib/i18n';
export interface EntityItem {
value: string | number;
@@ -142,7 +143,7 @@
<div class="ep-list" bind:this={listEl} role="listbox">
{#if filtered.length === 0}
<div class="ep-empty">No matches</div>
<div class="ep-empty">{t('common.noMatches')}</div>
{:else}
{#each filtered as item, i}
<button
@@ -0,0 +1,13 @@
<script lang="ts">
interface Props {
message: string;
class?: string;
}
let { message, class: className = '' }: Props = $props();
</script>
{#if message}
<div class="bg-[var(--color-error-bg)] text-[var(--color-error-fg)] text-sm rounded-md p-3 mb-4 {className}">
{message}
</div>
{/if}
@@ -1,5 +1,6 @@
<script lang="ts">
import MdiIcon from './MdiIcon.svelte';
import { t } from '$lib/i18n';
export interface GridItem {
value: string | number;
@@ -117,7 +118,7 @@
</button>
{/each}
{#if filtered.length === 0}
<div class="icon-grid-empty" style="grid-column: 1 / -1; text-align: center; padding: 0.75rem; color: var(--color-muted-foreground); font-size: 0.75rem;">No matches</div>
<div class="icon-grid-empty" style="grid-column: 1 / -1; text-align: center; padding: 0.75rem; color: var(--color-muted-foreground); font-size: 0.75rem;">{t('common.noMatches')}</div>
{/if}
</div>
</div>
+2 -1
View File
@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte';
import MdiIcon from './MdiIcon.svelte';
import { t } from '$lib/i18n';
let { open = false, title = '', onclose, children } = $props<{
open: boolean;
@@ -93,7 +94,7 @@
>
<div style="display: flex; align-items: center; justify-content: space-between; padding: 1.5rem 1.5rem 1rem;">
<h3 id="modal-title-{uniqueId}" style="font-size: 1.125rem; font-weight: 600;">{title}</h3>
<button class="modal-close" onclick={onclose} aria-label="Close">
<button class="modal-close" onclick={onclose} aria-label={t('common.close')}>
<MdiIcon name="mdiClose" size={18} />
</button>
</div>
@@ -1,5 +1,6 @@
<script lang="ts">
import MdiIcon from './MdiIcon.svelte';
import { t } from '$lib/i18n';
export interface MultiEntityItem {
value: string;
@@ -132,7 +133,7 @@
<div class="mes-list" bind:this={listEl} role="listbox">
{#if filtered.length === 0}
<div class="mes-empty">No matches</div>
<div class="mes-empty">{t('common.noMatches')}</div>
{:else}
{#each filtered as item, i}
{@const checked = (values || []).includes(item.value)}
+1 -1
View File
@@ -56,7 +56,7 @@
{/if}
{/if}
</div>
<button class="snack-close" onclick={() => removeSnack(snack.id)} aria-label="Dismiss">
<button class="snack-close" onclick={() => removeSnack(snack.id)} aria-label={t('common.dismiss')}>
<MdiIcon name="mdiClose" size={14} />
</button>
</div>